Пример #1
0
/* print binary tree postorder */
void print_postorder(struct BTreeNode *p) {
 if(p != NULL) {
  print_postorder(p->Left);
  print_postorder(p->Right);
  printf("%d ", p->Element);
 }
}
Пример #2
0
void print_postorder(t_nodo *nodo){
	if(nodo != NULL){
		print_postorder(nodo->sx);
		print_postorder(nodo->dx);
		printf("%s ",nodo->inf);
	}
}
Пример #3
0
void print_postorder(node * tree){
    if (tree){
        print_postorder(tree->left);
        print_postorder(tree->right);
        printf("%d\n",tree->data);
    }
}
Пример #4
0
/*
 * @brief	Utility to print postorder traversal sequence
 */
void print_postorder(node *root)
{
	if (root == NULL)
		return;
	print_postorder(root->left);
	print_postorder(root->right);
	printf("%d ", root->data);
}
Пример #5
0
void bst<T>::print_postorder(const binary_node<T>* node) const {
  if (node == NULL) {
    return;
  }
  print_postorder(node->left);
  print_postorder(node->right);
  std::cout << node->data << " ";
}
Пример #6
0
void print_postorder(Node * tree)
{
    if (tree) {
        print_postorder(tree->left);
        print_postorder(tree->right);
        printf("%d\n",tree->value);
    }
}
void print_postorder(node* temp){
	if(temp != NULL){
		print_postorder(temp->left);
		print_postorder(temp->right);
		printf("%d ",temp->data);
	}
	return;
}
Пример #8
0
//后序遍历
void print_postorder(binaryTreeNode * tree)
{
    if(tree!=NULL){
        print_postorder(tree->left);
        print_postorder(tree->right);
        printf("%d ",tree->value);
    }

}
Пример #9
0
/* print from node down, postorder */
void print_postorder(node_t *node)
{
	if (node == NULL)
		return;
	print_postorder(node->left);
	print_postorder(node->right);
	fprintf(stderr,"%s\n",node->name);
	return;
}
Пример #10
0
void print_postorder(node * tree)
{
    if (tree)
    {
        print_postorder(tree->left);
        print_postorder(tree->right);
        printf("%d %d %d -> %i \n", tree->r, tree->g, tree->b, tree->repetitions);
    }
}
Пример #11
0
//prints the tree in post_order form
void print_postorder(node *center)
{
    if (center!=NULL)
    {
        print_postorder(center->left);
        print_postorder(center->right);
        printf("%d\n",center->data);
    }
}
Пример #12
0
void print_postorder( BTNP btnptr )
{
  if(btnptr == (BTNP) 0)
    printf("");
  else {
    print_postorder(btnptr->left);
    print_postorder(btnptr->right);
    printf("%c ", btnptr->name);
  }
}
int main(){
	head = NULL;
	node* temp;
	
	int A[7] = {9,4,15,2,6,12,17};
	int i;
	for(i =0 ; i<7 ; i ++){
		temp = insert(head,A[i]);
	}
	
	printf("tree in print_preorder : \n");
	print_preorder(head);
	printf("\n");

	printf("tree in print_inorder : \n");
	print_inorder(head);
	printf("\n");

	printf("tree in print_postorder : \n");
	print_postorder(head);
	printf("\n");
	
	printf("all info\n");
	print_preorder_all(head);
	

	return 0;
}
Пример #14
0
/*
 * @brief	Driver function
 */
int main(int argc, char *argv[])
{
	node *root 		= add_node(1);
	root->left		= add_node(2);
	root->right		= add_node(3);
	root->left->left	= add_node(4);
	root->left->right	= add_node(5);
	root->right->left	= add_node(6);
	root->right->right	= add_node(7);
	root->right->left->left	= add_node(8);

	/* Preorder traversal sequence */
	printf("Preorder traversal sequence -\n");
	print_preorder(root);
	printf("\n\n");

	/* Inorder traversal sequence */
	printf("Inorder traversal sequence -\n");
	print_inorder(root);
	printf("\n\n");

	/* Postorder traversal sequence */
	printf("Postorder traversal sequence -\n");
	print_postorder(root);
	printf("\n\n");

	return 0;
}
TreeNode *print_postorder(char *pre_order, char *in_order, int length)
{
    if(length)
    {
        TreeNode *tnode = (TreeNode *)malloc(sizeof(TreeNode)); //为结点申请内存,创建一个新节点
        tnode->data = pre_order[0]; //根结点赋值
        int i = 0;
        while(pre_order[0] != in_order[i] && i < length)
        {
            i++;   //在中序序列中找到根结点,分割成左右子树
        }
        tnode->lchild = print_postorder(pre_order + 1, in_order, i); //先输出左子树
        tnode->rchild = print_postorder(pre_order + i + 1, in_order + i + 1, length - i - 1); //再输出右子树
        printf("%c ", pre_order[0]);
        return tnode;
    }
    return NULL;
}
Пример #16
0
static void print_postorder(const BiTreeNode *node) {

/*****************************************************************************
*                                                                            *
*  Display the binary tree rooted at the specified node in postorder.        *
*                                                                            *
*****************************************************************************/

if (!bitree_is_eob(node)) {

   if (!bitree_is_eob(bitree_left(node)))
      print_postorder(bitree_left(node));

   if (!bitree_is_eob(bitree_right(node)))
      print_postorder(bitree_right(node));

   fprintf(stdout, "Node=%03d\n", *(int *)bitree_data(node));

}

return;

}
Пример #17
0
int main(int argc, char *argv[])
{
    int n=0;
    int i=0;
    int val=0;
    tnode *root=NULL;

    int a=10, b=10;
    swap(&a, &b);
    printf("a(%d) b(%d)",a,b);


#if HARDCODED
    n=10;
    int a[10] = {15, 41, 6, 87, 9, 99, 150, 1, 75, 66 };

    for(i=0;i<n;i++) {
        val=a[i];
        printf("Inserting %d to Tree...\n",val);
        root=insert_tnode(root, val);

    }
#else
    scanf("%d", &n);
    printf ("Reading %d integers",n);
    for(i=0;i<n;i++) {
        scanf("%d", &val);
        printf("Inserting %d to Tree...\n",val);
        root=insert_tnode(root, val);
    }
#endif
    printf("Inorder Tree...\n");
    print_inorder(root);


    printf("PreOrder Tree...\n");
    print_preorder(root);


    printf("PostOrder Tree...\n");
    print_postorder(root);

    printf("Leafs of the Tree...\n");
    print_leaf(root);

    printf("Program Exited Normally....\n");


}
Пример #18
0
void main()
{
    node *root;
    node *tmp;
    //int i;
    int vetor[] = {50, 95, 180, 34, 119, 11, 123, 62, 64, 199};
    root = NULL;
    /* Inserting nodes into tree */
    int i;
    for(i = 0; i < 10; i++) {
        insert(&root, vetor[i], 0);
    }
    /*int altura = (7/2) - (1);
    insert(&root, 9,0);
    insert(&root, 4,0);
    insert(&root, 15,0);
    insert(&root, 6,0);
    insert(&root, 12,0);
    insert(&root, 17,0);
    insert(&root, 2,0);*/

    /* Printing nodes of tree */
    printf("Pre Order Display\n");
   // printf("%d\n", print_preorder(root , root->data, altura));

    printf("In Order Display\n");
    print_inorder(root);

    printf("Post Order Display\n");
    print_postorder(root);

    //isLeaf(root);
    /* Search node into tree */
    tmp = search(&root, 62);
    //printf("%d\n", tmp);
    if (tmp)
    {
        printf("Searched node=%d\n", tmp->data);
    }
    else
    {
        printf("Data Not found in tree.\n");
    }

    /* Deleting all nodes of tree */
    deltree(root);
}
Пример #19
0
int main()
{
    char input[3];
    int flag,data=0,h;
    //dynamic allocatio of the root
    root=(node*)malloc(sizeof(node));
    printf("\nEnter the data to the node");
    //assign data
    scanf("%d",&root->data);
    root->left=NULL;
    root->right=NULL;
    //for new node
    printf("\nDo you want to enter new node(yes/no):");
    scanf("%s",input);
    flag=string_test(input);
    //Iterate itself until flag is 1 i. the string entered is yes
    while(flag==1)
    {
        //for new node
        printf("\nEnter the data to the new node");
        //scan data
        scanf("%d",&data);
        //calling the create node function to create new node
        create_node(root,data);
        printf("\nDo you want to enter new node(yes/no):");
        scanf("%s",input);
        //calling string_test fuction to check whether the input is yes or no
        flag=string_test(input);
    }
    //display the tree in pre_order
    printf("\npreorder\n");
    print_preorder(root);
    //display the tree in in_order
    printf("\ninorder\n");
    print_inorder(root);
    //display the tree in post_order
    printf("\npostorder\n");
    print_postorder(root);
    path(root);
    h=arraymax(root);
    printf("\nmax of the tree: %d",h);
    return 0;
}
int main()
{
    /** code here **/
    int num;
    TreeNode *result;
    char pre[20], in[20], post[20]; //默认最大支持20字符,可以自行更改
    printf("请选择要执行的功能,按回车确认\n");
    printf(" 1、已知先序和中序,求后序\n");
    printf(" 2、已知后序和中序,求先序\n");
    scanf("%d", &num);
    switch(num)
    {
    case 1:
    {
        printf("输入先序遍历序列\n");
        scanf("%s", pre);
        printf("输入中序遍历序列\n");
        scanf("%s", in);
        int len = strlen(pre);
        printf("后序遍历序列为:\n");
        result = print_postorder(pre, in, len);
        break;
    }
    case 2:
    {
        printf("输入中序遍历序列\n");
        scanf("%s", in);
        printf("输入后序遍历序列\n");
        scanf("%s", post);
        int len = strlen(in);
        printf("前序遍历序列为:\n");
        result = print_preorder(in, post, len);
        break;
    }
    default:
    {
        printf("输入有误,重试!\n");
    }
    }
    return 0;
}
Пример #21
0
int main()
{
    
    node *tmp;

    root = NULL;
    /* Inserting nodes into tree */
    insert(&root, 9);
    insert(&root, 4);
    insert(&root, 15);
    insert(root, 6);
    insert(&root, 12);
    insert(&root, 17);
    insert(&root, 2);

    /* Printing nodes of tree */
    printf("Pre Order Display\n");
    print_preorder(root);

    printf("In Order Display\n");
    print_inorder(root);

    printf("Post Order Display\n");
    print_postorder(root);

    /* Search node into tree */
    tmp = search(&root, 4);
    if (tmp)
    {
        printf("Searched node=%d\n", tmp->data);
    }
    else
    {
        printf("Data Not found in tree.\n");
    }

    /* Deleting all nodes of tree */
    deltree(root);

    return 0;
}
Пример #22
0
int main(int argc, char **argv){
	t_nodo *albero = NULL;
	int idx = 0;
	int max = argc;
	int result;

	albero = ins_nodo_prefissa(albero,argv,&idx);
	albero = ins_nodo_postfissa(albero,argv,&max);
	
	result = calcola(albero);

	printf("\npreorder: \n");
	print_preorder(albero);
	printf("\npostorder: \n");
	print_postorder(albero);
	printf("\ninorder: \n");
	print_inorder(albero);

	printf("\nRisultato = %d\n",result);

	return 0;
}
Пример #23
0
int main()
{
    struct Tnode *root = NULL;
    root = create_node(1);
    root->left = create_node(2);
    root->left->left = create_node(4);
    root->left->right = create_node(5);
    root->right = create_node(3);
    root->right->right = create_node(7);
    printf("Pre-order\n");
    print_preorder(root);
    printf("\n");
    pre_order_iterative(root);

    printf("Post-order\n");
    print_postorder(root);
    printf("\n");
    post_order_iterative(root);

    printf("In-order\n");
    print_inorder(root);
    printf("\n");
    in_order_iterative(root);
}
Пример #24
0
int main()
{
  /*  tree0 */
  /*          M         */
  /*         /  \       */
  /*        /    \      */
  /*       A      K     */
  /*      / \    / \    */
  /*     C   E F    G   */
  /*        /   \       */
  /*       N     P      */
  BTN N = {1,'N',(BTNP)0,(BTNP)0};
  BTN P = {2,'P',(BTNP)0,(BTNP)0};
  BTN C = {3,'C',(BTNP)0,(BTNP)0};
  BTN G = {4,'G',(BTNP)0,(BTNP)0};
  BTN E = {5,'E',&N,(BTNP)0};
  BTN F = {6,'F',(BTNP)0,&P};
  BTN A = {7,'A',&C,&E};
  BTN K = {8,'K',&F,&G};
  BTN M = {9,'M',&A,&K};
  BTNP tree0 = &M;

  printf("The weight and height of tree0 are %d and %d.\n",
       weightBT(tree0), heightBT(tree0));

  printf("tree0 preorder is ");
  print_preorder(tree0);
  printf("\ntree0 inorder is ");
  print_inorder(tree0);
  printf("\ntree0 postorder is ");
  print_postorder(tree0);
  printf("\n");

  BTNP tree1=0;
  insertBST(4,'G',&tree1);
  insertBST(5,'E',&tree1);
  insertBST(3,'C',&tree1);
  insertBST(2,'P',&tree1);
  insertBST(7,'A',&tree1);
  insertBST(8,'K',&tree1);
  insertBST(1,'N',&tree1);
  insertBST(9,'M',&tree1);
  insertBST(6,'F',&tree1);

//  print_preorder(tree1);
//  printf("\n");
//  BTN temp = {4, 'G', (BTNP) 0, (BTNP) 0};
//  BTNP tempp = (BTNP) malloc(sizeof(BTN));
//  *tempp = temp;
//  print_preorder(tempp);
//  BTNP temp2 = make_BTN(4, 'G', (BTNP) 0, (BTNP) 0);
//  print_preorder(temp2);
//  printf("\n%d\n", weightBT(temp2));
  printf("The weight and height of tree1  are %d and %d.\n",
         weightBT(tree1), heightBT(tree1));

  printf("tree1 preorder is ");
  print_preorder(tree1);
  printf("\ntree1 inorder is ");
  print_inorder(tree1);
  printf("\ntree1 postorder is ");
  print_postorder(tree1);
  printf("\n");

//  BTNP tree2 = &N;
//  BTNP tree3 = findBST(0, tree2);
//  printf("%d\n", (int) *tree3);
//  printBTN(tree3);
//  printf("\n");

  BTNP np;
  int i;
  for( i=0; i<11; i++){
    np = findBST(i,tree1);
    printf( "  key %2d",i) ;
    if( np ){
      printf( " has name "); 
      printBTN(np);
      printf("\n");;
    }
    else 
      printf(" is not in tree1.\n");
  }

//  findBST(4,tree1)->name = 'b';
  insertBST(4,'b', &tree1);
  printf("After changing the name of key 4\n");
  for( i=0; i<11; i++){
    np = findBST(i,tree1);
    printf( "  key %2d",i) ;
    if( np ){
      printf( " has name "); 
      printBTN(np);
      printf("\n");;
    }
    else 
      printf(" is not in tree1.\n");
  }

  return 0;
}
Пример #25
0
int main(int argc, char **argv) {

BiTree             tree;
BiTreeNode         *node;

int                i;

/*****************************************************************************
*                                                                            *
*  Initialize the binary tree.                                               *
*                                                                            *
*****************************************************************************/

bitree_init(&tree, free);

/*****************************************************************************
*                                                                            *
*  Perform some binary tree operations.                                      *
*                                                                            *
*****************************************************************************/

fprintf(stdout, "Inserting some nodes\n");

if (insert_int(&tree, 20) != 0)
   return 1;

if (insert_int(&tree, 10) != 0)
   return 1;

if (insert_int(&tree, 30) != 0)
   return 1;

if (insert_int(&tree, 15) != 0)
   return 1;

if (insert_int(&tree, 25) != 0)
   return 1;

if (insert_int(&tree, 70) != 0)
   return 1;

if (insert_int(&tree, 80) != 0)
   return 1;

if (insert_int(&tree, 23) != 0)
   return 1;

if (insert_int(&tree, 26) != 0)
   return 1;

if (insert_int(&tree, 5) != 0)
   return 1;

fprintf(stdout, "Tree size is %d\n", bitree_size(&tree));
fprintf(stdout, "(Preorder traversal)\n");
print_preorder(bitree_root(&tree));

i = 30;

if ((node = search_int(&tree, i)) == NULL) {

   fprintf(stdout, "Could not find %03d\n", i);

   }

else {

   fprintf(stdout, "Found %03d...Removing the left tree below it\n", i);
   bitree_rem_left(&tree, node);
   fprintf(stdout, "Tree size is %d\n", bitree_size(&tree));
   fprintf(stdout, "(Preorder traversal)\n");
   print_preorder(bitree_root(&tree));

}

i = 99;

if ((node = search_int(&tree, i)) == NULL) {

   fprintf(stdout, "Could not find %03d\n", i);

   }

else {

   fprintf(stdout, "Found %03d...Removing the right tree below it\n", i);
   bitree_rem_right(&tree, node);
   fprintf(stdout, "Tree size is %d\n", bitree_size(&tree));
   fprintf(stdout, "(Preorder traversal)\n");
   print_preorder(bitree_root(&tree));

}

i = 20;

if ((node = search_int(&tree, i)) == NULL) {

   fprintf(stdout, "Could not find %03d\n", i);

   }

else {

   fprintf(stdout, "Found %03d...Removing the right tree below it\n", i);
   bitree_rem_right(&tree, node);
   fprintf(stdout, "Tree size is %d\n", bitree_size(&tree));
   fprintf(stdout, "(Preorder traversal)\n");
   print_preorder(bitree_root(&tree));

}

i = bitree_is_leaf(bitree_root(&tree));
fprintf(stdout, "Testing bitree_is_leaf...Value=%d (0=OK)\n", i);
i = bitree_is_leaf(bitree_left((bitree_root(&tree))));
fprintf(stdout, "Testing bitree_is_leaf...Value=%d (0=OK)\n", i);
i = bitree_is_leaf(bitree_left(bitree_left((bitree_root(&tree)))));
fprintf(stdout, "Testing bitree_is_leaf...Value=%d (1=OK)\n", i);
i = bitree_is_leaf(bitree_right(bitree_left((bitree_root(&tree)))));
fprintf(stdout, "Testing bitree_is_leaf...Value=%d (1=OK)\n", i);

fprintf(stdout, "Inserting some nodes\n");

if (insert_int(&tree, 55) != 0)
   return 1;

if (insert_int(&tree, 44) != 0)
   return 1;

if (insert_int(&tree, 77) != 0)
   return 1;

if (insert_int(&tree, 11) != 0)
   return 1;

fprintf(stdout, "Tree size is %d\n", bitree_size(&tree));
fprintf(stdout, "(Preorder traversal)\n");
print_preorder(bitree_root(&tree));
fprintf(stdout, "(Inorder traversal)\n");
print_inorder(bitree_root(&tree));
fprintf(stdout, "(Postorder traversal)\n");
print_postorder(bitree_root(&tree));

/*****************************************************************************
*                                                                            *
*  Destroy the binary tree.                                                  *
*                                                                            *
*****************************************************************************/

fprintf(stdout, "Destroying the tree\n");
bitree_destroy(&tree);

return 0;

}
Пример #26
0
void bst<T>::print_postorder() const {
  print_postorder(root);
  std::cout << std::endl;
}