Daa Unit-4 Notes (Aiml - Ai&Ds) Chapter-II
Daa Unit-4 Notes (Aiml - Ai&Ds) Chapter-II
TRAVERSAL TECHNIQUES (or) BINARY TREES TRAVERSAL: Any process for visiting all of the
nodes in some order is called a traversal (or) the process of visiting or accessing each node of the tree
exactly once in a certain order. Tree traversal algorithms help us to visit and process all the nodes of the tree.
We cannot randomly access a node in a tree. There are three ways which we use to traverse a tree
Pre-order Traversal
In-order Traversal
Post-order Traversal
Pre-order Traversal: Pre-order traversal is a tree traversal technique where the root node is visited first,
followed by the left subtree, and then the right subtree. (Formula is N-L-R)
1. Visit the root node.
2. Traverse the left subtree.
3. Traverse the right subtree.
Example: Let us understand the algorithm with the below example tree
1
Step 2: After this, traverse in the left subtree. Now the root of the left subtree is visited i.e., node 2 is visited.
12
Step 3: Again the left subtree of node 2 is traversed and the root of that subtree i.e., node 4 is visited.
124
Step 4: There is no subtree of 4 and the left subtree of node 2 is visited. So now the right subtree of node 2
will be traversed and the root of that subtree i.e., node 5 will be visited.
1245
Step 5: The left subtree of node 1 is visited. So now the right subtree of node 1 will be traversed and the root
node i.e., node 3 is visited.
12453
Step 6: Node 3 has no left subtree. So the right subtree will be traversed and the root of the subtree i.e., node
6 will be visited. After that there is no node that is not yet traversed. So the traversal ends.
124536s
Step 1: The traversal will go from 1 to its left subtree i.e., 2, then from 2 to its left subtree root, i.e., 4. Now 4
has no left subtree, so it will be visited. It also does not have any right subtree. So no more traversal from 4
4
Step 2: As the left subtree of 2 is visited completely, now it read data of node 2 before moving to its right
subtree.
42
Step 3: Now the right subtree of 2 will be traversed i.e., move to node 5. For node 5 there is no left subtree,
so it gets visited and after that, the traversal comes back because there is no right subtree of node 5.
425
Step 4: As the left subtree of node 1 is, the root itself, i.e., node 1 will be visited.
4251
Step 5: Left subtree of node 1 and the node itself is visited. So now the right subtree of 1 will be traversed
i.e., move to node 3. As node 3 has no left subtree so it gets visited.
42513
Step 6: The left subtree of node 3 and the node itself is visited. So traverse to the right subtree and visit node
6. Now the traversal ends as all the nodes are traversed.
425136
The output of pre-order traversal of this tree will be: 425136
Time Complexity: O(N) where N is the total number of nodes. Because it traverses all the nodes at least
once.
Post-order Traversal: Post-order traversal is a tree traversal technique where the left subtree and right
subtree are visited first, followed by the root node.
1. Traverse the left subtree.
2. Traverse the right subtree.
3. Visit the root node.
Example: Let us understand the algorithm with the below example tree
Step 1: The traversal will go from 1 to its left subtree i.e., 2, then from 2 to its left subtree root, i.e., 4. Now 4
has no subtree, so it will be visited.
4
Step 2: As the left subtree of 2 is visited completely, now it will traverse the right subtree of 2 i.e., it will
move to 5. As there is no subtree of 5, it will be visited.
45
Step 3: Now both the left and right subtrees of node 2 are visited. So now visit node 2 itself.
452
Step 4: As the left subtree of node 1 is traversed, it will now move to the right subtree root, i.e., 3. Node 3
does not have any left subtree, so it will traverse the right subtree i.e., 6. Node 6 has no subtree and so it is
visited.
4526
Step 5: All the subtrees of node 3 are traversed. So now node 3 is visited.
45263
Step 6: As all the subtrees of node 1 are traversed, now it is time for node 1 to be visited and the traversal
ends after that as the whole tree is traversed.
452631
So the order of traversal of nodes is 452631
Time Complexity: O(N) where N is the total number of nodes. Because it traverses all the nodes at least
once.
SEARCH TECHNIQUES (or)
The two fundamental graph / tree search techniques are
1. Breadth-First Search (BFS)
2. Depth-first search (DFS)
Breadth-First Search (BFS): This algorithm explores a graph by visiting all the neighboring nodes of a
given level before moving to the next level. BFS uses a queue data structure to maintain a level-wise
exploration order.BFS algorithm starts at the tree root and explores all nodes at the present depth prior to
moving on to the nodes at the next depth level. BFS (Breadth First Search)
BFS traversal of a graph produces a spanning tree as final result. Spanning Tree is a graph without loops.
We use Queue data structure with maximum size of total number of vertices in the graph to implement BFS
traversal.
Example
DFS (Depth First Search)
DFS traversal of a graph produces a spanning tree as final result. Spanning Tree is a graph without loops.
We use Stack data structure with maximum size of total number of vertices in the graph to implement DFS
traversal.
We use the following steps to implement DFS traversal...
5
CONNECTED AND BICONNECTED COMPONENTS: