Data Structures Module 4 QB Complete Solutions
Data Structures Module 4 QB Complete Solutions
PART A
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. 2h-1 + 1
Let there be n(h) nodes at height h.
In a perfect tree where every node has exactly
two children, except leaves, following recurrence holds.
n(h) = 2n(h-1) + 1
In given case, the numbers of nodes are two less, therefore
n(h) = 2n(h-1) + 1 - 2
= 2n(h-1) - 1
The above equation is only satisfied by 2h-1 + 1
A.
A.
8. Given A Binary Tree. Write an efficient algorithm to delete the
entire binary tree.
A. To delete a tree, we must traverse all the nodes of the tree and
delete them one by one. We should use the postorder transversal
because before deleting the parent node, we should delete its child
nodes first.
Algorithm Postorder Tree Deletion:
1. Traverse the left subtree, i.e., call Postorder(left-subtree)
2. Traverse the right subtree, i.e., call Postorder(right-subtree)
3. Visit the root and delete the node
class Node:
def __init__(self, data):
self.data = data
self.left = None
self.right = None
def leftView(root):
max_level = [0]
leftViewUtil(root, 1, max_level)
2. 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 De…
3. Question Incomplete
class Graph:
adj = []
# Function to fill empty adjacency matrix
def __init__(self, v, e):
self.v = v
self.e = e
Graph.adj = [[0 for i in range(v)]
for j in range(v)]
LINK:
https://www.geeksforgeeks.org/implementation-of-dfs-using-adjac
ency-matrix/
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.
12. Write the basic tree terminologies and the properties of binary
trees?
13. Explain the breadth first search and depth first search graph
traversal algorithms for the following graph?
BFS : A B C E G F D H
DFS: A B C D E H G F
14. Explain the following with an example: i. Full binary tree ii.
Strictly binary tree iii. Complete binary tree
● 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
17. 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
18. Write the procedure for finding an element 85 in a given binary
search tree?
A.
● The element to be searched is 85.
● Compare 85 with the root node.
● Since 85>33 we can move to the right subtree.
● Compare 85 with 60. Since 85> 60 we can move to the right
subtree. Since 85<90 we can move to the left subtree.
● Since 85<86 we can move to the left subtree.
● 85> 81 but 81 has no children hence 85 is not present in the
binary tree.
LINK:
https://www.geeksforgeeks.org/breadth-first-search-or-bfs-for-a-g
raph/
A.
PART C
Preorder Traversal
Preorder Traversal is another variant of DFS. Where atomic
operations in a recursive function, are as same as Inorder traversal
but with a different order.
Here, we visit the current node first and then goes to the left
sub-tree. After covering every node of the left sub-tree, we will
move towards the right subtree and visit in a similar fashion.
Order of the steps will be like…
1. Visit Node
2. Go to left-subtree
3. Go to right-subtree
Postorder Traversal
Where we visit the left subtree and the right subtree before visiting
the current node in recursion.
So, the sequence of the steps will be
1. Go to left-subtree
2. Go to right-subtree
3. Visit Node
Figure
As in the example given above, the BFS algorithm traverses from A
to B to E to F first then to C and G lastly to D. It employs the
following rules.
● Rule 1 − Visit the adjacent unvisited vertex. Mark it as visited.
Display it. Insert it in a queue.
● Rule 2 − If no adjacent vertex is found, remove the first vertex
from the queue.
● Rule 3 − Repeat Rule 1 and Rule 2 until the queue is empty.
class Node:
def __init__(self, data=None, left=None, right=None):
self.data = data
self.left = left
self.right = right
# Recursive function to perform preorder traversal on
tree
def preorder(root):
A binary tree is said to be a complete binary tree when all the levels
are completely filled except the last level, which is filled from the
left.
8. Write the time complexity for finding the height of the binary
tree?
Ans. O(n). A breadth first search from the root is probably the
fastest way to do this. You only need to visit each node once, and,
owing to the simple structure of a binary tree, you never have to
keep track of visited nodes or anything like that. You don't have to
even track the “deepest node seen so far” because you do an entire
tier at a time with BFS.
Incidence Matrix:
In Incidence matrix representation, graph can be represented using
a matrix of size:
Total number of vertices by total number of edges. It means if a
graph has 4 vertices and 6 edges, then it can be represented using
a matrix of 4X6 class. In this matrix, columns represent edges and
rows represent vertices. This matrix is filled with either 0 or 1 or -1.
Where,
● 0 is used to represent the row edge which is not connected to
the column vertex.
● 1 is used to represent the row edge which is connected as the
outgoing edge to the column vertex.
● -1 is used to represent the row edge which is connected as
incoming edge to column vertex.
Example
Consider the following directed graph representation.
Adjacency List:
Adjacency list is a linked representation.
In this representation, for each vertex in the graph, we maintain the
list of its neighbours. It means, every vertex of the graph contains a
list of its adjacent vertices .We have an array of vertices which is
indexed by the vertex number and for each vertex v, the
corresponding array element points to a singly linked list of
neighbours of v.
Example
Let's see the following directed graph representation implemented
using linked list:
Basic Operations
Following are the basic operations of a tree −
• Search − Searches an element in a tree.
• Insert − Inserts an element in a tree.
• Pre-order Traversal − Traverses a tree in a pre-order manner.
• In-order Traversal − Traverses a tree in an in-order manner.
• Post-order Traversal − Traverses a tree in a post-order
manner.