SlideShare a Scribd company logo
1 of 20
Creating a Binary tree from
a GeneralTree
S.DEEPA
Assistant Professor(Sr.G)
Department of ComputerTechnology – PG
Kongu Engineering College
• The rules for converting a general tree to a binary tree are
Rule 1: Root of the binary tree = Root of the general tree
Rule 2: Left child of a node in the binary tree = Leftmost child of the node
in the general tree
Rule 3: Right child of a node in the binary tree = Right sibling of the node in
the general tree
Now let us build the binary tree.
• Step 1: Node A is the root of the general tree, so it will also be
the root of the binary tree.
• Step 2: Left child of node A is the leftmost child of node A in
the general tree and right child of node A is the right sibling of
the node A in the general tree. Since node A has no right
sibling in the general tree, it has no right child in the binary
tree.
• Step 3: Now process node B. Left child of B is E and its right
child is C (right sibling in general tree).
• Step 4: Now process node C. Left child of C is F
(leftmost child) and its right child is D (right
sibling in general tree).
• Step 5: Now process node D. Left child of D is I
(leftmost child). There will be no right child of D
because it has no right sibling in the general
tree.
• Step 6: Now process node I. There will be no left
child of I in the binary tree because I has no left
child in the general tree. However, I has a right
sibling J, so it will be added as the right child of I.
• Step 7: Now process node J. Left child of J is K
(leftmost child). There will be no right child of J
because it has no right sibling in the general
tree.
• Step 8: Now process all the unprocessed nodes
(E, F, G, H, K) in the same fashion, so the
resultant binary tree can be given as follows.
TRAVERSING A BINARYTREE
• Traversing a binary tree is the process of visiting each node in the tree
exactly once in a systematic way.
• Unlike linear data structures in which the elements are traversed
sequentially, tree is a nonlinear data structure in which the elements can be
traversed in many different ways.
• There are different algorithms for tree traversals. These algorithms differ in
the order in which the nodes are visited.
Pre-orderTraversal
• To traverse a non-empty binary tree in pre-order, the following operations are
performed recursively at each node.The algorithm works by:
1.Visiting the root node,
2.Traversing the left sub-tree, and finally
3.Traversing the right sub-tree.
• Consider the tree given in Fig. 9.15. The pre-order traversal of the tree is given as A,
B, C.
• Root node first, the left sub-tree next, and then the right sub-tree. Pre-order
traversal is also called as depth-first traversal.
• In this algorithm, the left sub-tree is always traversed before the right sub-tree.
• The word ‘pre’ in the pre-order specifies that the root node is accessed prior to any
other nodes in the left and right sub-trees.
• Pre-order algorithm is also known as the NLR traversal algorithm (Node-Left-
Right).
• Pre-order traversal algorithms are used to extract a prefix notation from an
expression tree.
In-orderTraversal
• To traverse a non-empty binary tree in in-order, the following operations are
performed recursively at each node.
• The algorithm works by:
1.Traversing the left sub-tree,
2.Visiting the root node, and finally
3.Traversing the right sub-tree.
• Consider the tree given in Fig. 9.15.The in-order traversal of the tree is given as B,
A, and C.
• Left sub-tree first, the root node next, and then the right sub-tree. In-order
traversal is also called as symmetric traversal.
• In this algorithm, the left sub-tree is always traversed before the root node and the
right sub-tree.
• The word ‘in’ in the in-order specifies that the root node is accessed in between the
left and the right sub-trees. In-order algorithm is also known as the LNR traversal
algorithm (Left-Node-Right).
• In-order traversal algorithm is usually used to display the elements of a
binary search tree.
• Here, all the elements with a value lower than a given value are accessed
before the elements with a higher value.
Post-orderTraversal
• To traverse a non-empty binary tree in post-order, the following operations are performed recursively at each
node.The algorithm works by:
1.Traversing the left sub-tree,
2.Traversing the right sub-tree, and finally
3.Visiting the root node.
• Consider the tree given in Fig. 9.18.The post-order traversal of the tree is given as B, C andA.
• Left sub-tree first, the right sub-tree next, and finally the root node.
• In this algorithm, the left sub-tree is always traversed before the right sub-tree and the root node.
• The word ‘post’ in the post-order specifies that the root node is accessed after the left and the right sub-trees.
• Post-order algorithm is also known as the LRN traversal algorithm (Left-Right-Node).
• Post-order traversals are used to extract postfix notation from an expression tree.
Level-orderTraversal
• In level-order traversal, all the nodes at a level are accessed before going to
the next level.
• This algorithm is also called as the breadth-first traversal algorithm.
Constructing a BinaryTree fromTraversal
Results
• We can construct a binary tree if we are given at least two traversal results.
• The first traversal must be the in-order traversal and the second can be either pre-
order or post-order traversal.
• The in-order traversal result will be used to determine the left and the right child
nodes, and the pre-order/post-order can be used to determine the root node.
• For example, consider the traversal results given below:
• In–orderTraversal: D B E A F C G Pre–orderTraversal: A B D E C F G
• Here, we have the in-order traversal sequence and pre-order traversal sequence.
Steps to construct tree
• Step 1 Use the pre-order sequence to determine the root node of the tree.
The first element would be the root node.
• Step 2 Elements on the left side of the root node in the in-order traversal
sequence form the left sub-tree of the root node. Similarly, elements on the
right side of the root node in the in-order traversal sequence form the right
sub-tree of the root node.
• Step 3 Recursively select each element from pre-order traversal sequence
and create its left and right sub-trees from the in-order traversal sequence.
• Look at below image which constructs the tree from its traversal results.
• Now consider the in-order traversal and post-order traversal sequences of a given binary
tree.
• Before constructing the binary tree, remember that in post-order traversal the root node is
the last node.
Creating a Binary tree from a General Tree.pptx

More Related Content

What's hot (20)

Binary Search Tree in Data Structure
Binary Search Tree in Data StructureBinary Search Tree in Data Structure
Binary Search Tree in Data Structure
 
trees in data structure
trees in data structure trees in data structure
trees in data structure
 
introduction to_trees
introduction to_treesintroduction to_trees
introduction to_trees
 
Red black tree
Red black treeRed black tree
Red black tree
 
Data Structure: TREES
Data Structure: TREESData Structure: TREES
Data Structure: TREES
 
Expression trees
Expression treesExpression trees
Expression trees
 
Threaded Binary Tree.pptx
Threaded Binary Tree.pptxThreaded Binary Tree.pptx
Threaded Binary Tree.pptx
 
Data Structure (Tree)
Data Structure (Tree)Data Structure (Tree)
Data Structure (Tree)
 
AVL Tree
AVL TreeAVL Tree
AVL Tree
 
AVL tree ( Balanced Binary Search Tree)-Data Structure
AVL tree ( Balanced Binary Search Tree)-Data StructureAVL tree ( Balanced Binary Search Tree)-Data Structure
AVL tree ( Balanced Binary Search Tree)-Data Structure
 
Binary tree
Binary  treeBinary  tree
Binary tree
 
Binary Search Tree
Binary Search TreeBinary Search Tree
Binary Search Tree
 
Tree Traversal
Tree TraversalTree Traversal
Tree Traversal
 
Red black trees
Red black treesRed black trees
Red black trees
 
Shift reduce parser
Shift reduce parserShift reduce parser
Shift reduce parser
 
Graph traversals in Data Structures
Graph traversals in Data StructuresGraph traversals in Data Structures
Graph traversals in Data Structures
 
Threaded binary tree
Threaded binary treeThreaded binary tree
Threaded binary tree
 
Binary Tree Traversal
Binary Tree TraversalBinary Tree Traversal
Binary Tree Traversal
 
AVL Tree
AVL TreeAVL Tree
AVL Tree
 
AVL Tree in Data Structure
AVL Tree in Data Structure AVL Tree in Data Structure
AVL Tree in Data Structure
 

Similar to Creating a Binary tree from a General Tree.pptx

Similar to Creating a Binary tree from a General Tree.pptx (20)

Binary tree
Binary treeBinary tree
Binary tree
 
Tree
TreeTree
Tree
 
Binary search tree
Binary search treeBinary search tree
Binary search tree
 
Tree
TreeTree
Tree
 
Binary tree
Binary treeBinary tree
Binary tree
 
Tree traversal techniques
Tree traversal  techniquesTree traversal  techniques
Tree traversal techniques
 
Tree traversal techniques
Tree traversal techniquesTree traversal techniques
Tree traversal techniques
 
C++ UNIT4.pptx
C++ UNIT4.pptxC++ UNIT4.pptx
C++ UNIT4.pptx
 
Data Structures 5
Data Structures 5Data Structures 5
Data Structures 5
 
Search tree,Tree and binary tree and heap tree
Search tree,Tree  and binary tree and heap treeSearch tree,Tree  and binary tree and heap tree
Search tree,Tree and binary tree and heap tree
 
Trees
TreesTrees
Trees
 
BINARY SEARCH TREE
BINARY SEARCH TREEBINARY SEARCH TREE
BINARY SEARCH TREE
 
Trees in data structure
Trees in data structureTrees in data structure
Trees in data structure
 
Unit 3 trees
Unit 3   treesUnit 3   trees
Unit 3 trees
 
Advanced c c++
Advanced c c++Advanced c c++
Advanced c c++
 
lecture10 date structure types of graph and terminology
lecture10 date structure types of graph and terminologylecture10 date structure types of graph and terminology
lecture10 date structure types of graph and terminology
 
358 33 powerpoint-slides_11-efficient-binary-trees_chapter-11
358 33 powerpoint-slides_11-efficient-binary-trees_chapter-11358 33 powerpoint-slides_11-efficient-binary-trees_chapter-11
358 33 powerpoint-slides_11-efficient-binary-trees_chapter-11
 
tree-160731205832.pptx
tree-160731205832.pptxtree-160731205832.pptx
tree-160731205832.pptx
 
Binary Search Tree
Binary Search TreeBinary Search Tree
Binary Search Tree
 
Queues
QueuesQueues
Queues
 

More from DeepaThirumurugan

More from DeepaThirumurugan (6)

APACHE SPARK.pptx
APACHE SPARK.pptxAPACHE SPARK.pptx
APACHE SPARK.pptx
 
IO organization.ppt
IO organization.pptIO organization.ppt
IO organization.ppt
 
Difference between traditional and agile software development
Difference between traditional and agile software developmentDifference between traditional and agile software development
Difference between traditional and agile software development
 
Agile Development Models
Agile Development ModelsAgile Development Models
Agile Development Models
 
Cloud Computing
Cloud Computing Cloud Computing
Cloud Computing
 
Data structures - Introduction
Data structures - IntroductionData structures - Introduction
Data structures - Introduction
 

Recently uploaded

Science&tech:THE INFORMATION AGE STS.pdf
Science&tech:THE INFORMATION AGE STS.pdfScience&tech:THE INFORMATION AGE STS.pdf
Science&tech:THE INFORMATION AGE STS.pdfjimielynbastida
 
Snow Chain-Integrated Tire for a Safe Drive on Winter Roads
Snow Chain-Integrated Tire for a Safe Drive on Winter RoadsSnow Chain-Integrated Tire for a Safe Drive on Winter Roads
Snow Chain-Integrated Tire for a Safe Drive on Winter RoadsHyundai Motor Group
 
Artificial intelligence in the post-deep learning era
Artificial intelligence in the post-deep learning eraArtificial intelligence in the post-deep learning era
Artificial intelligence in the post-deep learning eraDeakin University
 
Designing IA for AI - Information Architecture Conference 2024
Designing IA for AI - Information Architecture Conference 2024Designing IA for AI - Information Architecture Conference 2024
Designing IA for AI - Information Architecture Conference 2024Enterprise Knowledge
 
CloudStudio User manual (basic edition):
CloudStudio User manual (basic edition):CloudStudio User manual (basic edition):
CloudStudio User manual (basic edition):comworks
 
Unblocking The Main Thread Solving ANRs and Frozen Frames
Unblocking The Main Thread Solving ANRs and Frozen FramesUnblocking The Main Thread Solving ANRs and Frozen Frames
Unblocking The Main Thread Solving ANRs and Frozen FramesSinan KOZAK
 
AI as an Interface for Commercial Buildings
AI as an Interface for Commercial BuildingsAI as an Interface for Commercial Buildings
AI as an Interface for Commercial BuildingsMemoori
 
Pigging Solutions in Pet Food Manufacturing
Pigging Solutions in Pet Food ManufacturingPigging Solutions in Pet Food Manufacturing
Pigging Solutions in Pet Food ManufacturingPigging Solutions
 
Key Features Of Token Development (1).pptx
Key  Features Of Token  Development (1).pptxKey  Features Of Token  Development (1).pptx
Key Features Of Token Development (1).pptxLBM Solutions
 
Build your next Gen AI Breakthrough - April 2024
Build your next Gen AI Breakthrough - April 2024Build your next Gen AI Breakthrough - April 2024
Build your next Gen AI Breakthrough - April 2024Neo4j
 
Swan(sea) Song – personal research during my six years at Swansea ... and bey...
Swan(sea) Song – personal research during my six years at Swansea ... and bey...Swan(sea) Song – personal research during my six years at Swansea ... and bey...
Swan(sea) Song – personal research during my six years at Swansea ... and bey...Alan Dix
 
Unleash Your Potential - Namagunga Girls Coding Club
Unleash Your Potential - Namagunga Girls Coding ClubUnleash Your Potential - Namagunga Girls Coding Club
Unleash Your Potential - Namagunga Girls Coding ClubKalema Edgar
 
Pigging Solutions Piggable Sweeping Elbows
Pigging Solutions Piggable Sweeping ElbowsPigging Solutions Piggable Sweeping Elbows
Pigging Solutions Piggable Sweeping ElbowsPigging Solutions
 
Transcript: New from BookNet Canada for 2024: BNC BiblioShare - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: BNC BiblioShare - Tech Forum 2024Transcript: New from BookNet Canada for 2024: BNC BiblioShare - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: BNC BiblioShare - Tech Forum 2024BookNet Canada
 
Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmatics
Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmaticsKotlin Multiplatform & Compose Multiplatform - Starter kit for pragmatics
Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmaticscarlostorres15106
 
Transcript: #StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
Transcript: #StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024Transcript: #StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
Transcript: #StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024BookNet Canada
 
SIEMENS: RAPUNZEL – A Tale About Knowledge Graph
SIEMENS: RAPUNZEL – A Tale About Knowledge GraphSIEMENS: RAPUNZEL – A Tale About Knowledge Graph
SIEMENS: RAPUNZEL – A Tale About Knowledge GraphNeo4j
 
Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...
Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...
Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...shyamraj55
 

Recently uploaded (20)

Science&tech:THE INFORMATION AGE STS.pdf
Science&tech:THE INFORMATION AGE STS.pdfScience&tech:THE INFORMATION AGE STS.pdf
Science&tech:THE INFORMATION AGE STS.pdf
 
DMCC Future of Trade Web3 - Special Edition
DMCC Future of Trade Web3 - Special EditionDMCC Future of Trade Web3 - Special Edition
DMCC Future of Trade Web3 - Special Edition
 
Snow Chain-Integrated Tire for a Safe Drive on Winter Roads
Snow Chain-Integrated Tire for a Safe Drive on Winter RoadsSnow Chain-Integrated Tire for a Safe Drive on Winter Roads
Snow Chain-Integrated Tire for a Safe Drive on Winter Roads
 
Artificial intelligence in the post-deep learning era
Artificial intelligence in the post-deep learning eraArtificial intelligence in the post-deep learning era
Artificial intelligence in the post-deep learning era
 
Designing IA for AI - Information Architecture Conference 2024
Designing IA for AI - Information Architecture Conference 2024Designing IA for AI - Information Architecture Conference 2024
Designing IA for AI - Information Architecture Conference 2024
 
CloudStudio User manual (basic edition):
CloudStudio User manual (basic edition):CloudStudio User manual (basic edition):
CloudStudio User manual (basic edition):
 
The transition to renewables in India.pdf
The transition to renewables in India.pdfThe transition to renewables in India.pdf
The transition to renewables in India.pdf
 
Unblocking The Main Thread Solving ANRs and Frozen Frames
Unblocking The Main Thread Solving ANRs and Frozen FramesUnblocking The Main Thread Solving ANRs and Frozen Frames
Unblocking The Main Thread Solving ANRs and Frozen Frames
 
AI as an Interface for Commercial Buildings
AI as an Interface for Commercial BuildingsAI as an Interface for Commercial Buildings
AI as an Interface for Commercial Buildings
 
Pigging Solutions in Pet Food Manufacturing
Pigging Solutions in Pet Food ManufacturingPigging Solutions in Pet Food Manufacturing
Pigging Solutions in Pet Food Manufacturing
 
Key Features Of Token Development (1).pptx
Key  Features Of Token  Development (1).pptxKey  Features Of Token  Development (1).pptx
Key Features Of Token Development (1).pptx
 
Build your next Gen AI Breakthrough - April 2024
Build your next Gen AI Breakthrough - April 2024Build your next Gen AI Breakthrough - April 2024
Build your next Gen AI Breakthrough - April 2024
 
Swan(sea) Song – personal research during my six years at Swansea ... and bey...
Swan(sea) Song – personal research during my six years at Swansea ... and bey...Swan(sea) Song – personal research during my six years at Swansea ... and bey...
Swan(sea) Song – personal research during my six years at Swansea ... and bey...
 
Unleash Your Potential - Namagunga Girls Coding Club
Unleash Your Potential - Namagunga Girls Coding ClubUnleash Your Potential - Namagunga Girls Coding Club
Unleash Your Potential - Namagunga Girls Coding Club
 
Pigging Solutions Piggable Sweeping Elbows
Pigging Solutions Piggable Sweeping ElbowsPigging Solutions Piggable Sweeping Elbows
Pigging Solutions Piggable Sweeping Elbows
 
Transcript: New from BookNet Canada for 2024: BNC BiblioShare - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: BNC BiblioShare - Tech Forum 2024Transcript: New from BookNet Canada for 2024: BNC BiblioShare - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: BNC BiblioShare - Tech Forum 2024
 
Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmatics
Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmaticsKotlin Multiplatform & Compose Multiplatform - Starter kit for pragmatics
Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmatics
 
Transcript: #StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
Transcript: #StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024Transcript: #StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
Transcript: #StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
 
SIEMENS: RAPUNZEL – A Tale About Knowledge Graph
SIEMENS: RAPUNZEL – A Tale About Knowledge GraphSIEMENS: RAPUNZEL – A Tale About Knowledge Graph
SIEMENS: RAPUNZEL – A Tale About Knowledge Graph
 
Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...
Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...
Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...
 

Creating a Binary tree from a General Tree.pptx

  • 1. Creating a Binary tree from a GeneralTree S.DEEPA Assistant Professor(Sr.G) Department of ComputerTechnology – PG Kongu Engineering College
  • 2. • The rules for converting a general tree to a binary tree are Rule 1: Root of the binary tree = Root of the general tree Rule 2: Left child of a node in the binary tree = Leftmost child of the node in the general tree Rule 3: Right child of a node in the binary tree = Right sibling of the node in the general tree
  • 3. Now let us build the binary tree. • Step 1: Node A is the root of the general tree, so it will also be the root of the binary tree. • Step 2: Left child of node A is the leftmost child of node A in the general tree and right child of node A is the right sibling of the node A in the general tree. Since node A has no right sibling in the general tree, it has no right child in the binary tree. • Step 3: Now process node B. Left child of B is E and its right child is C (right sibling in general tree).
  • 4. • Step 4: Now process node C. Left child of C is F (leftmost child) and its right child is D (right sibling in general tree). • Step 5: Now process node D. Left child of D is I (leftmost child). There will be no right child of D because it has no right sibling in the general tree. • Step 6: Now process node I. There will be no left child of I in the binary tree because I has no left child in the general tree. However, I has a right sibling J, so it will be added as the right child of I.
  • 5. • Step 7: Now process node J. Left child of J is K (leftmost child). There will be no right child of J because it has no right sibling in the general tree. • Step 8: Now process all the unprocessed nodes (E, F, G, H, K) in the same fashion, so the resultant binary tree can be given as follows.
  • 6. TRAVERSING A BINARYTREE • Traversing a binary tree is the process of visiting each node in the tree exactly once in a systematic way. • Unlike linear data structures in which the elements are traversed sequentially, tree is a nonlinear data structure in which the elements can be traversed in many different ways. • There are different algorithms for tree traversals. These algorithms differ in the order in which the nodes are visited.
  • 7. Pre-orderTraversal • To traverse a non-empty binary tree in pre-order, the following operations are performed recursively at each node.The algorithm works by: 1.Visiting the root node, 2.Traversing the left sub-tree, and finally 3.Traversing the right sub-tree. • Consider the tree given in Fig. 9.15. The pre-order traversal of the tree is given as A, B, C. • Root node first, the left sub-tree next, and then the right sub-tree. Pre-order traversal is also called as depth-first traversal.
  • 8. • In this algorithm, the left sub-tree is always traversed before the right sub-tree. • The word ‘pre’ in the pre-order specifies that the root node is accessed prior to any other nodes in the left and right sub-trees. • Pre-order algorithm is also known as the NLR traversal algorithm (Node-Left- Right). • Pre-order traversal algorithms are used to extract a prefix notation from an expression tree.
  • 9.
  • 10.
  • 11. In-orderTraversal • To traverse a non-empty binary tree in in-order, the following operations are performed recursively at each node. • The algorithm works by: 1.Traversing the left sub-tree, 2.Visiting the root node, and finally 3.Traversing the right sub-tree. • Consider the tree given in Fig. 9.15.The in-order traversal of the tree is given as B, A, and C. • Left sub-tree first, the root node next, and then the right sub-tree. In-order traversal is also called as symmetric traversal. • In this algorithm, the left sub-tree is always traversed before the root node and the right sub-tree. • The word ‘in’ in the in-order specifies that the root node is accessed in between the left and the right sub-trees. In-order algorithm is also known as the LNR traversal algorithm (Left-Node-Right).
  • 12. • In-order traversal algorithm is usually used to display the elements of a binary search tree. • Here, all the elements with a value lower than a given value are accessed before the elements with a higher value.
  • 13.
  • 14. Post-orderTraversal • To traverse a non-empty binary tree in post-order, the following operations are performed recursively at each node.The algorithm works by: 1.Traversing the left sub-tree, 2.Traversing the right sub-tree, and finally 3.Visiting the root node. • Consider the tree given in Fig. 9.18.The post-order traversal of the tree is given as B, C andA. • Left sub-tree first, the right sub-tree next, and finally the root node. • In this algorithm, the left sub-tree is always traversed before the right sub-tree and the root node. • The word ‘post’ in the post-order specifies that the root node is accessed after the left and the right sub-trees. • Post-order algorithm is also known as the LRN traversal algorithm (Left-Right-Node). • Post-order traversals are used to extract postfix notation from an expression tree.
  • 15.
  • 16. Level-orderTraversal • In level-order traversal, all the nodes at a level are accessed before going to the next level. • This algorithm is also called as the breadth-first traversal algorithm.
  • 17. Constructing a BinaryTree fromTraversal Results • We can construct a binary tree if we are given at least two traversal results. • The first traversal must be the in-order traversal and the second can be either pre- order or post-order traversal. • The in-order traversal result will be used to determine the left and the right child nodes, and the pre-order/post-order can be used to determine the root node. • For example, consider the traversal results given below: • In–orderTraversal: D B E A F C G Pre–orderTraversal: A B D E C F G • Here, we have the in-order traversal sequence and pre-order traversal sequence.
  • 18. Steps to construct tree • Step 1 Use the pre-order sequence to determine the root node of the tree. The first element would be the root node. • Step 2 Elements on the left side of the root node in the in-order traversal sequence form the left sub-tree of the root node. Similarly, elements on the right side of the root node in the in-order traversal sequence form the right sub-tree of the root node. • Step 3 Recursively select each element from pre-order traversal sequence and create its left and right sub-trees from the in-order traversal sequence.
  • 19. • Look at below image which constructs the tree from its traversal results. • Now consider the in-order traversal and post-order traversal sequences of a given binary tree. • Before constructing the binary tree, remember that in post-order traversal the root node is the last node.