SlideShare a Scribd company logo
Lecture 8
Tree Traversal
Abirami Sivaprasad
Binary Tree Traversal Methods
Preorder
Inorder
Postorder
Preorder Traversal
void preOrder(treePointer ptr)
{
if (ptr != NULL)
{
visit(t);
preOrder(ptr->leftChild);
preOrder(ptr->rightChild);
}
}
Preorder Example (Visit = print)
a
b c
a b c
Preorder Example (Visit = print)
a
b c
d e
f
g h i j
a b d g h e i c f j
Preorder Of Expression Tree
+
a b
-
c d
+
e f
*
/
Gives prefix form of expression!
/ * + a b - c d + e f
Inorder Traversal
void inOrder(treePointer ptr)
{
if (ptr != NULLL)
{
inOrder(ptr->leftChild);
visit(ptr);
inOrder(ptr->rightChild);
}
}
Inorder Example (Visit = print)
a
b c
b a c
Inorder Example (Visit = print)
a
b c
d e
f
g h i j
g d h b e i a f j c
Inorder By Projection (Squishing)
a
b c
d e
f
g h i j
g d h b e i a f j c
Inorder Of Expression Tree
+
a b
-
c d
+
e f
*
/
Gives infix form of expression
ea + b * c d / + f-
Postorder Traversal
void postOrder(treePointer ptr)
{
if (ptr != NULL)
{
postOrder(ptr->leftChild);
postOrder(ptr->rightChild);
visit(t);
}
}
Postorder Example (Visit = print)
a
b c
b c a
Postorder Example (Visit = print)
a
b c
d e
f
g h i j
g h d i e b j f c a
Postorder Of Expression Tree
+
a b
-
c d
+
e f
*
/
Gives postfix form of expression!
a b + c d - * e f + /
Binary Tree Construction
Suppose that the elements in a binary tree
are distinct.
Can you construct the binary tree from
which a given traversal sequence came?
When a traversal sequence has more than
one element, the binary tree is not
uniquely defined.
Therefore, the tree from which the
sequence was obtained cannot be
reconstructed uniquely.
Binary Tree Construction
Can you construct the binary tree,
given two traversal sequences?
Depends on which two sequences are
given.
Inorder And Preorder
• inorder = g d h b e i a f j c
• preorder = a b d g h e i c f j
• Scan the preorder left to right using the
inorder to separate left and right subtrees.
• a is the root of the tree; gdhbei are in the
left subtree; fjc are in the right subtree.
a
gdhbei fjc
Inorder And Preorder
• preorder = a b d g h e i c f j
• b is the next root; gdh are in the left
subtree; ei are in the right subtree.
a
gdhbei fjc
a
gdh
fjcb
ei
Inorder And Preorder
• preorder = a b d g h e i c f j
• d is the next root; g is in the left
subtree; h is in the right subtree.
a
gdh
fjcb
ei
a
g
fjcb
eid
h
Inorder And Postorder
• Scan postorder from right to left using
inorder to separate left and right subtrees.
• inorder = g d h b e i a f j c
• postorder = g h d i e b j f c a
• Tree root is a; gdhbei are in left subtree; fjc
are in right subtree.
Thank U

More Related Content

PDF
Molik Adhikar (1).pdf
PPT
PPTX
Unit 1 L7 L8 Binary Tree Construction using sequence.pptx
PPTX
Binary Tree Traversal
PPTX
Creating a Binary tree from a General Tree.pptx
DOCX
DS UNIT5_BINARY TREES.docx
PPT
Algorithm and Data Structure - Binary Trees
Molik Adhikar (1).pdf
Unit 1 L7 L8 Binary Tree Construction using sequence.pptx
Binary Tree Traversal
Creating a Binary tree from a General Tree.pptx
DS UNIT5_BINARY TREES.docx
Algorithm and Data Structure - Binary Trees

Similar to Lecture 8 tree traversal (20)

PPT
Data Structure And Algorithms for Computer Science
PPTX
Data structure
PDF
Binary Trees
PPT
Trees
PPTX
learn tree, linked list, queue, stack, and other algo
PPTX
Tree Traversals in Binary Tree for Beginner in Computer Science
PPTX
Tree structure and its definitions with an example
PPTX
Binary tree
PDF
Lecture notes data structures tree
PPTX
Presentation tree traversal
PPTX
binary tree.pptx
PPTX
Data Structure in Tree form. .pptx
PPTX
lecture 17,18. .pptx
PPTX
lecture 17,18. .pptx
PPT
Binary Search Tree Traversal.ppt
PPT
Binary Trees.ppt
PPT
Data Structure and Algorithms Binary Tree
PPTX
Introduction-to-Binary-Tree-Traversal.pptx
Data Structure And Algorithms for Computer Science
Data structure
Binary Trees
Trees
learn tree, linked list, queue, stack, and other algo
Tree Traversals in Binary Tree for Beginner in Computer Science
Tree structure and its definitions with an example
Binary tree
Lecture notes data structures tree
Presentation tree traversal
binary tree.pptx
Data Structure in Tree form. .pptx
lecture 17,18. .pptx
lecture 17,18. .pptx
Binary Search Tree Traversal.ppt
Binary Trees.ppt
Data Structure and Algorithms Binary Tree
Introduction-to-Binary-Tree-Traversal.pptx
Ad

More from Abirami A (12)

PPTX
Lecture 3 time complexity
PPTX
Lecture 2 Data Structure Introduction
PPTX
Lecture 1 introduction
PPTX
Lecture 14 splay tree
PPTX
Lecture 16 graphs traversal
PPTX
Lecture 16 graph introduction
PPTX
Lecture 6 disjoint set
PPTX
Lecture 9 b tree
PPTX
Lecture 7 bst
PPTX
Lecture 6 tree traversal
PPT
Lecture 5 tree.pptx
PPTX
Lecture 1 sorting insertion & shell sort
Lecture 3 time complexity
Lecture 2 Data Structure Introduction
Lecture 1 introduction
Lecture 14 splay tree
Lecture 16 graphs traversal
Lecture 16 graph introduction
Lecture 6 disjoint set
Lecture 9 b tree
Lecture 7 bst
Lecture 6 tree traversal
Lecture 5 tree.pptx
Lecture 1 sorting insertion & shell sort
Ad

Recently uploaded (20)

PDF
Enhancing Cyber Defense Against Zero-Day Attacks using Ensemble Neural Networks
PPT
Mechanical Engineering MATERIALS Selection
PPTX
Construction Project Organization Group 2.pptx
PPTX
Lecture Notes Electrical Wiring System Components
PPTX
OOP with Java - Java Introduction (Basics)
PPTX
CH1 Production IntroductoryConcepts.pptx
PDF
TFEC-4-2020-Design-Guide-for-Timber-Roof-Trusses.pdf
PPT
Introduction, IoT Design Methodology, Case Study on IoT System for Weather Mo...
PDF
Evaluating the Democratization of the Turkish Armed Forces from a Normative P...
PDF
Well-logging-methods_new................
PDF
Mitigating Risks through Effective Management for Enhancing Organizational Pe...
PPTX
Foundation to blockchain - A guide to Blockchain Tech
PPTX
CYBER-CRIMES AND SECURITY A guide to understanding
PPTX
Current and future trends in Computer Vision.pptx
PDF
Unit I ESSENTIAL OF DIGITAL MARKETING.pdf
PDF
Automation-in-Manufacturing-Chapter-Introduction.pdf
PDF
Model Code of Practice - Construction Work - 21102022 .pdf
PPTX
Geodesy 1.pptx...............................................
PDF
SM_6th-Sem__Cse_Internet-of-Things.pdf IOT
PDF
July 2025 - Top 10 Read Articles in International Journal of Software Enginee...
Enhancing Cyber Defense Against Zero-Day Attacks using Ensemble Neural Networks
Mechanical Engineering MATERIALS Selection
Construction Project Organization Group 2.pptx
Lecture Notes Electrical Wiring System Components
OOP with Java - Java Introduction (Basics)
CH1 Production IntroductoryConcepts.pptx
TFEC-4-2020-Design-Guide-for-Timber-Roof-Trusses.pdf
Introduction, IoT Design Methodology, Case Study on IoT System for Weather Mo...
Evaluating the Democratization of the Turkish Armed Forces from a Normative P...
Well-logging-methods_new................
Mitigating Risks through Effective Management for Enhancing Organizational Pe...
Foundation to blockchain - A guide to Blockchain Tech
CYBER-CRIMES AND SECURITY A guide to understanding
Current and future trends in Computer Vision.pptx
Unit I ESSENTIAL OF DIGITAL MARKETING.pdf
Automation-in-Manufacturing-Chapter-Introduction.pdf
Model Code of Practice - Construction Work - 21102022 .pdf
Geodesy 1.pptx...............................................
SM_6th-Sem__Cse_Internet-of-Things.pdf IOT
July 2025 - Top 10 Read Articles in International Journal of Software Enginee...

Lecture 8 tree traversal