0% found this document useful (0 votes)
6 views5 pages

Creation of Binary Tree

Creation of binar tree

Uploaded by

sajidtyagi460
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF or read online on Scribd
0% found this document useful (0 votes)
6 views5 pages

Creation of Binary Tree

Creation of binar tree

Uploaded by

sajidtyagi460
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF or read online on Scribd
You are on page 1/ 5
6.9.3 Creation of binary tree from inorder and preorder traversals The inorder, preorder or postorder traversals of different binary trees may be same, so if we are given_a single traversal we can’t construct a unique binary tree. However if we know the inorder and preorder traversals or inorder and postorder traversals then we can construct a unique binary tree. Note that inorder traversal is_ necessary for drawing the tree; we can’t draw a tree from only; preorder and postorder raversals. Suppose we are given a preorder XY and postorder YX then it is clear that &,is the root but we can't find out whether Y is left child or right child of X. ‘The procedure for constructing a binary tree fromvinorder and preorder traversals is {iyIn preorder traversal, first node is the root node, so we get the root node by taking the first node of preorder. Now alliremaining nodes form the left and right subtrees of the root. To divide them into left and right subtrees we look at the inorder traversal. . Gi) In inorder traversal, root is in the middle, and nodes to the left of the root node are nodes of left subtree of node, and nodes to the right of the root node are nodes of right subtree of root node, Since we know the root, no rst node of preorder), we can separate the nodes of left and right subtrees,’ (ii) Now we can follow the same procedure for both left and right subtrees tll we get an S™mply subtree or a single node in subtree. Inorder and preorder the whole tree. Latus construct a binary tree from given preorder and inorder traversals. . pene ABDHECFIGJK Inoder_ : DHBEAIFCJGK i Ih preorder the root node. Hence A is the root of the binary tree, From inores sw se8 ‘hat SSE a aes tn are nodes D, H, B, E so these nodes form the left subtree of A, similarly . ot 3 Nodes 1, F.C, J, ran pa the right subtree of A since they are to the right of A. ag for these sublrees can be obtained from the inorder and preorder of | In: 1FCIGK Left subtree of A subtree of A Left subtree of A : From preorder we get node B as root : ; From inorder we get nodes D, 11 in left subtree of B, and node E in right subtree of B. Right A: From preorder we get C as the root From inorder we get nodes I, F in left subtree of C, and nodes J, G, K in right subtree of C. Left subtree of B Left subtree of C Right subtree of C Le subtree of B: From preorder we get D as the root : From inorder we get empty left subtree of D, and node H in right subtree of D.) Left subtree of C : From preorder we get F as the root From inorder we get node | in left subtree of F, and empty right subtree of F. Right subtree of C: From preorder we get G as the root From inorder we get node J in left subtree of G, and node K in right subtree of G. Figure 6.20 6.9.4 Creation of binary tree from inorder and postorder traversals it We can create a binary tree from postorder and inorder traversals using a similar procedure as desc Previous section, The only difference is that here we get the root node by taking the last node of po traversal. Let us construct a binary tree from given postorder and inorder traversals, Postorder: HIDJEBKFGCA ' Inorder : HDIBEJAKFCG Node A is last node in postorder traversal, so it will be the root of the tree. From inorder, we see that nodes to the left of the root node A are nodes H, D, I, B, E, J, so these nodes form the left subtree of Asc K, F.C, G form the right subtree of A since they ae to the right of A. ‘Av similarly nodes In: HDIBE) Left subtree of A Right subtree of A Left subtree of A: From postorder we get node B as root. From inorder we get nodes H, D, I in left subtree of B, and nodes E, Jin right subtree of B. Right subtree of A : From postorder we get node C as root. From inorder we get nodes K, F in left subtree of C, and node G in right subtree of C. Leftsubtree of B. Right subtree of B_ Left subtree of C Left subtree of B : From postorder we get node D as root. ; From inorder we get nodes H in left subtree of D, and node | in right subtree of D. Right subtree of B : From postorder we get node E as root. ; From inorder we get empty lft subtree of E, and node Jin right subtree of E. Left subtree of C : From postorder we get node F as root. From inorder we get node K in left subtree of F, and empty right subtree of F. Now we'll see a quicker method of creating the tree from preorder and inorder traversal. In preorder wot the nodes one by one and keep inserting them inthe tree. In inorder traversal, underline the nodes travel ABDGHEICFIK GDHBEIACIJFK Preorder Inorder : Insert A: GDHBEIACIJFK Ais the first node in preorder, hence it is root of the ree. Insert B: ; GDHBEIACIJFK B is to left of A, hence it is left child of A. Insert D: GDHBEIACJFK Dis to the left of B, hence D is left child of B. Insert G: GDHBEIACIFK Gis to the left of D, hence G is left child of D. Insert H = GDHBEIACIFK H is to the left of B and right of D, hence H is right child of D. Insert EB: ae GDHBEIAC)I Eis to the left of A and right of B, hence Eis right child of B. {BEIACIFK fe wt ‘of A and right of E, hence lis right child of E. BELACIFK oon EY ‘of A, hence C is right child'of A. a : in a ‘fave been inserted. To insert a node in its proper position n the tree, we will look at that node in “ ‘and insert it according toits ‘position with respect to the underlined nodes. . @ ©) a ® y o © ) ws 6 eG) a Co © ® O . gw ) CC @ Sd 0 ® g od, Q @ © ® DOD Insert F: GDHBEIACIFK Fis to the right of C, hence F is right child of C, Insert J: GDHBEIACJEK Jisto the right of C and to left of F, hence J is left child of F. Insert Ks GDHBEIACIEK K isto the right of F, hence K is right child of F.

You might also like