Traversing A Binary Tree
Traversing A Binary Tree
NAME:
DHYAN PATEL (21BT04070)
ARPIT BHIMANI (21BT04008)
HARSHIL KATHIRIYA (21BT04026)
What is Traversing a Binary tree?
Traversing a binary tree is the process of visiting each node in the tree exactly once in a systematic way. Unlike
linear data structures in which the elements are traversed sequentially, tree is a non-linear data structure in which
the elements can be traversed in many different ways. There are different algorithms for tree traversals. These
algorithms differ in the order in which the nodes are visited. In this section, we will discuss these algorithms.
Consider the tree given in Fig. 9.15. The pre-order traversal of the tree is given as A,B,C. Root node first,
the left sub-tree next, and then the right sub -tree. Pre-order traversal is also called as depth-first traversal.
In this algorithm, the left sub-tree is always traversed before the right sub -tree.
Pre Order Traversal
Pre-order algorithm is also known as the NLR traversal algorithm(Node-Left-Right). The
algorithm for pre-order traversal is shown in Fig. 9.16
Pre Order Traversal
Example:
● Since, the traversal scheme, we are using is pre-order traversal, therefore, the first element to be printed is 18.
● Traverse the left sub-tree recursively. The root node of the left sub-tree is 211, print it and move to left.
● Left is empty therefore print the right children and move to the right sub-tree of the root i.e 12.
● 20 is the root of sub-tree therefore, print it and move to its left. Since left sub-tree is empty therefore move to the
right and print the only element present there i.e. 190.
● Therefore, the printing sequence will be 18, 211,12, 20, 190.
2. In-Order Traversal
To traverse a non-empty binary tree in in-order, the following operations are performed recursively
at each node
The algorithm works by:
1.Traversing the left sub-tree,
2.Visiting the root node, and finally
3.Traversing the right sub-tree
Consider the tree given in Fig. 9.15. The in-order traversal of the tree is given as B,A, and C. Left sub-tree
first, the root node next, and then the right sub-tree. In-order traversal is also called as symmetric
traversal. In this algorithm, the left sub-tree is always traversed before the root node and the right
sub-tree. The algorithm for in-order traversal is shown in Fig. 9.17.
In-Order Traversal
Consider the tree given in Fig. 9.15. The post-order traversal of the tree is given as B,C,andA. Left sub-tree first,
the right sub-tree next,and finally the root node. In this algorithm, the left sub-tree is always traversed before the
right sub-tree and the root node
Post Order Traversal
Post-order algorithm is also known as the LRN traversal algorithm(Left-Right-Node). The algorithm for
post-order traversal is shown in Fig. 9.18. Post-order traversals are used to extract post fix notation from an
expression tree.
Post Order Traversal
Example : :
● Then traverses to the right subtree, which has left and right node, Node D has left node, i.e., F.
● As there is no right node, it will traverse back to the root node of the subtree, i.e., D.
● As there is no right node, it will traverse back to the root node of the subtree, i.e., C and checks if there is any
● Then traverses to the root node of the subtree, i.e., C., and then A, the main root node .
PostOrder Traversal: B F D E C A
.
Thank you