Construct tree from inorder and postorder
Write a C Program to construct binary tree from inorder and postorder. Here’s simple Program to construct binary tree from inorder and postorder in C Programming Language.
What is Tree ?
In linear data structure, data is organized in sequential order and in non-linear data structure, data is organized in random order. Tree is a very popular data structure used in wide range of applications.
A tree data structure can be defined as follows…
- Tree is a non-linear data structure which organizes data in hierarchical structure and this is a recursive definition.
A tree data structure can also be defined as follows…
- Tree data structure is a collection of data (Node) which is organized in hierarchical structure and this is a recursive definition.
Every individual element is called as Node. Node in a tree data structure, stores the actual data of that particular element and link to next element in hierarchical structure.
Below is the source code for C Program to construct binary tree from inorder and postorder which is successfully compiled and run on Windows System to produce desired output as shown below :
SOURCE CODE : :
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 |
/* C Program to construct binary tree from inorder and postorder */ #include<stdio.h> #include<stdlib.h> struct treenode { int info; struct treenode *lchild; struct treenode *rchild; }*root=NULL; struct listnode { struct listnode *prev; int info; struct listnode *next; }*post=NULL, *in=NULL; void display(struct treenode *ptr,int level); struct listnode *addtoempty(struct listnode *start,int data); struct listnode *addatend(struct listnode *start,int data); struct listnode *create_list(struct listnode *start, int n); struct treenode *construct(struct listnode *inptr,struct listnode *postptr, int num); void inorder(struct treenode *ptr); void preorder(struct treenode *ptr); void postorder(struct treenode *ptr); int main() { int n; printf("Enter the number of nodes : "); scanf("%d",&n); printf("\nEnter inorder :\n"); in = create_list(in,n); printf("\nEnter postorder:\n"); post = create_list(post,n); root = construct(in,post,n); printf("\nInorder : "); inorder(root); printf("\n\nPostorder : "); postorder(root); printf("\n\nPreorder : "); preorder(root); printf("\n\n\nTree is : \n"); display(root,0); printf("\n"); return 0; }/*End of main()*/ struct treenode *construct(struct listnode *inptr,struct listnode *postptr, int num) { struct treenode *temp; struct listnode *q, *ptr; int i,j; if(num==0) return NULL; ptr=postptr; for(i=1;i<num;i++) ptr = ptr->next; /*Now ptr points to last node of postorder which is root*/ temp=(struct treenode *)malloc(sizeof(struct treenode)); temp->info=ptr->info; temp->lchild = NULL; temp->rchild = NULL; if(num==1)/*if only one node in tree */ return temp; q=inptr; for(i=0; q->info!=ptr->info; i++ ) q = q->next; /*Now i denotes the number of nodes in left subtree and q points to root node in inorder list*/ /*For left subtree*/ temp->lchild = construct(inptr, postptr, i); /*For right subtree*/ for(j=1;j<=i;j++) postptr = postptr->next; temp->rchild = construct(q->next, postptr, num-i-1); return temp; }/*End of construct()*/ struct listnode *create_list(struct listnode *start, int n) { int i,data; start=NULL; for(i=1;i<=n;i++) { printf("Enter the element to be inserted : "); scanf("%d",&data); if(start==NULL) start=addtoempty(start,data); else start=addatend(start,data); } return start; }/*End of create_list()*/ struct listnode *addtoempty(struct listnode *start,int data) { struct listnode *tmp; tmp=(struct listnode*)malloc(sizeof(struct listnode)); tmp->info=data; tmp->prev=NULL; tmp->next=NULL; start=tmp; return start; }/*End of addtoempty( )*/ struct listnode *addatend(struct listnode *start,int data) { struct listnode *tmp,*p; tmp=(struct listnode*)malloc(sizeof(struct listnode)); tmp->info=data; p=start; while(p->next!=NULL) p=p->next; p->next=tmp; tmp->next=NULL; tmp->prev=p; return start; }/*End of addatend( )*/ void inorder(struct treenode *ptr) { if(root==NULL) { printf("Tree is empty"); return; } if(ptr!=NULL) { inorder(ptr->lchild); printf("%d ",ptr->info); inorder(ptr->rchild); } }/*End of inorder( )*/ void postorder(struct treenode *ptr) { if(root==NULL) { printf("Tree is empty"); return; } if(ptr!=NULL) { postorder(ptr->lchild); postorder(ptr->rchild); printf("%d ",ptr->info); } }/*End of postorder( )*/ void preorder(struct treenode *ptr) { if(root==NULL) { printf("Tree is empty"); return; } if(ptr!=NULL) { printf("%d ",ptr->info); preorder(ptr->lchild); preorder(ptr->rchild); } }/*End of preorder( )*/ void display(struct treenode *ptr,int level) { int i; if(ptr!=NULL) { display(ptr->rchild, level+1); printf("\n"); for (i = 0; i < level; i++) printf(" "); printf("%d", ptr->info); display(ptr->lchild, level+1); } }/*End of display()*/ |
OUTPUT : :
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 |
/* C Program to construct binary tree from inorder and postorder */ Enter the number of nodes : 7 Enter inorder : Enter the element to be inserted : 1 Enter the element to be inserted : 2 Enter the element to be inserted : 3 Enter the element to be inserted : 4 Enter the element to be inserted : 5 Enter the element to be inserted : 6 Enter the element to be inserted : 7 Enter postorder: Enter the element to be inserted : 7 Enter the element to be inserted : 6 Enter the element to be inserted : 5 Enter the element to be inserted : 4 Enter the element to be inserted : 3 Enter the element to be inserted : 2 Enter the element to be inserted : 1 Inorder : 1 2 3 4 5 6 7 Postorder : 7 6 5 4 3 2 1 Preorder : 1 2 3 4 5 6 7 Tree is : 7 6 5 4 3 2 1 Process returned 0 |
If you found any error or any queries related to the above program or any questions or reviews , you wanna to ask from us ,you may Contact Us through our contact Page or you can also comment below in the comment section.We will try our best to reach up to you in short interval.
Thanks for reading the post….