Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
17 views

Traversing A Binary Tree

This document discusses different algorithms for traversing binary trees, including pre-order, in-order, and post-order traversals. It provides details on how each algorithm works by visiting nodes in a tree in a specific order, examples of applying each algorithm to sample trees, and illustrations of the traversal processes.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
17 views

Traversing A Binary Tree

This document discusses different algorithms for traversing binary trees, including pre-order, in-order, and post-order traversals. It provides details on how each algorithm works by visiting nodes in a tree in a specific order, examples of applying each algorithm to sample trees, and illustrations of the traversal processes.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 12

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.

4 different algorithms for tree traversals:


1. Pre order traversal
2. In order traversal
3. Post order traveraal
1. Pre Order Traversal
To traverse a non-empty binary tree in pre-order, the following operations are performed recursively at each
node. The algorithm works by:
1.Visiting the root node,
2.Traversing the left sub-tree, and finally
3.Traversing the right sub-tree

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

In-order algorithm is also known as the LNR traversal algorithm (Left-Node-Right).


In-Order Tarversal
Example:
● Print the left most node of the left sub-tree i.e. 23.
● Print the root of the left sub-tree i.e. 211.
● Print the right child i.e. 89.
● Print the root node of the tree i.e. 18.
● Then, move to the right sub-tree of the binary tree and print the left most node i.e. 10.
● Print the root of the right sub-tree i.e. 20.
● Print the right child i.e. 32.
● Hence, the printing sequence will be 23, 211,89, 18, 10, 20, 32.
3. Post Order Traversal
To traverse a non-empty binary tree in post-order, the following operations are performed recursively at each
node. The algorithm works by:
1.Traversing the left sub-tree,
2.Traversing the right sub-tree
3.Visiting the root node.

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 : :

● The first element printed is B, as it is the left node.

● 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

right node, i.e., E.

● 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

You might also like