SlideShare a Scribd company logo
1 of 24
1
Md. Shamim Alamgir
Dept. SWE
Batch:19th
Topics: Tree Traversal
2
Tree Anatomy
R
S
Y Z
X
T
U V W
Root
Internal Node
Leaf
Subtree
Level 0
Level 1
Level 2
Level 3
Child of X
Parent of Z and Y
The children of a node are, themselves, trees, called subtrees.
3
Tree Traversals
• One of the most common operations
performed on trees, are a tree traversals
• A traversal starts at the root of the tree and
visits every node in the tree exactly once
– visit means to process the data in the node
• Traversals are either depth-first or breadth-
first
4
Breadth First Traversals
• All the nodes in one
level are visited
• Followed by the nodes
at next level
• Beginning at the root
• For the sample tree
– 7, 6, 10, 4, 8, 13, 3, 5
7
6
3 5
4
10
8 13
5
Depth-First Traversals
• There are 3 different depth-first traversals
– pre-order traversal
– in-order traversal
– post-order traversal
6
Pre-order Traversal: VLR
• Visit the node
• Do a pre-order
traversal of the left
subtree
• Finish with a pre-order
traversal of the right
subtree
• For the sample tree
– 7, 6, 4, 3, 5, 10, 8, 13
7
6
3 5
4
10
8 13
7
Preorder Traversal
r
T1 T2 Tn
Step 1: Visit r
Step 2: Visit T1 in preorder
Step n+1: Visit Tn in preorder
Step 3: Visit T2 in preorder
8
Example
A R EY PM HJ Q T
A
R
EY
P
M
HJ
Q T
9
Ordering of the preorder traversal is the same a the
Universal Address System with lexicographic ordering.
A R EY PM HJ Q T
A
R
EY
P
M
HJ
Q T
0
1 2 3
1.1
2.1
2.2
2.2.1 2.2.2 2.2.3
10
In-order Traversal: LVR
• Do an in-order
traversal of the left
subtree
• Visit the node
• Finish with an in-order
traversal of the right
subtree
• For the sample tree
– 3, 4, 5, 6, 7, 8, 10, 13
7
6
3 5
4
10
8 13
11
Inorder Traversal
Step 1: Visit T1 in inorder
Step 2: Visit r
Step n+1: Visit Tn in inorder
Step 3: Visit T2 in inorder
r
T1 T2 Tn
12
Example
A R EY PM HJ Q T
A
R
EY
P
M
HJ
Q T
13
Post-order Traversal: LRV
7
6
3 5
4
10
8 13
• Do a post-order
traversal of the left
subtree
• Followed by a post-
order traversal of the
right subtree
• Visit the node
• For the sample tree
– 3, 5, 4, 6, 8, 13, 10, 7
14
Postorder Traversal
Step 1: Visit T1 in postorder
Step 2: Visit T2 in postorder
Step n+1: Visit r
Step n: Visit Tn in postorder
r
T1 T2 Tn
15
Example
A R EYP MHJ Q T
A
R
EY
P
M
HJ
Q T
16
Example
(x+y)2
+ (x-3)/(y+2)
+
x y
2
↑
–
x 3
+
y 2
/
+
17
Infix Notation
+
↑
– +
/
+ 2
x y x 3 y 2
• Traverse in inorder (LVR) adding parentheses for each
operation
x + y( ) ↑ 2( ) + x – 3( ) / y + 2( )( )( )
18
Prefix Notation
(Polish Notation)
• Traverse in preorder (VLR)
x+ y↑ 2+ x– 3/ y+ 2
+
↑
– +
/
+ 2
x y x 3 y 2
19
Evaluating Prefix Notation
• In an prefix expression, a binary operator
precedes its two operands
• The expression is evaluated right-left
• Look for the first operator from the right
• Evaluate the operator with the two operands
immediately to its right
20
Example
+ / + 2 2 2 / – 3 2 + 1 0
+ / + 2 2 2 / – 3 2 1
+ / + 2 2 2 / 1 1
+ / + 2 2 2 1
+ / 4 2 1
+ 2 1
3
21
Postfix Notation
(Reverse Polish)
• Traverse in postorder (LRV)
x +y ↑2 +x –3 /y +2
+
↑
– +
/
+ 2
x y x 3 y 2
22
• In an postfix expression, a binary operator
follows its two operands
• The expression is evaluated left-right
• Look for the first operator from the left
• Evaluate the operator with the two operands
immediately to its left
Evaluating Postfix Notation
23
Example
3
2 2 + 2 / 3 2 – 1 0 + / +
4 2 / 3 2 – 1 0 + / +
2 3 2 – 1 0 + / +
2 1 1 0 + / +
2 1 1 / +
2 1 +
24

More Related Content

What's hot

Study On Beam Dyeing Machine
Study On Beam Dyeing MachineStudy On Beam Dyeing Machine
Study On Beam Dyeing MachineMd Rakibul Hassan
 
Pretreatment seminar ppt (3)
Pretreatment seminar ppt (3)Pretreatment seminar ppt (3)
Pretreatment seminar ppt (3)eyssubogale
 
General composition of print paste
General composition of print pasteGeneral composition of print paste
General composition of print pasteAdane Nega
 
Bobbin leading flyer leading
Bobbin leading flyer leadingBobbin leading flyer leading
Bobbin leading flyer leadingFoyej Jaman
 
Machinery for preparation of textile
Machinery for preparation of textileMachinery for preparation of textile
Machinery for preparation of textileShahriar Shovon
 
Dyeing of cotton with vat dyes
Dyeing of cotton with vat dyesDyeing of cotton with vat dyes
Dyeing of cotton with vat dyesAdane Nega
 
Garment printing
Garment printingGarment printing
Garment printingSiva M
 
Winch Dyeing Machine
Winch Dyeing MachineWinch Dyeing Machine
Winch Dyeing MachineAsaye Dessie
 
Textile Printing Presentation
Textile Printing PresentationTextile Printing Presentation
Textile Printing PresentationMazedin Reza
 
Viscosity of different printing paste
Viscosity of different printing pasteViscosity of different printing paste
Viscosity of different printing pasteAdya Tiwari
 
Multiphase weaving
Multiphase weavingMultiphase weaving
Multiphase weavingFuad Ahmed
 
Dyeing of cotton with reactive dyes quality q&a
Dyeing of cotton with reactive dyes quality q&aDyeing of cotton with reactive dyes quality q&a
Dyeing of cotton with reactive dyes quality q&aAdane Nega
 
Dyeing Process & its Auxiliaries
Dyeing Process & its Auxiliaries Dyeing Process & its Auxiliaries
Dyeing Process & its Auxiliaries Anil Kumar
 

What's hot (20)

Needle & cam arrangement of knit fabric
Needle & cam arrangement of knit fabricNeedle & cam arrangement of knit fabric
Needle & cam arrangement of knit fabric
 
Study On Beam Dyeing Machine
Study On Beam Dyeing MachineStudy On Beam Dyeing Machine
Study On Beam Dyeing Machine
 
Pretreatment seminar ppt (3)
Pretreatment seminar ppt (3)Pretreatment seminar ppt (3)
Pretreatment seminar ppt (3)
 
General composition of print paste
General composition of print pasteGeneral composition of print paste
General composition of print paste
 
Reactive dye and disperse dye
Reactive dye and disperse dyeReactive dye and disperse dye
Reactive dye and disperse dye
 
Bobbin leading flyer leading
Bobbin leading flyer leadingBobbin leading flyer leading
Bobbin leading flyer leading
 
Machinery for preparation of textile
Machinery for preparation of textileMachinery for preparation of textile
Machinery for preparation of textile
 
Acid dyes
Acid dyesAcid dyes
Acid dyes
 
Dyeing of cotton with vat dyes
Dyeing of cotton with vat dyesDyeing of cotton with vat dyes
Dyeing of cotton with vat dyes
 
Garment printing
Garment printingGarment printing
Garment printing
 
Winch Dyeing Machine
Winch Dyeing MachineWinch Dyeing Machine
Winch Dyeing Machine
 
Textile Printing Presentation
Textile Printing PresentationTextile Printing Presentation
Textile Printing Presentation
 
Viscosity of different printing paste
Viscosity of different printing pasteViscosity of different printing paste
Viscosity of different printing paste
 
textile finishing
textile finishingtextile finishing
textile finishing
 
Multiphase weaving
Multiphase weavingMultiphase weaving
Multiphase weaving
 
Dyeing of cotton with reactive dyes quality q&a
Dyeing of cotton with reactive dyes quality q&aDyeing of cotton with reactive dyes quality q&a
Dyeing of cotton with reactive dyes quality q&a
 
Wool finishing
Wool finishingWool finishing
Wool finishing
 
Dyeing Process & its Auxiliaries
Dyeing Process & its Auxiliaries Dyeing Process & its Auxiliaries
Dyeing Process & its Auxiliaries
 
sulphur dyes
sulphur dyessulphur dyes
sulphur dyes
 
Discharge printing
Discharge printingDischarge printing
Discharge printing
 

Similar to Tree traversal

Creating a Binary tree from a General Tree.pptx
Creating a Binary tree from a General Tree.pptxCreating a Binary tree from a General Tree.pptx
Creating a Binary tree from a General Tree.pptxDeepaThirumurugan
 
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 terminologyKamranAli649587
 
Mov is turing-complete
Mov is turing-completeMov is turing-complete
Mov is turing-completeyeokm1
 

Similar to Tree traversal (11)

Tree traversal techniques
Tree traversal  techniquesTree traversal  techniques
Tree traversal techniques
 
Tree Traversal
Tree TraversalTree Traversal
Tree Traversal
 
Creating a Binary tree from a General Tree.pptx
Creating a Binary tree from a General Tree.pptxCreating a Binary tree from a General Tree.pptx
Creating a Binary tree from a General Tree.pptx
 
Z transform
Z transformZ transform
Z transform
 
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
 
binary tree
binary treebinary tree
binary tree
 
Trees
Trees Trees
Trees
 
Tree traversal techniques
Tree traversal techniquesTree traversal techniques
Tree traversal techniques
 
Binary tree
Binary treeBinary tree
Binary tree
 
Mov is turing-complete
Mov is turing-completeMov is turing-complete
Mov is turing-complete
 
Depth-First Search
Depth-First SearchDepth-First Search
Depth-First Search
 

Recently uploaded

TỔNG HỢP HƠN 100 ĐỀ THI THỬ TỐT NGHIỆP THPT TOÁN 2024 - TỪ CÁC TRƯỜNG, TRƯỜNG...
TỔNG HỢP HƠN 100 ĐỀ THI THỬ TỐT NGHIỆP THPT TOÁN 2024 - TỪ CÁC TRƯỜNG, TRƯỜNG...TỔNG HỢP HƠN 100 ĐỀ THI THỬ TỐT NGHIỆP THPT TOÁN 2024 - TỪ CÁC TRƯỜNG, TRƯỜNG...
TỔNG HỢP HƠN 100 ĐỀ THI THỬ TỐT NGHIỆP THPT TOÁN 2024 - TỪ CÁC TRƯỜNG, TRƯỜNG...Nguyen Thanh Tu Collection
 
DEMONSTRATION LESSON IN ENGLISH 4 MATATAG CURRICULUM
DEMONSTRATION LESSON IN ENGLISH 4 MATATAG CURRICULUMDEMONSTRATION LESSON IN ENGLISH 4 MATATAG CURRICULUM
DEMONSTRATION LESSON IN ENGLISH 4 MATATAG CURRICULUMELOISARIVERA8
 
Spring gala 2024 photo slideshow - Celebrating School-Community Partnerships
Spring gala 2024 photo slideshow - Celebrating School-Community PartnershipsSpring gala 2024 photo slideshow - Celebrating School-Community Partnerships
Spring gala 2024 photo slideshow - Celebrating School-Community Partnershipsexpandedwebsite
 
AIM of Education-Teachers Training-2024.ppt
AIM of Education-Teachers Training-2024.pptAIM of Education-Teachers Training-2024.ppt
AIM of Education-Teachers Training-2024.pptNishitharanjan Rout
 
The Story of Village Palampur Class 9 Free Study Material PDF
The Story of Village Palampur Class 9 Free Study Material PDFThe Story of Village Palampur Class 9 Free Study Material PDF
The Story of Village Palampur Class 9 Free Study Material PDFVivekanand Anglo Vedic Academy
 
8 Tips for Effective Working Capital Management
8 Tips for Effective Working Capital Management8 Tips for Effective Working Capital Management
8 Tips for Effective Working Capital ManagementMBA Assignment Experts
 
Observing-Correct-Grammar-in-Making-Definitions.pptx
Observing-Correct-Grammar-in-Making-Definitions.pptxObserving-Correct-Grammar-in-Making-Definitions.pptx
Observing-Correct-Grammar-in-Making-Definitions.pptxAdelaideRefugio
 
male presentation...pdf.................
male presentation...pdf.................male presentation...pdf.................
male presentation...pdf.................MirzaAbrarBaig5
 
How to Manage Website in Odoo 17 Studio App.pptx
How to Manage Website in Odoo 17 Studio App.pptxHow to Manage Website in Odoo 17 Studio App.pptx
How to Manage Website in Odoo 17 Studio App.pptxCeline George
 
Stl Algorithms in C++ jjjjjjjjjjjjjjjjjj
Stl Algorithms in C++ jjjjjjjjjjjjjjjjjjStl Algorithms in C++ jjjjjjjjjjjjjjjjjj
Stl Algorithms in C++ jjjjjjjjjjjjjjjjjjMohammed Sikander
 
ĐỀ THAM KHẢO KÌ THI TUYỂN SINH VÀO LỚP 10 MÔN TIẾNG ANH FORM 50 CÂU TRẮC NGHI...
ĐỀ THAM KHẢO KÌ THI TUYỂN SINH VÀO LỚP 10 MÔN TIẾNG ANH FORM 50 CÂU TRẮC NGHI...ĐỀ THAM KHẢO KÌ THI TUYỂN SINH VÀO LỚP 10 MÔN TIẾNG ANH FORM 50 CÂU TRẮC NGHI...
ĐỀ THAM KHẢO KÌ THI TUYỂN SINH VÀO LỚP 10 MÔN TIẾNG ANH FORM 50 CÂU TRẮC NGHI...Nguyen Thanh Tu Collection
 
Trauma-Informed Leadership - Five Practical Principles
Trauma-Informed Leadership - Five Practical PrinciplesTrauma-Informed Leadership - Five Practical Principles
Trauma-Informed Leadership - Five Practical PrinciplesPooky Knightsmith
 
Andreas Schleicher presents at the launch of What does child empowerment mean...
Andreas Schleicher presents at the launch of What does child empowerment mean...Andreas Schleicher presents at the launch of What does child empowerment mean...
Andreas Schleicher presents at the launch of What does child empowerment mean...EduSkills OECD
 
ANTI PARKISON DRUGS.pptx
ANTI         PARKISON          DRUGS.pptxANTI         PARKISON          DRUGS.pptx
ANTI PARKISON DRUGS.pptxPoojaSen20
 
SURVEY I created for uni project research
SURVEY I created for uni project researchSURVEY I created for uni project research
SURVEY I created for uni project researchCaitlinCummins3
 
Book Review of Run For Your Life Powerpoint
Book Review of Run For Your Life PowerpointBook Review of Run For Your Life Powerpoint
Book Review of Run For Your Life Powerpoint23600690
 
24 ĐỀ THAM KHẢO KÌ THI TUYỂN SINH VÀO LỚP 10 MÔN TIẾNG ANH SỞ GIÁO DỤC HẢI DƯ...
24 ĐỀ THAM KHẢO KÌ THI TUYỂN SINH VÀO LỚP 10 MÔN TIẾNG ANH SỞ GIÁO DỤC HẢI DƯ...24 ĐỀ THAM KHẢO KÌ THI TUYỂN SINH VÀO LỚP 10 MÔN TIẾNG ANH SỞ GIÁO DỤC HẢI DƯ...
24 ĐỀ THAM KHẢO KÌ THI TUYỂN SINH VÀO LỚP 10 MÔN TIẾNG ANH SỞ GIÁO DỤC HẢI DƯ...Nguyen Thanh Tu Collection
 

Recently uploaded (20)

TỔNG HỢP HƠN 100 ĐỀ THI THỬ TỐT NGHIỆP THPT TOÁN 2024 - TỪ CÁC TRƯỜNG, TRƯỜNG...
TỔNG HỢP HƠN 100 ĐỀ THI THỬ TỐT NGHIỆP THPT TOÁN 2024 - TỪ CÁC TRƯỜNG, TRƯỜNG...TỔNG HỢP HƠN 100 ĐỀ THI THỬ TỐT NGHIỆP THPT TOÁN 2024 - TỪ CÁC TRƯỜNG, TRƯỜNG...
TỔNG HỢP HƠN 100 ĐỀ THI THỬ TỐT NGHIỆP THPT TOÁN 2024 - TỪ CÁC TRƯỜNG, TRƯỜNG...
 
DEMONSTRATION LESSON IN ENGLISH 4 MATATAG CURRICULUM
DEMONSTRATION LESSON IN ENGLISH 4 MATATAG CURRICULUMDEMONSTRATION LESSON IN ENGLISH 4 MATATAG CURRICULUM
DEMONSTRATION LESSON IN ENGLISH 4 MATATAG CURRICULUM
 
Spring gala 2024 photo slideshow - Celebrating School-Community Partnerships
Spring gala 2024 photo slideshow - Celebrating School-Community PartnershipsSpring gala 2024 photo slideshow - Celebrating School-Community Partnerships
Spring gala 2024 photo slideshow - Celebrating School-Community Partnerships
 
AIM of Education-Teachers Training-2024.ppt
AIM of Education-Teachers Training-2024.pptAIM of Education-Teachers Training-2024.ppt
AIM of Education-Teachers Training-2024.ppt
 
The Story of Village Palampur Class 9 Free Study Material PDF
The Story of Village Palampur Class 9 Free Study Material PDFThe Story of Village Palampur Class 9 Free Study Material PDF
The Story of Village Palampur Class 9 Free Study Material PDF
 
VAMOS CUIDAR DO NOSSO PLANETA! .
VAMOS CUIDAR DO NOSSO PLANETA!                    .VAMOS CUIDAR DO NOSSO PLANETA!                    .
VAMOS CUIDAR DO NOSSO PLANETA! .
 
8 Tips for Effective Working Capital Management
8 Tips for Effective Working Capital Management8 Tips for Effective Working Capital Management
8 Tips for Effective Working Capital Management
 
Observing-Correct-Grammar-in-Making-Definitions.pptx
Observing-Correct-Grammar-in-Making-Definitions.pptxObserving-Correct-Grammar-in-Making-Definitions.pptx
Observing-Correct-Grammar-in-Making-Definitions.pptx
 
male presentation...pdf.................
male presentation...pdf.................male presentation...pdf.................
male presentation...pdf.................
 
ESSENTIAL of (CS/IT/IS) class 07 (Networks)
ESSENTIAL of (CS/IT/IS) class 07 (Networks)ESSENTIAL of (CS/IT/IS) class 07 (Networks)
ESSENTIAL of (CS/IT/IS) class 07 (Networks)
 
How to Manage Website in Odoo 17 Studio App.pptx
How to Manage Website in Odoo 17 Studio App.pptxHow to Manage Website in Odoo 17 Studio App.pptx
How to Manage Website in Odoo 17 Studio App.pptx
 
Supporting Newcomer Multilingual Learners
Supporting Newcomer  Multilingual LearnersSupporting Newcomer  Multilingual Learners
Supporting Newcomer Multilingual Learners
 
Stl Algorithms in C++ jjjjjjjjjjjjjjjjjj
Stl Algorithms in C++ jjjjjjjjjjjjjjjjjjStl Algorithms in C++ jjjjjjjjjjjjjjjjjj
Stl Algorithms in C++ jjjjjjjjjjjjjjjjjj
 
ĐỀ THAM KHẢO KÌ THI TUYỂN SINH VÀO LỚP 10 MÔN TIẾNG ANH FORM 50 CÂU TRẮC NGHI...
ĐỀ THAM KHẢO KÌ THI TUYỂN SINH VÀO LỚP 10 MÔN TIẾNG ANH FORM 50 CÂU TRẮC NGHI...ĐỀ THAM KHẢO KÌ THI TUYỂN SINH VÀO LỚP 10 MÔN TIẾNG ANH FORM 50 CÂU TRẮC NGHI...
ĐỀ THAM KHẢO KÌ THI TUYỂN SINH VÀO LỚP 10 MÔN TIẾNG ANH FORM 50 CÂU TRẮC NGHI...
 
Trauma-Informed Leadership - Five Practical Principles
Trauma-Informed Leadership - Five Practical PrinciplesTrauma-Informed Leadership - Five Practical Principles
Trauma-Informed Leadership - Five Practical Principles
 
Andreas Schleicher presents at the launch of What does child empowerment mean...
Andreas Schleicher presents at the launch of What does child empowerment mean...Andreas Schleicher presents at the launch of What does child empowerment mean...
Andreas Schleicher presents at the launch of What does child empowerment mean...
 
ANTI PARKISON DRUGS.pptx
ANTI         PARKISON          DRUGS.pptxANTI         PARKISON          DRUGS.pptx
ANTI PARKISON DRUGS.pptx
 
SURVEY I created for uni project research
SURVEY I created for uni project researchSURVEY I created for uni project research
SURVEY I created for uni project research
 
Book Review of Run For Your Life Powerpoint
Book Review of Run For Your Life PowerpointBook Review of Run For Your Life Powerpoint
Book Review of Run For Your Life Powerpoint
 
24 ĐỀ THAM KHẢO KÌ THI TUYỂN SINH VÀO LỚP 10 MÔN TIẾNG ANH SỞ GIÁO DỤC HẢI DƯ...
24 ĐỀ THAM KHẢO KÌ THI TUYỂN SINH VÀO LỚP 10 MÔN TIẾNG ANH SỞ GIÁO DỤC HẢI DƯ...24 ĐỀ THAM KHẢO KÌ THI TUYỂN SINH VÀO LỚP 10 MÔN TIẾNG ANH SỞ GIÁO DỤC HẢI DƯ...
24 ĐỀ THAM KHẢO KÌ THI TUYỂN SINH VÀO LỚP 10 MÔN TIẾNG ANH SỞ GIÁO DỤC HẢI DƯ...
 

Tree traversal

  • 1. 1 Md. Shamim Alamgir Dept. SWE Batch:19th Topics: Tree Traversal
  • 2. 2 Tree Anatomy R S Y Z X T U V W Root Internal Node Leaf Subtree Level 0 Level 1 Level 2 Level 3 Child of X Parent of Z and Y The children of a node are, themselves, trees, called subtrees.
  • 3. 3 Tree Traversals • One of the most common operations performed on trees, are a tree traversals • A traversal starts at the root of the tree and visits every node in the tree exactly once – visit means to process the data in the node • Traversals are either depth-first or breadth- first
  • 4. 4 Breadth First Traversals • All the nodes in one level are visited • Followed by the nodes at next level • Beginning at the root • For the sample tree – 7, 6, 10, 4, 8, 13, 3, 5 7 6 3 5 4 10 8 13
  • 5. 5 Depth-First Traversals • There are 3 different depth-first traversals – pre-order traversal – in-order traversal – post-order traversal
  • 6. 6 Pre-order Traversal: VLR • Visit the node • Do a pre-order traversal of the left subtree • Finish with a pre-order traversal of the right subtree • For the sample tree – 7, 6, 4, 3, 5, 10, 8, 13 7 6 3 5 4 10 8 13
  • 7. 7 Preorder Traversal r T1 T2 Tn Step 1: Visit r Step 2: Visit T1 in preorder Step n+1: Visit Tn in preorder Step 3: Visit T2 in preorder
  • 8. 8 Example A R EY PM HJ Q T A R EY P M HJ Q T
  • 9. 9 Ordering of the preorder traversal is the same a the Universal Address System with lexicographic ordering. A R EY PM HJ Q T A R EY P M HJ Q T 0 1 2 3 1.1 2.1 2.2 2.2.1 2.2.2 2.2.3
  • 10. 10 In-order Traversal: LVR • Do an in-order traversal of the left subtree • Visit the node • Finish with an in-order traversal of the right subtree • For the sample tree – 3, 4, 5, 6, 7, 8, 10, 13 7 6 3 5 4 10 8 13
  • 11. 11 Inorder Traversal Step 1: Visit T1 in inorder Step 2: Visit r Step n+1: Visit Tn in inorder Step 3: Visit T2 in inorder r T1 T2 Tn
  • 12. 12 Example A R EY PM HJ Q T A R EY P M HJ Q T
  • 13. 13 Post-order Traversal: LRV 7 6 3 5 4 10 8 13 • Do a post-order traversal of the left subtree • Followed by a post- order traversal of the right subtree • Visit the node • For the sample tree – 3, 5, 4, 6, 8, 13, 10, 7
  • 14. 14 Postorder Traversal Step 1: Visit T1 in postorder Step 2: Visit T2 in postorder Step n+1: Visit r Step n: Visit Tn in postorder r T1 T2 Tn
  • 15. 15 Example A R EYP MHJ Q T A R EY P M HJ Q T
  • 17. 17 Infix Notation + ↑ – + / + 2 x y x 3 y 2 • Traverse in inorder (LVR) adding parentheses for each operation x + y( ) ↑ 2( ) + x – 3( ) / y + 2( )( )( )
  • 18. 18 Prefix Notation (Polish Notation) • Traverse in preorder (VLR) x+ y↑ 2+ x– 3/ y+ 2 + ↑ – + / + 2 x y x 3 y 2
  • 19. 19 Evaluating Prefix Notation • In an prefix expression, a binary operator precedes its two operands • The expression is evaluated right-left • Look for the first operator from the right • Evaluate the operator with the two operands immediately to its right
  • 20. 20 Example + / + 2 2 2 / – 3 2 + 1 0 + / + 2 2 2 / – 3 2 1 + / + 2 2 2 / 1 1 + / + 2 2 2 1 + / 4 2 1 + 2 1 3
  • 21. 21 Postfix Notation (Reverse Polish) • Traverse in postorder (LRV) x +y ↑2 +x –3 /y +2 + ↑ – + / + 2 x y x 3 y 2
  • 22. 22 • In an postfix expression, a binary operator follows its two operands • The expression is evaluated left-right • Look for the first operator from the left • Evaluate the operator with the two operands immediately to its left Evaluating Postfix Notation
  • 23. 23 Example 3 2 2 + 2 / 3 2 – 1 0 + / + 4 2 / 3 2 – 1 0 + / + 2 3 2 – 1 0 + / + 2 1 1 0 + / + 2 1 1 / + 2 1 +
  • 24. 24