Ds Mid 2
Ds Mid 2
Q1)Let G be a graph with n vertices and m edges. Find the tightest upper
bound on the runningtimeondepthfirstsearchofgraphG. Assume that the
graph is represented using an adjacency matrix.
A. Depth First Search of a graph takes O(m+n) time when the graph is
represented using an adjacency list. In adjacency matrix representation, the
graph is represented as an ‘n x n’ matrix. To do DFS, for every vertex, we
traverse the row corresponding to that vertex to find all adjacent vertices In
adjacency list representation we traverse only the adjacent vertices of the
vertex). Therefore time complexity becomes O(n2).
A.
Q7) Draw a directed graph with five vertices and seven edges.
Exactly one of the edges should be a loop, and do not have any
multiple edges.
A.
1.Initialize:
○Create an empty queue for level order traversal.
○Enqueue the root node along with its level (0).
2.Level Order Traversal:
○Initialize a variable to track the maximum level visited so far.
3.Traversal Loop:
○While the queue is not empty:
1.Dequeue the front node and its level from the queue.
2.If the current level is greater than the maximum level
visited so far:
■Print the node (this is the first node at this level).
■Update the maximum level visited.
3.Enqueue the left and right children of the node along
with their levels (current level + 1).
Q10) Given a binary tree, write a recursive solution to traverse the
tree using post order traversal.
A. Algorithm: Post-Order Traversal of a Binary Tree
Post-order traversal involves visiting the left subtree, then the right
subtree, and finally the root node. Here's a step-by-step recursive
algorithm for postorder traversal:
Steps:
1.Base Case: If the current node is null, return.
2.Recursive Case:
○Recursively traverse the left subtree.
○Recursively traverse the right subtree.
○Visit the root node (process the current node).
PART-B
Q1) ConstructaBinarySearch Treeforthefollowingdata and
doin-order,Preorder andPost-ordertraversalof thetree.
50,60,25,40,30, 70,35,10,55,65,5
A. Lec-56: Preorder, Inorder and Postorder in 5 minute | Tree Tr…
Q2) Explain the breadth first search and depth first search tree
traversal on the following graph.
A.
REFERENCE
5.1 Graph Traversals - BFS & DFS -Breadth First Search and D…
●Pre-Order: 1,2,4,5,3,61, 2, 4, 5, 3, 6
●In-Order: 4,2,5,1,3,64, 2, 5, 1, 3, 6
●Post-Order: 4,5,2,6,3,1
Q6) Write the in-order, pre-order and post-order traversals for the
given binary tree.
A. Adjacency Matrix:
● Adjacency Matrix is a 2D array of size V x V where V is the number
of vertices in a graph.
● Let the 2D array be adj[ ][ ], a slot adj[i][j] = 1 indicates that there is
an edge from vertex i to vertex j.
● Adjacency matrix for an undirected graph is always symmetric.
Adjacency Matrix is also used to represent weighted graphs. If
adj[i][j] = w, then there is an edge from vertex i to vertex j with weight
w.
Q8) Explain the array and linked representation of a binary tree using
a suitable example?
A. LINK:Data Structures Tutorials - Binary Tree Representations with
an example
Q9) Define a binary tree? Construct a binary tree given the pre-order
traversal and in-order traversals as follows:
Pre-Order Traversal: G B Q A C K F P D E R H
In-Order Traversal: Q B K C F A G P E D H R
A. A binary tree is a tree data structure in which each node has at
most two children, which are referred to as the left child and the right
child.
5.7 Construct Binary Tree from Preorder and Inorder Traversal |…
Q12) Write the basic tree terminologies and the properties of binary
trees?
A.
Q13) Explain the breadth first search and depth first search graph
traversal algorithms for the following graph?
Q14) Explain the following with an example: i. Full binary tree ii.
Strictly binary tree iii. Complete binary tree
A.
● If every non leaf node in a binary tree has nonempty left and right
subtrees, the tree is called a strictly binary tree.
● A strictly binary tree with n leaves always contains 2n 1 nodes.
A.
BFS: M R Q N P O
Q17) Define a binary search tree and write the properties of a binary
search tree? Construct a binary search with the following keys: 8, 3,
1, 6, 14, 4, 7, 13, 17, 5
Q18) Write the procedure for finding an element 85in a given binary
search tree?
A.
class Graph {
private int vertices; // Number of vertices
private LinkedList<Integer> adjList[]; // Adjacency List
// Constructor
Graph(int v) {
vertices = v;
adjList = new LinkedList[v];
for (int i = 0; i < v; ++i) {
adjList[i] = new LinkedList<>();
}
}
visited[startVertex] = true;
queue.add(startVertex);
while (queue.size() != 0) {
startVertex = queue.poll();
System.out.print(startVertex + " ");
Iterator<Integer> i = adjList[startVertex].listIterator();
while (i.hasNext()) {
int n = i.next();
if (!visited[n]) {
visited[n] = true;
queue.add(n);
}
}
}
}
g.addEdge(0, 1);
g.addEdge(0, 2);
g.addEdge(1, 2);
g.addEdge(2, 0);
g.addEdge(2, 3);
g.addEdge(3, 3);
g.BFS(2);
}
}
Q20) Write the in-order, pre-order and post-order traversal of a given
tree?
A.