Open navigation menu
Close suggestions
Search
Search
en
Change Language
Upload
Sign in
Sign in
Download free for days
0 ratings
0% found this document useful (0 votes)
6 views
5 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
Download
Save
Save Creation of Binary Tree For Later
Share
0%
0% found this document useful, undefined
0%
, undefined
Print
Embed
Report
0 ratings
0% found this document useful (0 votes)
6 views
5 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
Carousel Previous
Carousel Next
Download
Save
Save Creation of Binary Tree For Later
Share
0%
0% found this document useful, undefined
0%
, undefined
Print
Embed
Report
Download
Save Creation of Binary Tree For Later
You are on page 1
/ 5
Search
Fullscreen
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 @ © ® DODInsert 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
Data Structures - Unit 5 Tree PDF
PDF
No ratings yet
Data Structures - Unit 5 Tree PDF
105 pages
Ds 10-Binary Tree
PDF
No ratings yet
Ds 10-Binary Tree
24 pages
Tree and Tree Traversal
PDF
No ratings yet
Tree and Tree Traversal
33 pages
Binary Search Tree
PDF
No ratings yet
Binary Search Tree
85 pages
Chapter 4b. Trees
PDF
No ratings yet
Chapter 4b. Trees
96 pages
Lecture On Tree
PDF
No ratings yet
Lecture On Tree
181 pages
Binary Trees
PDF
No ratings yet
Binary Trees
55 pages
Trees (Part 1)
PDF
No ratings yet
Trees (Part 1)
71 pages
Tree
PDF
No ratings yet
Tree
102 pages
8 23ec60 Data Structures and Algorithms Kir Binary Tree
PDF
No ratings yet
8 23ec60 Data Structures and Algorithms Kir Binary Tree
53 pages
Trees
PDF
No ratings yet
Trees
44 pages
Unit-2 Trees& AVL Tree
PDF
No ratings yet
Unit-2 Trees& AVL Tree
112 pages
Tree PDF
PDF
No ratings yet
Tree PDF
102 pages
Lecture 7 01122022 122605pm 01112023 065713pm
PDF
No ratings yet
Lecture 7 01122022 122605pm 01112023 065713pm
129 pages
Trees
PDF
No ratings yet
Trees
93 pages
TREE
PDF
No ratings yet
TREE
27 pages
Unit 3 - Tree
PDF
No ratings yet
Unit 3 - Tree
169 pages
IT202-DS-Unit 4 - Non-Linear-Data-Structures
PDF
No ratings yet
IT202-DS-Unit 4 - Non-Linear-Data-Structures
110 pages
Chap - 4) Tree
PDF
No ratings yet
Chap - 4) Tree
19 pages
Binary Tree - Expression Tree
PDF
No ratings yet
Binary Tree - Expression Tree
60 pages
Tree Final
PDF
No ratings yet
Tree Final
109 pages
Chapter 4
PDF
No ratings yet
Chapter 4
41 pages
Unit-5 Data Structures
PDF
No ratings yet
Unit-5 Data Structures
63 pages
Unit 4
PDF
No ratings yet
Unit 4
105 pages
Trees
PDF
No ratings yet
Trees
63 pages
Lecture 3 Trees in Data Structure
PDF
No ratings yet
Lecture 3 Trees in Data Structure
30 pages
8.0 Tree Data Structure v2.2 - Notes
PDF
No ratings yet
8.0 Tree Data Structure v2.2 - Notes
35 pages
Ds Unit 5
PDF
No ratings yet
Ds Unit 5
6 pages
Binary Tree Travers Al
PDF
No ratings yet
Binary Tree Travers Al
79 pages
L6 Expression Tree
PDF
No ratings yet
L6 Expression Tree
22 pages
DSA - Trees
PDF
No ratings yet
DSA - Trees
32 pages
Ds Unit 5
PDF
No ratings yet
Ds Unit 5
6 pages
Tree Data Structure
PDF
No ratings yet
Tree Data Structure
72 pages
Data Structure Tree
PDF
No ratings yet
Data Structure Tree
13 pages
Tree Traversals
PDF
No ratings yet
Tree Traversals
7 pages
Trees and Binary Trees
PDF
No ratings yet
Trees and Binary Trees
59 pages
ITCC 104 - Data Structures & Algorithms: Lesson 1: Trees Tree
PDF
No ratings yet
ITCC 104 - Data Structures & Algorithms: Lesson 1: Trees Tree
13 pages
Week 07 Lecture
PDF
No ratings yet
Week 07 Lecture
16 pages
Unit-3: Non-Linear Data Structure
PDF
No ratings yet
Unit-3: Non-Linear Data Structure
40 pages
ADA UNIT 5 1 Trees
PDF
No ratings yet
ADA UNIT 5 1 Trees
7 pages
Trees
PDF
No ratings yet
Trees
5 pages
DS UNIT-V Notes
PDF
No ratings yet
DS UNIT-V Notes
21 pages
Tree Construction Fro Inorder and Preorder
PDF
No ratings yet
Tree Construction Fro Inorder and Preorder
3 pages
Construct A Binary Tree Using Traversals
PDF
No ratings yet
Construct A Binary Tree Using Traversals
5 pages
Data Structures Trees: Vikas Kumar
PDF
No ratings yet
Data Structures Trees: Vikas Kumar
43 pages
Constructing B-Tree From In-Order & Post-Order-1 PDF
PDF
No ratings yet
Constructing B-Tree From In-Order & Post-Order-1 PDF
4 pages
Unit-4 2
PDF
No ratings yet
Unit-4 2
8 pages
Tree Traversal
PDF
No ratings yet
Tree Traversal
3 pages
Binary Tree
PDF
No ratings yet
Binary Tree
14 pages
Paper 2
PDF
No ratings yet
Paper 2
5 pages
Binary Tree Traversal Methods: Welcome To CS221: Programming & Data Structures
PDF
No ratings yet
Binary Tree Traversal Methods: Welcome To CS221: Programming & Data Structures
21 pages
Travesal Methods
PDF
No ratings yet
Travesal Methods
14 pages
Lec 214
PDF
No ratings yet
Lec 214
7 pages
Binary Tree Traversal
PDF
No ratings yet
Binary Tree Traversal
3 pages
Data Structure-64-66
PDF
No ratings yet
Data Structure-64-66
3 pages
Trees
PDF
No ratings yet
Trees
7 pages
Binary Tree Traversal Methods
PDF
No ratings yet
Binary Tree Traversal Methods
5 pages
Construct Tree From Given Inorder and Preorder Traversals
PDF
No ratings yet
Construct Tree From Given Inorder and Preorder Traversals
5 pages
Data Structures For Binary Trees
PDF
No ratings yet
Data Structures For Binary Trees
4 pages