示例#1
0
文件: helper.cpp 项目: lczech/genesis
void reset_edge_nums( PlacementTree& tree )
{
    // Edge numbers need to be in ascending order via postorder traversal.
    int current = 0;
    for( auto it : postorder(tree) ) {
        // The last iteration is skipped, as the root does not have an edge.
        if (it.is_last_iteration()) {
            continue;
        }

        it.edge().data<PlacementEdgeData>().reset_edge_num( current );
        ++current;
    }
}
int main(void)
{
    int n; scanf("%d", &n);
    int *a = malloc(sizeof(int) * n);
    for (int i = 0; i < n; i++)
        scanf("%d", &a[i]);
    if (judge(n, a, 0)) {
        puts("YES");
        tree_ptr h = NULL;
        for (int i = 0; i < n; i++)
            h = insert(h, a[i], 0);
        postorder(h, h);
    } else if (judge(n, a, 1)) {
        puts("YES");
        tree_ptr h = NULL;
        for (int i = 0; i < n; i++)
            h = insert(h, a[i], 1);
        postorder(h, h); 
    } else {
        puts("NO");
    }
    free(a);
}
示例#3
0
void newicktree(int n,int* & P,int* & Suc1,int* & Suc2,string* & Labels,double* & B,string* & Support,FILE *w){
    list<int> post_order=postorder(P,Suc1,Suc2,n);
    int r=0;
    for (list<int>::iterator iter = post_order.begin();iter!=post_order.end();iter++){
        int i = *iter;
        int s1 = Suc1[i];
        int s2 = Suc2[i];
        ostringstream st1,st2;
        st1<< B[s1];
        st2<< B[s2];
        Labels[i]="("+Labels[s1]+Support[s1]+":"+st1.str()+","+Labels[s2]+Support[s2]+":"+st2.str()+")";
        r=i;
    }
    fprintf(w,"%s;\n",Labels[r].c_str());
}
示例#4
0
int main()
{
	struct node * root = newnode(1);
	root->left=newnode(2);
	root->right=newnode(3);
	root->left->left=newnode(4);
	root->left->right=newnode(5);
	printf("preorder\n");
	preorder(root);
	printf("INorder\n");
	inorder(root);
	printf("postorder\n");
	postorder(root);
	return 0;
}
示例#5
0
int main(void)
{
	struct node* root = NULL;
	//struct node* root2 = NULL;
	struct node *find = NULL;

	char str[1024];

	root = insert(root, 10);
	root = insert(root, 15);
	root = insert(root, 9);
	root = insert(root, 8);
	root = insert(root, 13);
	root = insert(root, 7);
	root = insert(root, 5);
	root = insert(root, 18);
	root = insert(root, 22);
	root = insert(root, 3);
	root = insert(root, 4);
	root = insert(root, 2);
	root = insert(root, 1);
	print_ascii_tree(root);
	find = search(root, 18);
	print_ascii_tree(root);
	find = search(root, 22);
	printf("\n\n\nDATA found is %d\n", find->data);
	find = min_node(root);
	printf("Min in this tree is %d\n", find->data);
	find = max_node(root);
	printf("Mx in this tree is %d\n", find->data);
	print_ascii_tree(root);
	preorder(root);
	printf("\n");
	inorder(root);
	printf("\n");
	postorder(root);
	printf("\n");
	printf("DEPTH is %d\n", depth(root));
	tree_to_string(root, str);
	printf("The STR generated is %s\n", str);
	//string_to_tree(&root2, str);
	//print_ascii_tree(root2);
	printf("COUNT is %d\n",nodes(root));
	bool res = hassum(root, 45);
	printf("Bool val is %d\n", (int)res);
	levelorder(root);
	return 0;
}
示例#6
0
main()
{
	int choice,num;
	root=NULL;
	while(1)
	{
		printf("\n");
		printf("1.Insert\n");
		printf("2.Delete\n");
		printf("3.Inorder Traversal\n");
		printf("4.Preorder Traversal\n");
		printf("5.Postorder Traversal\n");
		printf("6.Display\n");
		printf("7.Quit\n");
		printf("Enter your choice : ");
		scanf("%d",&choice);

		switch(choice)
		{
		 case 1:
			printf("Enter the number to be inserted : ");
			scanf("%d",&num);
			insert(num);
			break;
		 case 2:
			printf("Enter the number to be deleted : ");
			scanf("%d",&num);
			del(num);
			break;
		 case 3:
			inorder(root);
			break;
		 case 4:
			preorder(root);
			break;
		 case 5:
			postorder(root);
			break;
		 case 6:
			display(root,1);
			break;
		 case 7:
			exit();
		 default:
			printf("Wrong choice\n");
		}/*End of switch */
	}/*End of while */
}/*End of main()*/
示例#7
0
文件: 5.c 项目: Bminus/Algorithms
int main()
{
	int n;
	scanf("%d",&n);
	int i;
	struct node* root=NULL;
	for(i=0;i<n;i++)
	{
		int a;
		scanf("%d",&a);
		insert(&root,a,NULL);
	}
	inorder(root);
	printf("\n");
	preorder(root);
	printf("\n");
	postorder(root);
	printf("\n");
	int search;
	scanf("%d",&search);
	int start=0;
	int end=k-1;
	while(start<=end)
	{
		if(arr[start]+arr[end]==search)
		{
			printf("%d %d",arr[start],arr[end]);
			break;
		}
		else if(arr[start]+arr[end]>search)
		{
			end--;
		}
		else
		{
			start++;
		}
	}
/*	searchi(root,search);
	//printf("%d",root->right->left->data);
	inorder(root);
	printf("\n");
	preorder(root);
	printf("\n");
	postorder(root);
	printf("\n");*/
	return 0;
}
示例#8
0
int main() 
{ 
	int n,x; 
	struct node *location,*parent; 
	do 
	{ 
		printf("1.insertion\n2.deletion\n3.search\n4.inorder traversal\n5.preorder traversal\n6.postorder\n7.exit\n"); 
		printf("\nenter ur choice\n"); 
		scanf("%d",&n); 
		switch(n) 
		{ 
			case 1: 
				printf("\nenter element to be inserted\n"); 
				scanf("%d",&x); 
				insert(x); 
				break; 
			case 2: 
				printf("\nenter element to be deleted\n"); 
				scanf("%d",&x); 
				del(x); 
				break; 
			case 3: 
				printf("\nenter element to be searched\n"); 
				scanf("%d",&x); 
				find(x,&parent,&location); 
				if(location!=NULL) 
				printf("\nsearch successfull\n"); 
				else 
				printf("\nitem not found\n"); 
				break; 
			case 4: 
				inorder(root); 
				break; 
			case 5: 
				preorder(root); 
				break; 
			case 6: 
				postorder(root); 
				break; 
			case 7: 
				return 0; 
				break; 
			default : 
				printf("\ndefault entry\n"); 
		} 
	} 
	while(n!=7); 
} 
示例#9
0
文件: exam010.cpp 项目: cqiyi/kaoshi
void main()
{
	tnode *r;
	int n=0;
	char pre[max],in[max];
	printf("input inorder and postorder!  \n");
	gets(pre);
	gets(in);
	mktree(pre,0,strlen(pre)-1,in,0,strlen(in)-1,&r);
	printf("The preorder is as follows  \n");
	postorder(r);
	printf("\n There are %5d leaves in the tree. \n", leaf(r));
	printf("\nThe one degree node is as follows:\n");
	oneleaf(r,&n);
	printf("\n The one degree nodes number is:%d\n",n);
}
示例#10
0
show(NodeT *root,int level)
{
    while ('\n' != getc(stdin));
    printf( "\nPreorder listing\n" );
    preorder( root, 0 );
    printf( "Press Enter to continue." );
    while ('\n' != getc(stdin));
    printf( "\nInorder listing\n" );
    inorder( root, 0 );
    printf( "Press Enter to continue." );
    while ('\n' != getc(stdin));
    printf( "\nPostorder listing\n" );
    postorder( root, 0 );
    printf( "Press Enter to continue." );
    while ('\n' != getc(stdin));
}
示例#11
0
文件: exam2.c 项目: jyshi/ece264
int main(int argc, char *argv[])
{
int array[] = {2, 3, 4, 5, 6, 8, 10, 12, 14, 15, 16, 18, 20};
int lb = 0;
int ub = sizeof(array)/sizeof(int) - 1;
tnode *root = tnode_construct_bst(array, lb, ub); 
preorder(root);
//Question
printf("\n");
postorder(root);
// Question
printf("\n");
tnode_destroy_bst(root);
// Question
return EXIT_SUCCESS;
}
TEST_F(SolutionTest, case1) {
	TreeNode *root = new TreeNode(1);
	root->left = new TreeNode(2);
	root->right = new TreeNode(3);
	root->left->left = new TreeNode(4);
	root->left->right = new TreeNode(5);
	root->right->left = new TreeNode(6);
	root->right->right = new TreeNode(7);

	int in[] = {4, 2, 5, 1, 6, 3, 7};
	int post[] = {4, 5, 2, 6, 7, 3, 1};
	vector<int> inorder(in, in + sizeof(in)/sizeof(int));
	vector<int> postorder(post, post + sizeof(post)/sizeof(int));
	TreeNode *node = solution_.buildTree(inorder, postorder);
	EXPECT_TRUE(solution_.isSameTree(root, node));
}
示例#13
0
/**************************************************
 * This function opens the file and reads each    *
 * line into the buildArray function. It then     *
 * calls the rest of the functions to sort the    *
 * array and build the balanced binary tree.      *
 * EXIT 2 for file input error                    *
 * EXIT 1 for memory error                        *
 **************************************************/
void initialize(char *filename)
{
    FILE *fp;
    char buffer[21], **names;
    int count = 0, *count_ptr = &count, size = 2, *size_ptr = &size;
    BNODE* root;

    if ((fp = fopen(filename, "r")) == NULL)
    {
        printf("Error: Filename does not exist\n");
        exit(2);
    }
    
    if ((names = malloc(2 * sizeof(char*))) == NULL)
    {
        printf("Error: Unable to allocate memory\n");
        exit(1);
    }

    while (fgets(buffer, sizeof(buffer), fp) != NULL)
    {
        /* Double the size of the array after it is full */
        if (count == size)
            names = resizeArray(names, size_ptr);

        names = buildArray(names, buffer, count_ptr);
    }

    fclose(fp);

    names = sortArray(names, count_ptr);

    root = buildTree(names, 0, count-1);

    printf("\n preorder: ");
    preorder(root);
    printf("\n\n");

    printf("  inorder: ");
    inorder(root);
    printf("\n\n");

    printf("postorder: ");
    postorder(root);
    printf("\n");
}
示例#14
0
/* Driver program to test above functions*/
int main()
{
     struct node *root  = newNode(1);
     root->left             = newNode(2);
     root->right           = newNode(3);
     root->left->left     = newNode(4);
     root->left->right   = newNode(5); 
 
     printf("\nPostorder traversal of binary tree is \n");
     postorder(root);
 
     printf("\nPostorder traversal using iterative approach\n");
     postorder_iterative(root);
 
     getchar();
     return 0;
}
示例#15
0
void postorder (astree* root) {
   assert (root != NULL);
   if (root->symbol == TOK_IFELSE) {
      emit (root);
      return;
   }
   for (size_t child = 0; child < root->children.size(); ++child) {
      
      if ((root->children.at(child)->symbol == TOK_WHILE) |
          (root->children.at(child)->symbol == TOK_IFELSE)|
          (root->children.at(child)->symbol == TOK_CALL)) {
         emit (root->children.at(child));
      }else
      postorder (root->children.at(child));
   }
   emit (root);
}
示例#16
0
void
loopit(Reg *r, int32 nr)
{
	Reg *r1;
	int32 i, d, me;

	if(nr > maxnr) {
		rpo2r = mal(nr * sizeof(Reg*));
		idom = mal(nr * sizeof(int32));
		maxnr = nr;
	}

	d = postorder(r, rpo2r, 0);
	if(d > nr)
		fatal("too many reg nodes %d %d", d, nr);
	nr = d;
	for(i = 0; i < nr / 2; i++) {
		r1 = rpo2r[i];
		rpo2r[i] = rpo2r[nr - 1 - i];
		rpo2r[nr - 1 - i] = r1;
	}
	for(i = 0; i < nr; i++)
		rpo2r[i]->rpo = i;

	idom[0] = 0;
	for(i = 0; i < nr; i++) {
		r1 = rpo2r[i];
		me = r1->rpo;
		d = -1;
		// rpo2r[r->rpo] == r protects against considering dead code,
		// which has r->rpo == 0.
		if(r1->p1 != R && rpo2r[r1->p1->rpo] == r1->p1 && r1->p1->rpo < me)
			d = r1->p1->rpo;
		for(r1 = r1->p2; r1 != nil; r1 = r1->p2link)
			if(rpo2r[r1->rpo] == r1 && r1->rpo < me)
				d = rpolca(idom, d, r1->rpo);
		idom[i] = d;
	}

	for(i = 0; i < nr; i++) {
		r1 = rpo2r[i];
		r1->loop++;
		if(r1->p2 != R && loophead(idom, r1))
			loopmark(rpo2r, i, r1);
	}
}
示例#17
0
文件: main.c 项目: Alecs94/DSA-lab
int main()
{

    //FILE*f=fopen("data.txt","r");
    //FILE*g=fopen("out.txt","w");
    Node*rad=constructie();
    printf("\npreordine\n");
    preorder(rad,0);
    printf("\ninordine\n");
    inorder(rad,0);
    printf("\npostorder\n");
    postorder(rad,0);
    //fclose(f);
    //fclose(g);

    return 0;
}
示例#18
0
void main()
{
	node *root;
    insert(&root, 9);
    insert(&root, 4);
    insert(&root, 15);
    insert(&root, 6);
    insert(&root, 12);
    insert(&root, 17);
    insert(&root, 2);
	printf("PREORDER\n");
	preorder(root);
	printf("INORDER\n");
	inorder(root);
	printf("POSTORDER\n");
	postorder(root);
}
void main(void)
{
  int ch;
  char y;
  int data;
  struct node *temp;
  
  while(1)
  {
    printf("\n\n\n1: Insert\t2: Inorder Traversal\t3: Preorder Traversal\t4: Postorder Traversal\t5: Exit\nEnter you choice: ");
    scanf("%d",&ch);
    

    switch(ch)
    {
      case 1:
          printf("\nEnter data value: ");
          scanf("%d",&data);
          temp=new_node(data);
          if(root==NULL)
          {
            root=temp;
          }
          else
          insert(root,temp);

        break;

      case 2:
        inorder(root);
        break;

      case 3:
        preorder(root);
        break;

      case 4:
        postorder(root);
        break;

      case 5:
        exit(0);
      default: printf("\nInvalid Choice !");
    }
  }
}
void main(){
int no,ch;
printf("Enter root\n");
scanf("%d",&no);
root=make(no);
p=root;
while(1){
printf("\nEnter another no\n");
printf("\nEnter 0 to terminate\n");
scanf("%d",&no);
if(no==0)
break;
p=root;
q=root;
while(no!=p->data && q!=NULL){
p=q;
if(no< p->data)
q=p->left;
else
q=p->right;}
if(no < p->data){
printf("Left branch of %d is %d",p->data,no);
left(p,no);}
else{
right(p,no);
printf("Right branch of %d is %d",p->data,no);
}}
while(1)
{
printf("\n1:Inorder\n2:Preorder\n3:Postorder\n");
printf("Enter choice\n");
scanf("%d",&ch);
switch(ch)
{
case 1: inorder(root);
	break;
case 2: preorder(root);
	break;
case 3: postorder(root);
	break;
case 4: exit(0);
default : printf("Invalid choice\n");
	 break;

}}}
示例#21
0
文件: reg.c 项目: carriercomm/legacy
void
loopit(Reg *r, int32_t nr)
{
	Reg *r1;
	int32_t i, d, me;

	if(nr > maxnr) {
		rpo2r = alloc(nr * sizeof(Reg*));
		idom = alloc(nr * sizeof(int32_t));
		maxnr = nr;
	}

	d = postorder(r, rpo2r, 0);
	if(d > nr)
		fatal(Z, "too many reg nodes");
	nr = d;
	for(i = 0; i < nr / 2; i++){
		r1 = rpo2r[i];
		rpo2r[i] = rpo2r[nr - 1 - i];
		rpo2r[nr - 1 - i] = r1;
	}
	for(i = 0; i < nr; i++)
		rpo2r[i]->rpo = i;

	idom[0] = 0;
	for(i = 0; i < nr; i++){
		r1 = rpo2r[i];
		me = r1->rpo;
		d = -1;
		if(r1->p1 != R && r1->p1->rpo < me)
			d = r1->p1->rpo;
		for(r1 = r1->p2; r1 != nil; r1 = r1->p2link)
			if(r1->rpo < me)
				d = rpolca(idom, d, r1->rpo);
		idom[i] = d;
	}

	for(i = 0; i < nr; i++){
		r1 = rpo2r[i];
		r1->loop++;
		if(r1->p2 != R && loophead(idom, r1))
			loopmark(rpo2r, i, r1);
	}
}
示例#22
0
文件: 22.c 项目: moonblade/dsa
void main()
{
  root=NULL;
  int val,opt;
  printf("Menu \n1.Insert \n2.Delete \n3.Inorder Traversal \n4.Preorder Traversal \n5.Postorder Traversal \n6.Exit");
  while(1)
    {
      opt=menu();
      switch(opt)
	{
	case 1:
	  printf("Enter value to insert : ");
	  scanf("%d",&val);
	  root=insert(val,root);
	  break;
	case 2:
	  printf("Enter element to delete : ");
	  scanf("%d",&val);
	  delete(val,root);
	  if(flag)
	    printf("%d Deleted\n",val);
	  else
	    printf("Not found\n");
	  break;
	case 3:
	  printf("Contents : ");
	  inorder(root);
	  break;
	case 4:
	  printf("Contents : ");
	  preorder(root);
	  break;
	case 5:
	  printf("Contents : ");
	  postorder(root);
	  break;
	case 6:
	  return;
	default:
	  printf("Invalid Option\n");
	  break;
	} 
    }
}
示例#23
0
文件: helper.cpp 项目: lczech/genesis
bool has_correct_edge_nums( PlacementTree const& tree )
{
    int current = 0;

    // Edge numbers need to be in ascending order via postorder traversal. Check this.
    for( auto it : postorder(tree) ) {
        // The last iteration is skipped, as the root does not have an edge.
        if (it.is_last_iteration()) {
            continue;
        }

        if( it.edge().data<PlacementEdgeData>().edge_num() != current) {
            return false;
        }
        ++current;
    }

    return true;
}
void main()
 {
    struct node *root=NULL;
	int element,i,n,ch;

	printf("Enter number of nodes: ");
	scanf("%d",&n);
	printf("Enter the elements:- \n");
	for(i=0;i<n;i++)
	{
		scanf("%d",&element);
		create_node(&root,element);
	}

	while(ch!=4)
	{
		system("cls");
		printf("\n1.Preorder\n2.Inorder\n3.Postorder\n4.Exit");
		printf("\nEnter choice: ");
		scanf("%d",&ch);
		switch(ch)
		{
			case 1:
				printf("\nPreorder Traversal:\n");
				preorder(root);
				getch();
				break;
			case 2:
				printf("\nInorder Traversal:\n");
				inorder(root);
				getch();
				break;
			case 3:
				printf("\nPostorder Traversal:\n");
				postorder(root);
				getch();
				break;
			case 4:
				break;
		}
	}
 }
示例#25
0
文件: testbin.c 项目: DiegoEstrada/C
int main(){
    Arbin a1=cons(50,cons(25,vacio(),vacio()),cons(75,vacio(),vacio()));
    Arbin a2=cons(150,cons(125,vacio(),vacio()),cons(200,vacio(),vacio())); 
    Arbin a3;
      
    printf("El arbol prueba tiene %d datos.\n",numelem(a1));
    
    printf("El arbol prueba tiene %d datos.\n",numelem(a3=cons(100,a1,a2)));    

    puts("Inorder:");
    inorder(a3);
    puts("Preorder:");
    preorder(a3);
    puts("Postorder:");
    postorder(a3);


    
    return 0;   
}
示例#26
0
int main() {
	int n;
	while (scanf("%d", &n) == 1) {
		cnt = 0;
		for (int i = 0; i < n; i++)
			scanf("%d", &pre[i]);
		for (int i = 0; i < n; i++) {
			scanf("%d", &in[i]);
			pos[in[i]] = i;
		}
		cnt1 = 0;
		postorder(build(0, 0, n));
		for (int i = 0; i < cnt1; i++) {
			if (i) printf(" ");
			printf("%d", ans[i]);
		}
		puts("");
	}
	return 0;
}
示例#27
0
int main() {
    time_t t;
    srand((unsigned)time(&t));
    int i=0,ar[1000],si[1];
    si[0]=0;
    tnp tree=NULL;
    lnp maxnodeu[0];
    tree=magicalbinarytreemaker(100,ar,si);
    inorder(tree); printf("\n\n");
    preorder(tree); printf("\n\n");
    postorder(tree); printf("\n\n");
    maxl(tree,0);
    //printf("The maximum sum from path to leaf is %d and way :\n",maxleafl);
    pleaf(tree,0); printf("\n\n");
    maxn(tree,maxnodeu);
    //printf("The maximum sum from node to node is %d and way :\n",maxnodel);
    for(i=0;i<maxnodeas;i++) {
        printf("%d ",maxnodea[i]);
    }
}
示例#28
0
void main()
{
	char c,d;
	printf("enter root node..");
scanr:c=getche();
	if(!(isalpha(c)||('0'<=c&&c<='9')))
	{
		printf("\nonly alphabats & number is allowed!!!\n");
		goto scanr;
	}
	root=(struct neel *)malloc(sizeof(struct neel));
	root->x=c;
	root->l=NULL;
	root->r=NULL;
	for(;1;)
	{
		printf("\nmenu\n1.insert\n2.inorder travers\n3.preorder travers\n4.postorder travers\n0.exit");
		printf("\nenter your choice..\n");
		switch(getche())
		{
			case '1':printf("\nenter parent node & new node..");
					c=getche();
					printf("  ");
					d=getche();
					insert(c,d);
					break;
			case '2':travers('#');break;
			case '3':printf("\nPREORDER SEQUENCE :- ");
					preorder(root);
					break;
			case '4':printf("\nPOSTORDER SEQUENCE :- ");
					postorder(root);
					break;
			case '0':return;
					break;
			default:
				printf("wrong choice...");	
		}	
	}
getch();
}
示例#29
0
void main()
{
	treeNode* n7 = makeRootNode('D', NULL, NULL);
	treeNode* n6 = makeRootNode('C', NULL, NULL);
	treeNode* n5 = makeRootNode('B', NULL, NULL);
	treeNode* n4 = makeRootNode('A', NULL, NULL);
	treeNode* n3 = makeRootNode('/', n6, n7);
	treeNode* n2 = makeRootNode('*', n4, n5);
	treeNode* n1 = makeRootNode('-', n2, n3);

	printf("\n preorder  : ");
	preorder(n1);
	
	printf("\n inorder   : ");
	inorder(n1);
	
	printf("\n postorder : ");
	postorder(n1);

	getchar();
}
示例#30
0
void prefix_func (astree* root, astree* name) {
   current_string = func_prefix;
   *current_string += oc_oil (name) + " (";
   vector<astree*> paramlist = root->children[1]->children;
   for (size_t i = 0; i < paramlist.size(); ++i) {
      *current_string += "\n        ";
      if (paramlist[i]->attr[ATTR_array]) {
         *current_string += oc_oil (paramlist[i]->children[1]);
      }else{
         *current_string += oc_oil (paramlist[i]->children[0]);
      }
      if (i != paramlist.size() -1) *current_string += ",\n";
   }
  
   *current_string += ")\n{\n";
   postorder(root->children[2]);
   *current_string += "};\n";
   current_string = main_string;

   
}