0% found this document useful (0 votes)
4 views21 pages

Daa Unit-4 Notes (Aiml - Ai&Ds) Chapter-II

The document outlines basic tree traversal techniques including Pre-order, In-order, and Post-order traversals, each with their respective algorithms and time complexities. It also discusses two fundamental search techniques for graphs: Breadth-First Search (BFS) and Depth-First Search (DFS), detailing their implementations using queue and stack data structures respectively. Both BFS and DFS result in a spanning tree as the final output.

Uploaded by

rkhyathipradha3
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)
4 views21 pages

Daa Unit-4 Notes (Aiml - Ai&Ds) Chapter-II

The document outlines basic tree traversal techniques including Pre-order, In-order, and Post-order traversals, each with their respective algorithms and time complexities. It also discusses two fundamental search techniques for graphs: Breadth-First Search (BFS) and Depth-First Search (DFS), detailing their implementations using queue and stack data structures respectively. Both BFS and DFS result in a spanning tree as the final output.

Uploaded by

rkhyathipradha3
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/ 21

BASIC TRAVERSAL AND SEARCH TECHNIQUES

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

Step 1: At first the root will be visited, i.e. node 1.

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.

12
Step 3: Again the left subtree of node 2 is traversed and the root of that subtree i.e., node 4 is visited.
124
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.

1245
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.

12453
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.

124536s

The output of pre-order traversal of this tree will be: 1 → 2 → 4 → 5 → 3 → 6


Time Complexity: O(N) where N is the total number of nodes. Because it traverses all the nodes at least
once.
In-order Traversal: In-order traversal is a tree traversal technique where the left subtree is visited first,
followed by the root node, and then the right subtree. (Formula is L-N-R)
1. Traverse the left subtree.
2. Visit the root node.
3. Traverse the right subtree.
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 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.

42
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.

425
Step 4: As the left subtree of node 1 is, the root itself, i.e., node 1 will be visited.

4251
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.

42513
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.

425136
The output of pre-order traversal of this tree will be: 425136
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.

45
Step 3: Now both the left and right subtrees of node 2 are visited. So now visit node 2 itself.

452
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.
4526
Step 5: All the subtrees of node 3 are traversed. So now node 3 is visited.

45263
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.

452631
So the order of traversal of nodes is 452631
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.

We use the following steps to implement BFS traversal...

 Step 1 - Define a Queue of size total number of vertices in the graph.


 Step 2 - Select any vertex as starting point for traversal. Visit that vertex and insert it into the Queue.
 Step 3 - Visit all the non-visited adjacent vertices of the vertex which is at front of the Queue and
insert them into the Queue.
 Step 4 - When there is no new vertex to be visited from the vertex which is at front of the Queue then
delete that vertex.
 Step 5 - Repeat steps 3 and 4 until queue becomes empty.
 Step 6 - When queue becomes empty, then produce final spanning tree by removing unused edges
from the graph

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

 Step 1 - Define a Stack of size total number of vertices in the graph.


 Step 2 - Select any vertex as starting point for traversal. Visit that vertex and push it on to the
Stack.
 Step 3 - Visit any one of the non-visited adjacent vertices of a vertex which is at the top of stack and
push it on to the stack.
 Step 4 - Repeat step 3 until there is no new vertex to be visited from the vertex which is at the top of
the stack.
 Step 5 - When there is no new vertex to visit then use back tracking and pop one vertex from the
stack.
 Step 6 - Repeat steps 3, 4 and 5 until stack becomes Empty.
 Step 7 - When stack becomes Empty, then produce final spanning tree by removing unused edges
from the graph

5
CONNECTED AND BICONNECTED COMPONENTS:

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy