SlideShare a Scribd company logo
5
Most read
9
Most read
 The process of systematically visiting all 
the nodes in a tree and performing some 
computation at each node in the tree is 
called a tree traversal.
H 
D 
B 
N 
L 
F J 
A C E G I K M O 
H B F N E G O
Name for each Node: CODE 
public void displayTree(Node n){ 
if( n == null ) return; 
printf(n.data); 
displayTree(n.left); 
displayTree(n.right); 
} 
•Visit the node 
•Visit the left subtree, if any. 
•Visit the right subtree, if any. 
Preorder 
(N-L-R) 
public void displayTree(Node n){ 
if( n == null ) return; 
displayTree(n.left); 
printf(n.data); 
displayTree(n.right); 
} 
•Visit the left subtree, if any. 
Visit the node 
•Visit the right subtree, if any. 
Inorder 
(L-N-R) 
public void displayTree(Node n){ 
if( n == null ) return; 
displayTree(n.left); 
displayTree(n.right); 
printf(n.data); 
} 
•Visit the left subtree, if any. 
•Visit the right subtree, if any. 
•Visit the node 
Postorder 
(L-R-N)
N-L-R 
H 
D 
B 
N 
L 
F J 
A C E G I K M O 
H D A C E L J I K N O
L-N-R 
H 
D 
B 
N 
L 
F J 
A C E G I K M O 
A B C D E F G H I J K L M N O 
Note: An inorder traversal of a BST visits the keys sorted in 
increasing order.
L-R-N 
H 
D 
B 
N 
L 
F J 
A C E G I K M O 
A C B E G F D I K J M O N L H
 The children of any node in a binary tree 
are ordered into a left child and a right 
child 
 A node can have a left and 
a right child, a left child 
only, a right child only, 
or no children 
 The tree made up of a left 
child (of a node x) and all its 
descendents is called the left subtree of 
x 
 Right subtrees are defined similarly 
7 
8 9 
10 
1 
3 
11 
5 
4 6 
7 
12
 Assume: visiting a node 
is printing its label 
 Preorder: 
1 3 5 4 6 7 8 9 10 11 12 
 Inorder: 
4 5 6 3 1 8 7 9 11 10 12 
 Postorder: 
4 6 5 3 8 11 12 10 9 7 1 
8 
1 
3 
8 9 
11 
5 
4 6 
7 
10 
12
 Assume: visiting a node 
is printing its data 
 Preorder: 15 8 2 6 3 7 
11 10 12 14 20 27 22 30 
 Inorder: 2 3 6 7 8 10 11 
12 14 15 20 22 27 30 
 Postorder: 3 7 6 2 10 14 
12 11 8 22 30 27 20 15 
9 
6 
15 
8 
2 
3 7 
11 
10 
14 
12 
20 
27 
22 30
 Observe the output of the inorder 
traversal of the BST example two slides 
earlier 
 It is sorted 
 This is no coincidence 
 As a general rule, if you output the keys 
(data) of the nodes of a BST using inorder 
traversal, the data comes out sorted in 
increasing order 
10

More Related Content

PPTX
Linked list
PPT
Queue implementation
PPT
standard template library(STL) in C++
PDF
Data structure using c++
PDF
Linked list implementation of Queue
PPTX
trees in data structure
PPTX
Trees (data structure)
PPTX
Binary Tree Traversal
Linked list
Queue implementation
standard template library(STL) in C++
Data structure using c++
Linked list implementation of Queue
trees in data structure
Trees (data structure)
Binary Tree Traversal

What's hot (20)

PPT
Graph traversal-BFS & DFS
PPT
Singly link list
PPTX
Red black trees
PDF
Expression trees
PPT
Linked lists
PPTX
Data Structures - Lecture 7 [Linked List]
PPTX
Avl trees
PPT
Data Structure and Algorithms Binary Search Tree
PDF
Applications of stack
PPTX
PDF
Double ended queue
PPTX
linked list in data structure
PPTX
Bfs and Dfs
PPT
PDF
Binary tree
PDF
Namespaces
PPSX
Data Structure (Queue)
PPT
Binary search tree(bst)
PPT
Data Structures- Part5 recursion
PPTX
Doubly Linked List
Graph traversal-BFS & DFS
Singly link list
Red black trees
Expression trees
Linked lists
Data Structures - Lecture 7 [Linked List]
Avl trees
Data Structure and Algorithms Binary Search Tree
Applications of stack
Double ended queue
linked list in data structure
Bfs and Dfs
Binary tree
Namespaces
Data Structure (Queue)
Binary search tree(bst)
Data Structures- Part5 recursion
Doubly Linked List
Ad

Viewers also liked (20)

PPT
Binary tree
PDF
Tree and binary tree
PPTX
Binary Search Tree in Data Structure
PDF
computer notes - Traversal of a binary tree
PPT
(Binary tree)
PPTX
Trees data structure
PPT
1.5 binary search tree
PPT
Binary Search Tree
PDF
Binary tree
PPTX
Tree in data structure
PPTX
Tree (Data Structure & Discrete Mathematics)
PPT
PPTX
Inorder traversal
PPTX
DOCX
Project 2- traversing
PPT
binary tree
DOCX
Assignment 2 - Traverse
PPTX
Tree Traversals (In-order, Pre-order and Post-order)
PDF
Discrete-Chapter 10 Trees
Binary tree
Tree and binary tree
Binary Search Tree in Data Structure
computer notes - Traversal of a binary tree
(Binary tree)
Trees data structure
1.5 binary search tree
Binary Search Tree
Binary tree
Tree in data structure
Tree (Data Structure & Discrete Mathematics)
Inorder traversal
Project 2- traversing
binary tree
Assignment 2 - Traverse
Tree Traversals (In-order, Pre-order and Post-order)
Discrete-Chapter 10 Trees
Ad

Similar to Traversals | Data Structures (20)

PPTX
Bca ii dfs u-3 tree and graph
PDF
Unit iv data structure-converted
PPTX
Unit-VStackStackStackStackStackStack.pptx
PDF
Lecture notes data structures tree
PPTX
Bsc cs ii dfs u-3 tree and graph
PPTX
Mca iii dfs u-4 tree and graph
PPT
Data Structures- Part9 trees simplified
PPTX
Tree data structure
PPTX
Creating a Binary tree from a General Tree.pptx
PPTX
Lecture_10 - Revised.pptx
PPT
Binary trees
PPTX
Data structure
PPT
BINARY SEARCH TREE
PPTX
learn tree, linked list, queue, stack, and other algo
PPTX
UNIT 2 TREES & GRAPH COMPLETE NOTES OF DATA STRUCTURE
PPTX
Presentation tree traversal
PPT
Tree and Binary Search tree
PDF
LEC 6-DS ALGO(updated).pdf
Bca ii dfs u-3 tree and graph
Unit iv data structure-converted
Unit-VStackStackStackStackStackStack.pptx
Lecture notes data structures tree
Bsc cs ii dfs u-3 tree and graph
Mca iii dfs u-4 tree and graph
Data Structures- Part9 trees simplified
Tree data structure
Creating a Binary tree from a General Tree.pptx
Lecture_10 - Revised.pptx
Binary trees
Data structure
BINARY SEARCH TREE
learn tree, linked list, queue, stack, and other algo
UNIT 2 TREES & GRAPH COMPLETE NOTES OF DATA STRUCTURE
Presentation tree traversal
Tree and Binary Search tree
LEC 6-DS ALGO(updated).pdf

More from Omair Imtiaz Ansari (7)

PDF
PPTX
Circular wait - Operating Systems
PPT
Sorting algos
PPT
Pointers - DataStructures
PPT
Processor simulator - Computer Archetecture
PPTX
Communication Skills
PPTX
Computer Archetecture & Organization
Circular wait - Operating Systems
Sorting algos
Pointers - DataStructures
Processor simulator - Computer Archetecture
Communication Skills
Computer Archetecture & Organization

Recently uploaded (20)

PDF
Assigned Numbers - 2025 - Bluetooth® Document
PPTX
Cloud computing and distributed systems.
PDF
Dropbox Q2 2025 Financial Results & Investor Presentation
PDF
The Rise and Fall of 3GPP – Time for a Sabbatical?
PDF
Review of recent advances in non-invasive hemoglobin estimation
PPTX
Machine Learning_overview_presentation.pptx
PDF
Architecting across the Boundaries of two Complex Domains - Healthcare & Tech...
PDF
Reach Out and Touch Someone: Haptics and Empathic Computing
PDF
Mobile App Security Testing_ A Comprehensive Guide.pdf
PPTX
Spectroscopy.pptx food analysis technology
PDF
Encapsulation theory and applications.pdf
PPTX
A Presentation on Artificial Intelligence
PDF
Agricultural_Statistics_at_a_Glance_2022_0.pdf
PDF
Building Integrated photovoltaic BIPV_UPV.pdf
PDF
Empathic Computing: Creating Shared Understanding
PDF
Per capita expenditure prediction using model stacking based on satellite ima...
PDF
Unlocking AI with Model Context Protocol (MCP)
PDF
NewMind AI Weekly Chronicles - August'25-Week II
PDF
Machine learning based COVID-19 study performance prediction
PDF
Electronic commerce courselecture one. Pdf
Assigned Numbers - 2025 - Bluetooth® Document
Cloud computing and distributed systems.
Dropbox Q2 2025 Financial Results & Investor Presentation
The Rise and Fall of 3GPP – Time for a Sabbatical?
Review of recent advances in non-invasive hemoglobin estimation
Machine Learning_overview_presentation.pptx
Architecting across the Boundaries of two Complex Domains - Healthcare & Tech...
Reach Out and Touch Someone: Haptics and Empathic Computing
Mobile App Security Testing_ A Comprehensive Guide.pdf
Spectroscopy.pptx food analysis technology
Encapsulation theory and applications.pdf
A Presentation on Artificial Intelligence
Agricultural_Statistics_at_a_Glance_2022_0.pdf
Building Integrated photovoltaic BIPV_UPV.pdf
Empathic Computing: Creating Shared Understanding
Per capita expenditure prediction using model stacking based on satellite ima...
Unlocking AI with Model Context Protocol (MCP)
NewMind AI Weekly Chronicles - August'25-Week II
Machine learning based COVID-19 study performance prediction
Electronic commerce courselecture one. Pdf

Traversals | Data Structures

  • 1.  The process of systematically visiting all the nodes in a tree and performing some computation at each node in the tree is called a tree traversal.
  • 2. H D B N L F J A C E G I K M O H B F N E G O
  • 3. Name for each Node: CODE public void displayTree(Node n){ if( n == null ) return; printf(n.data); displayTree(n.left); displayTree(n.right); } •Visit the node •Visit the left subtree, if any. •Visit the right subtree, if any. Preorder (N-L-R) public void displayTree(Node n){ if( n == null ) return; displayTree(n.left); printf(n.data); displayTree(n.right); } •Visit the left subtree, if any. Visit the node •Visit the right subtree, if any. Inorder (L-N-R) public void displayTree(Node n){ if( n == null ) return; displayTree(n.left); displayTree(n.right); printf(n.data); } •Visit the left subtree, if any. •Visit the right subtree, if any. •Visit the node Postorder (L-R-N)
  • 4. N-L-R H D B N L F J A C E G I K M O H D A C E L J I K N O
  • 5. L-N-R H D B N L F J A C E G I K M O A B C D E F G H I J K L M N O Note: An inorder traversal of a BST visits the keys sorted in increasing order.
  • 6. L-R-N H D B N L F J A C E G I K M O A C B E G F D I K J M O N L H
  • 7.  The children of any node in a binary tree are ordered into a left child and a right child  A node can have a left and a right child, a left child only, a right child only, or no children  The tree made up of a left child (of a node x) and all its descendents is called the left subtree of x  Right subtrees are defined similarly 7 8 9 10 1 3 11 5 4 6 7 12
  • 8.  Assume: visiting a node is printing its label  Preorder: 1 3 5 4 6 7 8 9 10 11 12  Inorder: 4 5 6 3 1 8 7 9 11 10 12  Postorder: 4 6 5 3 8 11 12 10 9 7 1 8 1 3 8 9 11 5 4 6 7 10 12
  • 9.  Assume: visiting a node is printing its data  Preorder: 15 8 2 6 3 7 11 10 12 14 20 27 22 30  Inorder: 2 3 6 7 8 10 11 12 14 15 20 22 27 30  Postorder: 3 7 6 2 10 14 12 11 8 22 30 27 20 15 9 6 15 8 2 3 7 11 10 14 12 20 27 22 30
  • 10.  Observe the output of the inorder traversal of the BST example two slides earlier  It is sorted  This is no coincidence  As a general rule, if you output the keys (data) of the nodes of a BST using inorder traversal, the data comes out sorted in increasing order 10