Data Structures (Binary Trees)
Data Structures (Binary Trees)
Data Structures (Binary Trees)
STRUCTURES
Instructor:
Ms.Dur-e-Shawar Agha
ROAD MAP
Introduction to Binary Tree
Tree Traversals
Binary Tree
1. Depth-First Search (DFS) Algorithm: It starts with the root node and first visits all
nodes of one branch as deep as possible of the chosen Node and before
backtracking, it visits all other branches in a similar fashion. There are three sub-
types under this, which we will cover in this article.
• In-order Traversal
• Pre-order Traversal
• Post-order Traversal
Contd….
2. Breadth-First Search (BFS) Algorithm: It also starts from the root node and visits all
nodes of current depth before moving to the next depth in the tree. We will cover
one algorithm of BFS type in the upcoming section.
• Level-order Traversal
1. Depth First search (DFS)
Preorder Traversal
• Algorithm
i. Visit the root
ii.Traverse the left sub tree i.e. call Preorder (left sub tree)
iii.Traverse the right sub tree i.e. call Preorder (right sub tree)
Root → Left → Right
Contd….
Inorder Traversal
• Algorithm
i. Traverse the left sub tree i.e. call Inorder (left sub tree)
ii. Visit the root
iii. Traverse the right sub tree i.e. call Inorder (right sub tree)
Postorder Traversal
• Algorithm
i. Traverse the left sub tree i.e. call Postorder (left sub tree)
ii. Traverse the right sub tree i.e. call Postorder (right sub tree)
iii.Visit the root
Level-Order Traversal-
Breadth First Traversal of a tree prints all the nodes of a tree level by level.
Example
Implementation of Binary Tree
The expression tree is a binary tree in which each internal node corresponds to the
operator and each leaf node corresponds to the operand.
Example
a + (b * c) + d * (e + f)
Activity#01
If the binary tree in figure is traversed in inorder, preorder and postorder then the order in
which the nodes will be visited is ____?
Activity#02
a. (3 * 7) + 9
c. (NOT A) AND (B OR C)
SUMMARY
Introduction to Binary Tree
Tree Traversal