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

What's hot (10)

PPTX
Theory of Computation Unit 1
Jena Catherine Bel D
 
PPTX
Analysis of algorithms
iqbalphy1
 
PPTX
Trees data structure
Sumit Gupta
 
PPTX
3.4 deterministic pda
Sampath Kumar S
 
PPTX
data structures- back tracking
Abinaya B
 
PPTX
Lecture 21 problem reduction search ao star search
Hema Kashyap
 
PPTX
Joins & constraints
VENNILAV6
 
PPTX
Technical aptitude Test 1 CSE
Sujata Regoti
 
PDF
Tableau file types
Learnbay Datascience
 
Theory of Computation Unit 1
Jena Catherine Bel D
 
Analysis of algorithms
iqbalphy1
 
Trees data structure
Sumit Gupta
 
3.4 deterministic pda
Sampath Kumar S
 
data structures- back tracking
Abinaya B
 
Lecture 21 problem reduction search ao star search
Hema Kashyap
 
Joins & constraints
VENNILAV6
 
Technical aptitude Test 1 CSE
Sujata Regoti
 
Tableau file types
Learnbay Datascience
 

Similar to Lecture 8 tree traversal (12)

PPT
Lec21
Nikhil Chilwant
 
PPTX
Unit 1 L7 L8 Binary Tree Construction using sequence.pptx
ratnapatil14
 
PPTX
Binary Tree Traversal
Dhrumil Panchal
 
PDF
Binary Trees, Traversals and BST.pdf....
snamya20
 
PDF
Tree and binary tree
Zaid Shabbir
 
PPTX
Weak 13 Trees, BST update.pptxhjjujjjhhhy
baloch4551701
 
PDF
Threaded binarytree&heapsort
Ssankett Negi
 
PPT
Data structures algoritghm for binary tree method.ppt
Johny139575
 
PPT
binary tree power point presentation for iT
DanilynSukkie
 
PPT
Unit8 C
arnold 7490
 
PPT
BINARY SEARCH TREE
ER Punit Jain
 
Unit 1 L7 L8 Binary Tree Construction using sequence.pptx
ratnapatil14
 
Binary Tree Traversal
Dhrumil Panchal
 
Binary Trees, Traversals and BST.pdf....
snamya20
 
Tree and binary tree
Zaid Shabbir
 
Weak 13 Trees, BST update.pptxhjjujjjhhhy
baloch4551701
 
Threaded binarytree&heapsort
Ssankett Negi
 
Data structures algoritghm for binary tree method.ppt
Johny139575
 
binary tree power point presentation for iT
DanilynSukkie
 
Unit8 C
arnold 7490
 
BINARY SEARCH TREE
ER Punit Jain
 
Ad

More from Abirami A (12)

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

Recently uploaded (20)

PDF
IoT - Unit 2 (Internet of Things-Concepts) - PPT.pdf
dipakraut82
 
PPTX
MobileComputingMANET2023 MobileComputingMANET2023.pptx
masterfake98765
 
PDF
monopile foundation seminar topic for civil engineering students
Ahina5
 
PDF
PORTFOLIO Golam Kibria Khan — architect with a passion for thoughtful design...
MasumKhan59
 
PDF
International Journal of Information Technology Convergence and services (IJI...
ijitcsjournal4
 
PDF
Water Design_Manual_2005. KENYA FOR WASTER SUPPLY AND SEWERAGE
DancanNgutuku
 
PPT
Oxygen Co2 Transport in the Lungs(Exchange og gases)
SUNDERLINSHIBUD
 
PPTX
Benefits_^0_Challigi😙🏡💐8fenges[1].pptx
akghostmaker
 
PPTX
Innowell Capability B0425 - Commercial Buildings.pptx
regobertroza
 
PPTX
NEUROMOROPHIC nu iajwojeieheueueueu.pptx
knkoodalingam39
 
PPTX
REINFORCEMENT AS CONSTRUCTION MATERIALS.pptx
mohaiminulhaquesami
 
PDF
A presentation on the Urban Heat Island Effect
studyfor7hrs
 
PPTX
Heart Bleed Bug - A case study (Course: Cryptography and Network Security)
Adri Jovin
 
PDF
Statistical Data Analysis Using SPSS Software
shrikrishna kesharwani
 
PDF
BioSensors glucose monitoring, cholestrol
nabeehasahar1
 
DOCX
8th International Conference on Electrical Engineering (ELEN 2025)
elelijjournal653
 
PDF
POWER PLANT ENGINEERING (R17A0326).pdf..
haneefachosa123
 
PDF
MOBILE AND WEB BASED REMOTE BUSINESS MONITORING SYSTEM
ijait
 
PDF
Set Relation Function Practice session 24.05.2025.pdf
DrStephenStrange4
 
PPTX
ISO/IEC JTC 1/WG 9 (MAR) Convenor Report
Kurata Takeshi
 
IoT - Unit 2 (Internet of Things-Concepts) - PPT.pdf
dipakraut82
 
MobileComputingMANET2023 MobileComputingMANET2023.pptx
masterfake98765
 
monopile foundation seminar topic for civil engineering students
Ahina5
 
PORTFOLIO Golam Kibria Khan — architect with a passion for thoughtful design...
MasumKhan59
 
International Journal of Information Technology Convergence and services (IJI...
ijitcsjournal4
 
Water Design_Manual_2005. KENYA FOR WASTER SUPPLY AND SEWERAGE
DancanNgutuku
 
Oxygen Co2 Transport in the Lungs(Exchange og gases)
SUNDERLINSHIBUD
 
Benefits_^0_Challigi😙🏡💐8fenges[1].pptx
akghostmaker
 
Innowell Capability B0425 - Commercial Buildings.pptx
regobertroza
 
NEUROMOROPHIC nu iajwojeieheueueueu.pptx
knkoodalingam39
 
REINFORCEMENT AS CONSTRUCTION MATERIALS.pptx
mohaiminulhaquesami
 
A presentation on the Urban Heat Island Effect
studyfor7hrs
 
Heart Bleed Bug - A case study (Course: Cryptography and Network Security)
Adri Jovin
 
Statistical Data Analysis Using SPSS Software
shrikrishna kesharwani
 
BioSensors glucose monitoring, cholestrol
nabeehasahar1
 
8th International Conference on Electrical Engineering (ELEN 2025)
elelijjournal653
 
POWER PLANT ENGINEERING (R17A0326).pdf..
haneefachosa123
 
MOBILE AND WEB BASED REMOTE BUSINESS MONITORING SYSTEM
ijait
 
Set Relation Function Practice session 24.05.2025.pdf
DrStephenStrange4
 
ISO/IEC JTC 1/WG 9 (MAR) Convenor Report
Kurata Takeshi
 

Lecture 8 tree traversal