0% found this document useful (0 votes)
21 views15 pages

Tree Traversal

The document discusses different tree traversal algorithms including depth-first search (DFS) and breadth-first search (BFS). DFS algorithms include pre-order, in-order, and post-order traversal. The document provides details and examples of each traversal technique.

Uploaded by

aditya thote
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
21 views15 pages

Tree Traversal

The document discusses different tree traversal algorithms including depth-first search (DFS) and breadth-first search (BFS). DFS algorithms include pre-order, in-order, and post-order traversal. The document provides details and examples of each traversal technique.

Uploaded by

aditya thote
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 15

Tree Traversal

Traversal is a process to visit all the nodes of a tree and may print their values too

Tree Traversal Algorithms can be classified broadly in the following two categories
by the order in which the nodes are visited:

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

1. Pre-order Traversal (Root, Left ,


Right)
2. In-order Traversal (Left ,Root,
Right).
3. Post-order Traversal (Left , Right,
Root)

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.
Tree traversal techniques

Depth-First Search (DFS) Breadth-First Search (BFS)

Preorder Traversal

Inorder Traversal

Postorder Traversal
Traversal is a process to visit all the nodes of a tree and may print
their values too

Type of Tree Traversals :


1. Pre-order Traversal (Root, Left , Right)

2. In-order Traversal (Left ,Root, Right)

3. Post-order Traversal (Left , Right, Root)


Algorithm

Step 1 − Visit root node.


Step 2 − Recursively traverse left subtree.
Step 3 − Recursively traverse right subtree.

Until all nodes are traversed


Pre-order Traversal (Root, Left , Right)
In this traversal method, the root is visited first, then left
subtree and later the right sub-tree.

20
20,10,25
10 25

20,4 ,25,21,26

20

4 25

21 26
20 Root, Left , Right

4 25

3 5 21 26

20, 4, 3, 5, 25, 21, 26


20

25
4

3 5 21 26

Pre-order - 20, 4, 3, 5, 25, 21, 26


In-order Traversal
In this traversal method, the left subtree is visited first, then the
root and later the right sub-tree.

Left ,Root, Right


Left ,Root, Right

20
10,20,25
10 25

20 4,20,21,25,26

4 25

21 26
Inorder

20

25
4

3 5 21 26

3 4 5 20 21 25 26
20, 4, 3, 51, 25, 21, 26,2

20 Left ,Root, Right

4 51

25
3

21 26
2

Preorde- 20, 4,3,2,51,25,21,26


Inorder- 2,3,4,20,21 25,26,51
Post-order Traversal(Left , Right, Root)
In this traversal method, the left subtree is visited first, then the
right sub-tree and later the root.

we visit the left subtree and the right subtree before visiting the
current node in recursion

1. Go to left-subtree
2. Go to right-subtree
3. Visit Node
Left , Right, Root

20
10,25,20
10 25

20 4,21,26,25,20

4 25

21 26
Post-order
Leaf break from left side
20

25
4

3 5 21 26

3 ,5 ,4, 21,26,25,20

You might also like