Binary Tree-
Â
Before you go through this article, make sure that you gone through the previous article on Binary Trees.
Â
We have discussed-
- Binary tree is a special tree data structure.
- In a binary tree, each node can have at most 2 children.
Â
In this article, we will discuss about Binary Tree Traversal.
Â
Tree Traversal-
Â
| Tree Traversal refers to the process of visiting each node in a tree data structure exactly once. |
Â
Various tree traversal techniques are-
Â

Â
Depth First Traversal-
Â
Following three traversal techniques fall under Depth First Traversal-
- Preorder Traversal
- Inorder Traversal
- Postorder Traversal
Â
1. Preorder Traversal-
Â
Algorithm-
Â
- Visit the root
- Traverse the left sub tree i.e. call Preorder (left sub tree)
- Traverse the right sub tree i.e. call Preorder (right sub tree)
Â
Root → Left → Right
Â
Example-
Â
Consider the following example-
Â

Â
Preorder Traversal Shortcut 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.
Â
2. Inorder Traversal-
Â
Algorithm-
Â
- Traverse the left sub tree i.e. call Inorder (left sub tree)
- Visit the root
- Traverse the right sub tree i.e. call Inorder (right sub tree)
Â
Left → Root → Right
Â
Example-
Â
Consider the following example-
Â

Â
Inorder Traversal Shortcut 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.
Â
3. Postorder Traversal-
Â
Algorithm-
Â
- Traverse the left sub tree i.e. call Postorder (left sub tree)
- Traverse the right sub tree i.e. call Postorder (right sub tree)
- Visit the root
Â
Left → Right → Root
Â
Example-
Â
Consider the following example-
Â

Â
Postorder Traversal Shortcut Pluck all the leftmost leaf nodes one by one. Â
|
Â
Applications-
Â
- Postorder traversal is used to get postfix expression of an expression tree.
- Postorder traversal is used to delete the tree.
- This is because it deletes the children first and then it deletes the parent.
Â
Breadth First Traversal-
Â
- 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.
Â
To gain better understanding about Tree Traversal,
Â
Also Read- Binary Tree Properties
Â
PRACTICE PROBLEMS BASED ON TREE TRAVERSAL-
Â
Problem-01:
Â
If the binary tree in figure is traversed in inorder, then the order in which the nodes will be visited is ____?
Â

Â
Solution-
Â
The inorder traversal will be performed as-
Â

Â
Problem-02:
Â
Which of the following sequences denotes the postorder traversal sequence of the tree shown in figure?
Â

Â
- FEGCBDBA
- GCBDAFE
- GCDBFEA
- FDEGCBA
Â
Solution-
Â
Perform the postorder traversal by plucking all the leftmost leaf nodes one by one.
Then,
Postorder Traversal :Â G , C , D , B , F , E , A
Â
Thus, Option (C) is correct.
Â
Problem-03:
Â
Let LASTPOST, LASTIN, LASTPRE denote the last vertex visited in a postorder, inorder and preorder traversal respectively of a complete binary tree. Which of the following is always true?
- LASTIN = LASTPOST
- LASTIN = LASTPRE
- LASTPRE = LASTPOST
- None of these
Â
Solution-
Â
Consider the following complete binary tree-
Â

Â
Preorder Traversal : B , A , E
Inorder Traversal   : B , A , E
Postorder Traversal : B , E , A
Â
Clearly, LASTIN = LASTPRE.
Thus, Option (B) is correct.
Â
Problem-04:
Â
Which of the following binary trees has its inorder and preorder traversals as BCAD and ABCD respectively-
Â

Â
Solution-
Â
Option (D) is correct.
Â
To watch video solutions and practice more problems,
Â
Download Handwritten Notes Here-
Â
Â
Next Article- Binary Search Trees
Â
Get more notes and other study material of Data Structures.
Watch video lectures by visiting our YouTube channel LearnVidFun.























