Tree Traversal
Tree Traversal
on
DATA STRUCTURES
2021 – 2022
Sitapur Road,Lucknow
Uttar Pradesh
India
Pin Code: 226021
INSTITUTE OF ENGINEERING
&TECHNOLOGY
Disclaimer: The e-content is exclusively meant for academic purposes and for enhancing teaching
and learning. Any other use for economic/commercial purpose is strictly prohibited. The users of the
content shall not distribute, disseminate or share it with anyone else and its use is restricted to
advancement of individual knowledge.
The information provided in this e-content is developed from authentic references, to the best of my
knowledge
1. Preorder Traversal
2. Postorder Traversal
3. Inorder Traversal
1. Preorder Traversal
Algorithm for preorder traversal
Step 2 : A + B + D (E + F) + C (G + H)
Step 3 : A + B + D + E + F + C + G + H
Preorder Traversal : A B C D E F G H
Example 2
Consider the following example-
Traverse the entire tree starting from the root node keeping
yourself to the left.
Applications-
Preorder traversal is used to get prefix expression of an expression tree.
Preorder traversal is used to create a copy of the tree.
Step 1 : As we know, preorder traversal starts from left subtree (last leaf) ((Postorder on E + Postorder
on F) + D + B )) + ((Postorder on G + Postorder on H) + C) + (Root A)
Step 2 : (E + F) + D + B + (G + H) + C + A
Step 3 : E + F + D + B + G + H + C + A
Postorder Traversal : E F D B G H C A
Applications-
Step 3 : B + E + D + F + A + G + C + H
Keep a plane mirror horizontally at the bottom of the tree and take the
projection of all the nodes.
Application-
Inorder traversal is used to get infix expression of an expression tree.
Breadth First Traversal of a tree prints all the nodes of a tree level by level.
Breadth First Traversal is also called as Level Order Traversal.
Example-
Application-
Level order traversal is used to print the data in the same order as stored in the array representation of
a complete binary tree.
Example 2:
Traverse the following binary tree in pre, post, inorder and level order.