SlideShare a Scribd company logo
Data Structures and Algorithms I 
Introduction to Trees
Towards Non-Linear Data Structures 
 The data structures we have studied so far are 
linear; an element is followed by exactly one 
element 
 The data can also be represented in a non-linear 
fashion 
 An important concept is a family like structure; 
this structure is called a tree
Tree 
 A tree is a hierarchical data structure which 
consists of a set of nodes connected through edges 
 Note: A can be followed by B or C (i.e. not 
exactly one follower, c.f. arrays) 
A 
B C 
H 
I 
D E F G
Terminology 
 Node: is a structure which 
normally contains a value, e.g. 
round boxes labeled as D,E, etc. 
 Root: the top most node in the 
tree, e.g. A is root node 
 Child Node: the roots of the 
subtrees of a node X are the 
children of X. e.g. B and C are 
children of A – A is parent of B 
and C 
A 
B C 
H 
I 
D E F G
Terminology 
 Terminal nodes 
(leaf/external): nodes that 
have degree zero. OR nodes 
with no children. E.g. D, E 
 Nonterminal/internal nodes: 
nodes that don’t belong to 
terminal nodes. E.g. B, C 
A 
B C 
H 
I 
D E F G
Terminology 
 Siblings: children of the 
same parent are said to be 
siblings. E.g. B and C are 
siblings, so is F and G. 
 Ancestors of a node: all 
the nodes along the path 
from the root to that node. 
e.g. ancestors of I are I, H, 
C and A 
A 
B C 
H 
I 
D E F G
Tree Traversal 
 What is traversal? 
 Traversal is the facility to move through a structure, 
visiting each of the nodes exactly once 
 Which of the following is not traversal? 
1. Bisha  Abaha  Jeddah  Riadh 
2. Bisha  Abaha  Jeddah  Bisha  Riadh (A 
repeated visit to Bisha – not allowed)
Tree Traversal 
 Pre-order Traversal 
 Post-order Traversal 
 In-order Traversal 
 Notion 
 P: Visit the parent node 
 L: Visit the left subtree 
 R: Visit the right subtree
Pre-order Traversal 
 PLR, i.e., 
 First, visit the parent node 
 Then, visit the left subtree (in pre-order) 
 Then, visit the right subtree (in pre-order) 
40 
30 45 
20 35 60
Pre-order Traversal 
Step 1: root = 40, so display it, then traverse its left 
subtree (root = 40) and then right subtree (root = 45) 
40 
30 45 
20 35 60 
Display: 40
Pre-order Traversal 
Step 2: root = 30, so display it, then traverse its left 
subtree (root = 20) and then right subtree (root = 35) 
40 
30 45 
20 35 60 
Display: 40 30
Pre-order Traversal 
Step 3: root = 20, so display it, then traverse its left 
subtree (root = null) and then right subtree (root = null) 
40 
30 45 
20 35 60 
Display: 40 30 20 
Since node with value 20 is a leaf node, we finished traversing this 
subtree (root = 20), which is a left subtree of node with value 30. 
So, in the next step we’ll traverse the right subtree of 30.
Pre-order Traversal 
Step 4: root = 35, so display it, then traverse its left 
subtree (root = null) and then right subtree (root = null) 
40 
30 45 
20 35 60 
Display: 40 30 20 35 
Since node with value 35 is a leaf node, we finished traversing this 
subtree (root = 35), which is a right subtree of node with value 30. 
So, in the next step we’ll traverse the right subtree of 40.
Pre-order Traversal 
Step 5: root = 45, so display it, then traverse its left 
subtree (root = null) and then right subtree (root = 60) 
40 
30 45 
20 35 60 
Display: 40 30 20 35 45 
Since node with value 45 has no left subtree but a right subtree 
(root = 60), in the next step we’ll traverse this subtree (root = 60).
Pre-order Traversal 
Step 6: root = 60, so display it, then traverse its left 
subtree (root = null) and then right subtree (root = null) 
40 
30 45 
20 35 60 
Display: 40 30 20 35 45 60 
Finished!
Post-order Traversal 
 LRP, i.e., 
 First, visit the left subtree (in post-order) 
 Then, visit the right subtree (in post-order) 
 Then, visit the parent
Post-order Traversal 
40 
30 45 
20 35 60 
Step 1: 
Display: 20
Post-order Traversal 
40 
30 45 
20 35 60 
Step 2: 
Display: 20 35
Post-order Traversal 
40 
30 45 
20 35 60 
Step 3: 
Display: 20 35 30
Post-order Traversal 
Step 4: Note that the node with value 45 has no left subtree! 
40 
30 45 
20 35 60 
Display: 20 35 30 60
Post-order Traversal 
40 
30 45 
20 35 60 
Step 5: 
Display: 20 35 30 60 45
Post-order Traversal 
40 
30 45 
20 35 60 
Step 6: 
Display: 20 35 30 60 45 40 
Finished!
In-order Traversal 
 LPR, i.e., 
 First, visit the left subtree (in in-order) 
 Then, visit the parent 
 Then, visit the right subtree (in in-order)
In-order Traversal 
40 
30 45 
20 35 60 
Step 1: 
Display: 20
In-order Traversal 
40 
30 45 
20 35 60 
Step 2: 
Display: 20 30
In-order Traversal 
40 
30 45 
20 35 60 
Step 3: 
Display: 20 30 35
In-order Traversal 
40 
30 45 
20 35 60 
Step 4: 
Display: 20 30 35 40
In-order Traversal 
40 
30 45 
20 35 60 
Step 5: 
Display: 20 30 35 40 45
In-order Traversal 
40 
30 45 
20 35 60 
Step 6: 
Display: 20 30 35 40 45 60

More Related Content

Viewers also liked

AWS Innovate: AWS Container Management using Amazon EC2 Container Service an...
AWS Innovate:  AWS Container Management using Amazon EC2 Container Service an...AWS Innovate:  AWS Container Management using Amazon EC2 Container Service an...
AWS Innovate: AWS Container Management using Amazon EC2 Container Service an...
Amazon Web Services Korea
 
Applying Design Patterns in Practice
Applying Design Patterns in PracticeApplying Design Patterns in Practice
Applying Design Patterns in Practice
Ganesh Samarthyam
 
AWS ECS Quick Introduction
AWS ECS Quick IntroductionAWS ECS Quick Introduction
AWS ECS Quick Introduction
Vinothini Raju
 
Web Database
Web DatabaseWeb Database
Web Database
idroos7
 
Data Structures- Part7 linked lists
Data Structures- Part7 linked listsData Structures- Part7 linked lists
Data Structures- Part7 linked lists
Abdullah Al-hazmy
 
10 Sets of Best Practices for Java 8
10 Sets of Best Practices for Java 810 Sets of Best Practices for Java 8
10 Sets of Best Practices for Java 8
Garth Gilmour
 
Advanced Java - Praticals
Advanced Java - PraticalsAdvanced Java - Praticals
Advanced Java - Praticals
Fahad Shaikh
 
Java 8 Support at the JVM Level
Java 8 Support at the JVM LevelJava 8 Support at the JVM Level
Java 8 Support at the JVM Level
Nikita Lipsky
 
Building Digital Transaction Systems in the new Banking World
Building Digital Transaction Systems in the new Banking WorldBuilding Digital Transaction Systems in the new Banking World
Building Digital Transaction Systems in the new Banking World
Ramit Surana
 
Coding standards for java
Coding standards for javaCoding standards for java
Coding standards for javamaheshm1206
 
Arrays Basics
Arrays BasicsArrays Basics
Arrays Basics
Nikhil Pandit
 
Ad java prac sol set
Ad java prac sol setAd java prac sol set
Ad java prac sol set
Iram Ramrajkar
 
Modern Programming in Java 8 - Lambdas, Streams and Date Time API
Modern Programming in Java 8 - Lambdas, Streams and Date Time APIModern Programming in Java 8 - Lambdas, Streams and Date Time API
Modern Programming in Java 8 - Lambdas, Streams and Date Time API
Ganesh Samarthyam
 
Running Microservices on Amazon ECS - AWS April 2016 Webinar Series
Running Microservices on Amazon ECS - AWS April 2016 Webinar SeriesRunning Microservices on Amazon ECS - AWS April 2016 Webinar Series
Running Microservices on Amazon ECS - AWS April 2016 Webinar Series
Amazon Web Services
 
Java 103 intro to java data structures
Java 103   intro to java data structuresJava 103   intro to java data structures
Java 103 intro to java data structures
agorolabs
 
Arrays Data Structure
Arrays Data StructureArrays Data Structure
Arrays Data Structure
student
 
Java 8 ​and ​Best Practices
 Java 8 ​and ​Best Practices Java 8 ​and ​Best Practices
Java 8 ​and ​Best Practices
WSO2
 
Advanced Java Practical File
Advanced Java Practical FileAdvanced Java Practical File
Advanced Java Practical FileSoumya Behera
 
Java PRACTICAL file
Java PRACTICAL fileJava PRACTICAL file
Java PRACTICAL file
RACHIT_GUPTA
 
Array Presentation (EngineerBaBu.com)
Array Presentation (EngineerBaBu.com)Array Presentation (EngineerBaBu.com)
Array Presentation (EngineerBaBu.com)
EngineerBabu
 

Viewers also liked (20)

AWS Innovate: AWS Container Management using Amazon EC2 Container Service an...
AWS Innovate:  AWS Container Management using Amazon EC2 Container Service an...AWS Innovate:  AWS Container Management using Amazon EC2 Container Service an...
AWS Innovate: AWS Container Management using Amazon EC2 Container Service an...
 
Applying Design Patterns in Practice
Applying Design Patterns in PracticeApplying Design Patterns in Practice
Applying Design Patterns in Practice
 
AWS ECS Quick Introduction
AWS ECS Quick IntroductionAWS ECS Quick Introduction
AWS ECS Quick Introduction
 
Web Database
Web DatabaseWeb Database
Web Database
 
Data Structures- Part7 linked lists
Data Structures- Part7 linked listsData Structures- Part7 linked lists
Data Structures- Part7 linked lists
 
10 Sets of Best Practices for Java 8
10 Sets of Best Practices for Java 810 Sets of Best Practices for Java 8
10 Sets of Best Practices for Java 8
 
Advanced Java - Praticals
Advanced Java - PraticalsAdvanced Java - Praticals
Advanced Java - Praticals
 
Java 8 Support at the JVM Level
Java 8 Support at the JVM LevelJava 8 Support at the JVM Level
Java 8 Support at the JVM Level
 
Building Digital Transaction Systems in the new Banking World
Building Digital Transaction Systems in the new Banking WorldBuilding Digital Transaction Systems in the new Banking World
Building Digital Transaction Systems in the new Banking World
 
Coding standards for java
Coding standards for javaCoding standards for java
Coding standards for java
 
Arrays Basics
Arrays BasicsArrays Basics
Arrays Basics
 
Ad java prac sol set
Ad java prac sol setAd java prac sol set
Ad java prac sol set
 
Modern Programming in Java 8 - Lambdas, Streams and Date Time API
Modern Programming in Java 8 - Lambdas, Streams and Date Time APIModern Programming in Java 8 - Lambdas, Streams and Date Time API
Modern Programming in Java 8 - Lambdas, Streams and Date Time API
 
Running Microservices on Amazon ECS - AWS April 2016 Webinar Series
Running Microservices on Amazon ECS - AWS April 2016 Webinar SeriesRunning Microservices on Amazon ECS - AWS April 2016 Webinar Series
Running Microservices on Amazon ECS - AWS April 2016 Webinar Series
 
Java 103 intro to java data structures
Java 103   intro to java data structuresJava 103   intro to java data structures
Java 103 intro to java data structures
 
Arrays Data Structure
Arrays Data StructureArrays Data Structure
Arrays Data Structure
 
Java 8 ​and ​Best Practices
 Java 8 ​and ​Best Practices Java 8 ​and ​Best Practices
Java 8 ​and ​Best Practices
 
Advanced Java Practical File
Advanced Java Practical FileAdvanced Java Practical File
Advanced Java Practical File
 
Java PRACTICAL file
Java PRACTICAL fileJava PRACTICAL file
Java PRACTICAL file
 
Array Presentation (EngineerBaBu.com)
Array Presentation (EngineerBaBu.com)Array Presentation (EngineerBaBu.com)
Array Presentation (EngineerBaBu.com)
 

Recently uploaded

Executive Directors Chat Leveraging AI for Diversity, Equity, and Inclusion
Executive Directors Chat  Leveraging AI for Diversity, Equity, and InclusionExecutive Directors Chat  Leveraging AI for Diversity, Equity, and Inclusion
Executive Directors Chat Leveraging AI for Diversity, Equity, and Inclusion
TechSoup
 
Normal Labour/ Stages of Labour/ Mechanism of Labour
Normal Labour/ Stages of Labour/ Mechanism of LabourNormal Labour/ Stages of Labour/ Mechanism of Labour
Normal Labour/ Stages of Labour/ Mechanism of Labour
Wasim Ak
 
The basics of sentences session 5pptx.pptx
The basics of sentences session 5pptx.pptxThe basics of sentences session 5pptx.pptx
The basics of sentences session 5pptx.pptx
heathfieldcps1
 
Reflective and Evaluative Practice...pdf
Reflective and Evaluative Practice...pdfReflective and Evaluative Practice...pdf
Reflective and Evaluative Practice...pdf
amberjdewit93
 
RPMS TEMPLATE FOR SCHOOL YEAR 2023-2024 FOR TEACHER 1 TO TEACHER 3
RPMS TEMPLATE FOR SCHOOL YEAR 2023-2024 FOR TEACHER 1 TO TEACHER 3RPMS TEMPLATE FOR SCHOOL YEAR 2023-2024 FOR TEACHER 1 TO TEACHER 3
RPMS TEMPLATE FOR SCHOOL YEAR 2023-2024 FOR TEACHER 1 TO TEACHER 3
IreneSebastianRueco1
 
kitab khulasah nurul yaqin jilid 1 - 2.pptx
kitab khulasah nurul yaqin jilid 1 - 2.pptxkitab khulasah nurul yaqin jilid 1 - 2.pptx
kitab khulasah nurul yaqin jilid 1 - 2.pptx
datarid22
 
Aficamten in HCM (SEQUOIA HCM TRIAL 2024)
Aficamten in HCM (SEQUOIA HCM TRIAL 2024)Aficamten in HCM (SEQUOIA HCM TRIAL 2024)
Aficamten in HCM (SEQUOIA HCM TRIAL 2024)
Ashish Kohli
 
Introduction to AI for Nonprofits with Tapp Network
Introduction to AI for Nonprofits with Tapp NetworkIntroduction to AI for Nonprofits with Tapp Network
Introduction to AI for Nonprofits with Tapp Network
TechSoup
 
বাংলাদেশ অর্থনৈতিক সমীক্ষা (Economic Review) ২০২৪ UJS App.pdf
বাংলাদেশ অর্থনৈতিক সমীক্ষা (Economic Review) ২০২৪ UJS App.pdfবাংলাদেশ অর্থনৈতিক সমীক্ষা (Economic Review) ২০২৪ UJS App.pdf
বাংলাদেশ অর্থনৈতিক সমীক্ষা (Economic Review) ২০২৪ UJS App.pdf
eBook.com.bd (প্রয়োজনীয় বাংলা বই)
 
South African Journal of Science: Writing with integrity workshop (2024)
South African Journal of Science: Writing with integrity workshop (2024)South African Journal of Science: Writing with integrity workshop (2024)
South African Journal of Science: Writing with integrity workshop (2024)
Academy of Science of South Africa
 
Assignment_4_ArianaBusciglio Marvel(1).docx
Assignment_4_ArianaBusciglio Marvel(1).docxAssignment_4_ArianaBusciglio Marvel(1).docx
Assignment_4_ArianaBusciglio Marvel(1).docx
ArianaBusciglio
 
Group Presentation 2 Economics.Ariana Buscigliopptx
Group Presentation 2 Economics.Ariana BuscigliopptxGroup Presentation 2 Economics.Ariana Buscigliopptx
Group Presentation 2 Economics.Ariana Buscigliopptx
ArianaBusciglio
 
Pollock and Snow "DEIA in the Scholarly Landscape, Session One: Setting Expec...
Pollock and Snow "DEIA in the Scholarly Landscape, Session One: Setting Expec...Pollock and Snow "DEIA in the Scholarly Landscape, Session One: Setting Expec...
Pollock and Snow "DEIA in the Scholarly Landscape, Session One: Setting Expec...
National Information Standards Organization (NISO)
 
A Strategic Approach: GenAI in Education
A Strategic Approach: GenAI in EducationA Strategic Approach: GenAI in Education
A Strategic Approach: GenAI in Education
Peter Windle
 
Digital Artefact 1 - Tiny Home Environmental Design
Digital Artefact 1 - Tiny Home Environmental DesignDigital Artefact 1 - Tiny Home Environmental Design
Digital Artefact 1 - Tiny Home Environmental Design
amberjdewit93
 
CACJapan - GROUP Presentation 1- Wk 4.pdf
CACJapan - GROUP Presentation 1- Wk 4.pdfCACJapan - GROUP Presentation 1- Wk 4.pdf
CACJapan - GROUP Presentation 1- Wk 4.pdf
camakaiclarkmusic
 
MASS MEDIA STUDIES-835-CLASS XI Resource Material.pdf
MASS MEDIA STUDIES-835-CLASS XI Resource Material.pdfMASS MEDIA STUDIES-835-CLASS XI Resource Material.pdf
MASS MEDIA STUDIES-835-CLASS XI Resource Material.pdf
goswamiyash170123
 
What is the purpose of studying mathematics.pptx
What is the purpose of studying mathematics.pptxWhat is the purpose of studying mathematics.pptx
What is the purpose of studying mathematics.pptx
christianmathematics
 
PCOS corelations and management through Ayurveda.
PCOS corelations and management through Ayurveda.PCOS corelations and management through Ayurveda.
PCOS corelations and management through Ayurveda.
Dr. Shivangi Singh Parihar
 
Digital Artifact 1 - 10VCD Environments Unit
Digital Artifact 1 - 10VCD Environments UnitDigital Artifact 1 - 10VCD Environments Unit
Digital Artifact 1 - 10VCD Environments Unit
chanes7
 

Recently uploaded (20)

Executive Directors Chat Leveraging AI for Diversity, Equity, and Inclusion
Executive Directors Chat  Leveraging AI for Diversity, Equity, and InclusionExecutive Directors Chat  Leveraging AI for Diversity, Equity, and Inclusion
Executive Directors Chat Leveraging AI for Diversity, Equity, and Inclusion
 
Normal Labour/ Stages of Labour/ Mechanism of Labour
Normal Labour/ Stages of Labour/ Mechanism of LabourNormal Labour/ Stages of Labour/ Mechanism of Labour
Normal Labour/ Stages of Labour/ Mechanism of Labour
 
The basics of sentences session 5pptx.pptx
The basics of sentences session 5pptx.pptxThe basics of sentences session 5pptx.pptx
The basics of sentences session 5pptx.pptx
 
Reflective and Evaluative Practice...pdf
Reflective and Evaluative Practice...pdfReflective and Evaluative Practice...pdf
Reflective and Evaluative Practice...pdf
 
RPMS TEMPLATE FOR SCHOOL YEAR 2023-2024 FOR TEACHER 1 TO TEACHER 3
RPMS TEMPLATE FOR SCHOOL YEAR 2023-2024 FOR TEACHER 1 TO TEACHER 3RPMS TEMPLATE FOR SCHOOL YEAR 2023-2024 FOR TEACHER 1 TO TEACHER 3
RPMS TEMPLATE FOR SCHOOL YEAR 2023-2024 FOR TEACHER 1 TO TEACHER 3
 
kitab khulasah nurul yaqin jilid 1 - 2.pptx
kitab khulasah nurul yaqin jilid 1 - 2.pptxkitab khulasah nurul yaqin jilid 1 - 2.pptx
kitab khulasah nurul yaqin jilid 1 - 2.pptx
 
Aficamten in HCM (SEQUOIA HCM TRIAL 2024)
Aficamten in HCM (SEQUOIA HCM TRIAL 2024)Aficamten in HCM (SEQUOIA HCM TRIAL 2024)
Aficamten in HCM (SEQUOIA HCM TRIAL 2024)
 
Introduction to AI for Nonprofits with Tapp Network
Introduction to AI for Nonprofits with Tapp NetworkIntroduction to AI for Nonprofits with Tapp Network
Introduction to AI for Nonprofits with Tapp Network
 
বাংলাদেশ অর্থনৈতিক সমীক্ষা (Economic Review) ২০২৪ UJS App.pdf
বাংলাদেশ অর্থনৈতিক সমীক্ষা (Economic Review) ২০২৪ UJS App.pdfবাংলাদেশ অর্থনৈতিক সমীক্ষা (Economic Review) ২০২৪ UJS App.pdf
বাংলাদেশ অর্থনৈতিক সমীক্ষা (Economic Review) ২০২৪ UJS App.pdf
 
South African Journal of Science: Writing with integrity workshop (2024)
South African Journal of Science: Writing with integrity workshop (2024)South African Journal of Science: Writing with integrity workshop (2024)
South African Journal of Science: Writing with integrity workshop (2024)
 
Assignment_4_ArianaBusciglio Marvel(1).docx
Assignment_4_ArianaBusciglio Marvel(1).docxAssignment_4_ArianaBusciglio Marvel(1).docx
Assignment_4_ArianaBusciglio Marvel(1).docx
 
Group Presentation 2 Economics.Ariana Buscigliopptx
Group Presentation 2 Economics.Ariana BuscigliopptxGroup Presentation 2 Economics.Ariana Buscigliopptx
Group Presentation 2 Economics.Ariana Buscigliopptx
 
Pollock and Snow "DEIA in the Scholarly Landscape, Session One: Setting Expec...
Pollock and Snow "DEIA in the Scholarly Landscape, Session One: Setting Expec...Pollock and Snow "DEIA in the Scholarly Landscape, Session One: Setting Expec...
Pollock and Snow "DEIA in the Scholarly Landscape, Session One: Setting Expec...
 
A Strategic Approach: GenAI in Education
A Strategic Approach: GenAI in EducationA Strategic Approach: GenAI in Education
A Strategic Approach: GenAI in Education
 
Digital Artefact 1 - Tiny Home Environmental Design
Digital Artefact 1 - Tiny Home Environmental DesignDigital Artefact 1 - Tiny Home Environmental Design
Digital Artefact 1 - Tiny Home Environmental Design
 
CACJapan - GROUP Presentation 1- Wk 4.pdf
CACJapan - GROUP Presentation 1- Wk 4.pdfCACJapan - GROUP Presentation 1- Wk 4.pdf
CACJapan - GROUP Presentation 1- Wk 4.pdf
 
MASS MEDIA STUDIES-835-CLASS XI Resource Material.pdf
MASS MEDIA STUDIES-835-CLASS XI Resource Material.pdfMASS MEDIA STUDIES-835-CLASS XI Resource Material.pdf
MASS MEDIA STUDIES-835-CLASS XI Resource Material.pdf
 
What is the purpose of studying mathematics.pptx
What is the purpose of studying mathematics.pptxWhat is the purpose of studying mathematics.pptx
What is the purpose of studying mathematics.pptx
 
PCOS corelations and management through Ayurveda.
PCOS corelations and management through Ayurveda.PCOS corelations and management through Ayurveda.
PCOS corelations and management through Ayurveda.
 
Digital Artifact 1 - 10VCD Environments Unit
Digital Artifact 1 - 10VCD Environments UnitDigital Artifact 1 - 10VCD Environments Unit
Digital Artifact 1 - 10VCD Environments Unit
 

Data Structures- Part9 trees simplified

  • 1. Data Structures and Algorithms I Introduction to Trees
  • 2. Towards Non-Linear Data Structures  The data structures we have studied so far are linear; an element is followed by exactly one element  The data can also be represented in a non-linear fashion  An important concept is a family like structure; this structure is called a tree
  • 3. Tree  A tree is a hierarchical data structure which consists of a set of nodes connected through edges  Note: A can be followed by B or C (i.e. not exactly one follower, c.f. arrays) A B C H I D E F G
  • 4. Terminology  Node: is a structure which normally contains a value, e.g. round boxes labeled as D,E, etc.  Root: the top most node in the tree, e.g. A is root node  Child Node: the roots of the subtrees of a node X are the children of X. e.g. B and C are children of A – A is parent of B and C A B C H I D E F G
  • 5. Terminology  Terminal nodes (leaf/external): nodes that have degree zero. OR nodes with no children. E.g. D, E  Nonterminal/internal nodes: nodes that don’t belong to terminal nodes. E.g. B, C A B C H I D E F G
  • 6. Terminology  Siblings: children of the same parent are said to be siblings. E.g. B and C are siblings, so is F and G.  Ancestors of a node: all the nodes along the path from the root to that node. e.g. ancestors of I are I, H, C and A A B C H I D E F G
  • 7. Tree Traversal  What is traversal?  Traversal is the facility to move through a structure, visiting each of the nodes exactly once  Which of the following is not traversal? 1. Bisha  Abaha  Jeddah  Riadh 2. Bisha  Abaha  Jeddah  Bisha  Riadh (A repeated visit to Bisha – not allowed)
  • 8. Tree Traversal  Pre-order Traversal  Post-order Traversal  In-order Traversal  Notion  P: Visit the parent node  L: Visit the left subtree  R: Visit the right subtree
  • 9. Pre-order Traversal  PLR, i.e.,  First, visit the parent node  Then, visit the left subtree (in pre-order)  Then, visit the right subtree (in pre-order) 40 30 45 20 35 60
  • 10. Pre-order Traversal Step 1: root = 40, so display it, then traverse its left subtree (root = 40) and then right subtree (root = 45) 40 30 45 20 35 60 Display: 40
  • 11. Pre-order Traversal Step 2: root = 30, so display it, then traverse its left subtree (root = 20) and then right subtree (root = 35) 40 30 45 20 35 60 Display: 40 30
  • 12. Pre-order Traversal Step 3: root = 20, so display it, then traverse its left subtree (root = null) and then right subtree (root = null) 40 30 45 20 35 60 Display: 40 30 20 Since node with value 20 is a leaf node, we finished traversing this subtree (root = 20), which is a left subtree of node with value 30. So, in the next step we’ll traverse the right subtree of 30.
  • 13. Pre-order Traversal Step 4: root = 35, so display it, then traverse its left subtree (root = null) and then right subtree (root = null) 40 30 45 20 35 60 Display: 40 30 20 35 Since node with value 35 is a leaf node, we finished traversing this subtree (root = 35), which is a right subtree of node with value 30. So, in the next step we’ll traverse the right subtree of 40.
  • 14. Pre-order Traversal Step 5: root = 45, so display it, then traverse its left subtree (root = null) and then right subtree (root = 60) 40 30 45 20 35 60 Display: 40 30 20 35 45 Since node with value 45 has no left subtree but a right subtree (root = 60), in the next step we’ll traverse this subtree (root = 60).
  • 15. Pre-order Traversal Step 6: root = 60, so display it, then traverse its left subtree (root = null) and then right subtree (root = null) 40 30 45 20 35 60 Display: 40 30 20 35 45 60 Finished!
  • 16. Post-order Traversal  LRP, i.e.,  First, visit the left subtree (in post-order)  Then, visit the right subtree (in post-order)  Then, visit the parent
  • 17. Post-order Traversal 40 30 45 20 35 60 Step 1: Display: 20
  • 18. Post-order Traversal 40 30 45 20 35 60 Step 2: Display: 20 35
  • 19. Post-order Traversal 40 30 45 20 35 60 Step 3: Display: 20 35 30
  • 20. Post-order Traversal Step 4: Note that the node with value 45 has no left subtree! 40 30 45 20 35 60 Display: 20 35 30 60
  • 21. Post-order Traversal 40 30 45 20 35 60 Step 5: Display: 20 35 30 60 45
  • 22. Post-order Traversal 40 30 45 20 35 60 Step 6: Display: 20 35 30 60 45 40 Finished!
  • 23. In-order Traversal  LPR, i.e.,  First, visit the left subtree (in in-order)  Then, visit the parent  Then, visit the right subtree (in in-order)
  • 24. In-order Traversal 40 30 45 20 35 60 Step 1: Display: 20
  • 25. In-order Traversal 40 30 45 20 35 60 Step 2: Display: 20 30
  • 26. In-order Traversal 40 30 45 20 35 60 Step 3: Display: 20 30 35
  • 27. In-order Traversal 40 30 45 20 35 60 Step 4: Display: 20 30 35 40
  • 28. In-order Traversal 40 30 45 20 35 60 Step 5: Display: 20 30 35 40 45
  • 29. In-order Traversal 40 30 45 20 35 60 Step 6: Display: 20 30 35 40 45 60