Data Structure II (Tree & Graphs) Basic Solution
Data Structure II (Tree & Graphs) Basic Solution
1. A perfect binary tree has every leaf on the same level, and every non-leaf node
has two children. A perfect binary tree with k leaves contains how many
nodes?
(a) k (b) k2
(c)log2 k (d) 2k -1
Answer: D
Solution:
Maximum number of nodes in perfect binary tree = 20 + 21 + 22 + ⋯ + 2𝑚 = 2𝑚 +1 − 1.
2. A complete n-ary tree is one in which every node has 0 or n children. If x is the
number of internal nodes of a complete n-ary tree, the number of leaves in it is
given by
(a) x (n – 1) + 1 (b) xn – 1
(c) xn + 1 (d) x (n + 1)
Answer: A
Solution: If we take n = 2 then we have a complete binary tree in which every
node has 0 or 2 children. In this case, if the number of internal nodes is x then
number of leaves = x + 1. So, option A is satisfied.
3. A k-ary tree is a tree in which every node has at most k children. In a k-ary
tree with n nodes and height h, which of the following is an upper bound for
the maximum number of leaves node?
𝑛
(a) 𝑙𝑜𝑔𝑘 𝑛 (b)
𝑙𝑜𝑔 𝑘 𝑛
(c)𝑘 (d)𝑘
Answer: C
Solution:
4. A Binary tree in which every non-leaf node has non-empty left and right sub
trees is called a strictly binary tree. Such a tree with 10 leaves
(a) has 19 nodes (b)has 16 nodes
(c)has 15 nodes (d) has 20 nodes
Solution:
Strictly Binary tree with 10 leaves
5. What is the minimum and maximum height of a binary tree that contains 100
nodes? (Assume root at height 0)
(a) Min: 10; Max 99
(b) Min: 6; Max 50
(c) Min: 10; Max 50
(d)Min: 6; Max 99
Solution:
To be minimum height, tree should be complete binary tree.
log 2 𝑛 + 1 − 1 𝑤𝑒𝑟𝑒 𝑒𝑖𝑔𝑡 𝑜𝑓 𝑟𝑜𝑜𝑡 = 0
In this case height =
log 2 𝑛 + 1 , 𝑟𝑜𝑜𝑡 = 1
Minimum height
= log 2 101 − 1 =7 – 1 =6
To be maximum height, tree should be chain,
Maximum height =100-1 =99
Answer is D
6. The level of a node is the length of the path (or number of edges) from the root
to that node. The level of a tree is equal to the level of its deepest leaf. A binary
tree has level k. Which represents
1. The maximum possible number of nodes, and
2. The minimum possible number of nodes in the tree
(a) 2k+1 , 2k + 1
(b) 2 k+1 , k
7. If you know that a tree is a BST, which of the following is always sufficient to
reconstruct it?
i) Pre-order traversal
ii) In-order traversal
iii) Post-order traversal
iv)Level-ordertraversal
(a) i & ii both
(b) ii & iii both
(c) iv only
(d) Either of i or iii
(a) 25 → 27
(b) 27 → 11
(c) 11 → 7
(d) 7 → 9
Answer: B
13. The in-order traversal of a binary tree is HFIEJGZ, and the post-order traversal
of the same tree is HIFJZGE. What will be the total number of nodes in the left
sub tree of root of the given tree?
(L Rt R)
Pre order: E F H I G J Z
(Rt L R)
Number of nodes of left – subtree of root
=3
Answer is C
14. Consider a Binary Tree. Let A and B be two nodes in the tree. It is given that B
is the inorder successor of A, and A has 2 children. Which of the following is
true about B?
(A) B has no right child
(B) B has no left child
(C) B has both children
(D) None of the above
(2017-10)
Answer: B
Solution:
Since B is successor of A, then definitely B has no left child as if it does have a left
child then in that case B won‟t be successor of A as in inorder traversal, we follow
Left Node Right approach and if left child does exist then in that case that left child
will be inorder successor of A and not B.
(A) i only (B) ii only (C) iii only (D) i and ii only
Answer: i, iii
Solution: (i) It doesn’t represent AVL tree because 6 should not be right childe
of 7.
19. How many binary trees with 3 nodes, A, B, and C when traversed in post-order
will give the sequence A, B, C?
(a) 3
(b) 7
(c) 5
(d) 4
Solution: Post Order – A, B, C
(L R Rt)
Answer is C
20. What is the maximum numbers of levels (root is in level 1)of an AVL tree with 7
nodes?
(a)3
(b)4
(c)5
(d)6
Answer: B
Solution: The AVL tree of 7 elements with maximum levels:
If the post-order traversal gives a b-cd* +, then the label of the nodes 1, 2, 3,...
Solution:
Which one best describes the ranges of possible values for X and Y if
duplicates are not allowed?
(a)X >22 and Y >77
(b) 22 < X <44 and Y <77
(c) 22 < X <44 and Y >77
(d) 22 < X <44 and 44 < Y <77
Solution:
(a) The value of the root is 4. The left child of the 6 node is null.
(b) The value of the root is 7. The 9 node is a leaf.
(c) The value of the root is 12. The 15 node is its right child.
(d) The value of the root is 9. The right child of the 6 node is 7.
Solution:
Inorder predecessor,just small element from 11.
so just smallest element from 11 is 9.
so 11 is replace by 9.
25. Consider the AVL tree in the figure below which is not balanced after some
insertion.
Perform the appropriate rotation to restore the height balance of the tree. What
is the level order traversal of the tree after it has been balanced?
(a) R E X C M S Y A H P F
(b) R M X E P S Y C H A F
(c) M E R C H P X A F S Y
(d) E C M A H P F R X S Y
Solution:
50
25 80
20 35 70 90
15 23
30 45 65 75 100
5 18
68 95 150
26. If the value 46 is inserted into this tree, which node becomes its parent?
_________
Solution:
If the value 46 is inserted into this tree so 46 will become right child of 45
because it greater than 45.
Answer: 45 become parent of 46.
27. If the value 64 is inserted into this tree, which node becomes its parent?
_________
Solution:
If value 64 is inserted into this tree than 64 is greater than 50, and less than
80, and less than 70 and less than 65 , so it become left child of 65.
Answer:65
28. If we delete node 65, which node should be its replacement node?__________
solution:
If we delete 65, then 68 at its place.
Answer:68.
29. If we delete node 90, which node should be its replacement node, when we
replace in order successor?___________
Solution:
Delete node 90, So order successor of 90 is 95.
than node 95 replaced by node 90.
Answer: 95.
30. If we delete the root node 50, which node should be selected as its replacement
node so that the fewest number of changes are made to the tree? __________
Solution:
When we delete Root, then it can replace by either it inorder successor or
Preorder.
BASIC COMPUTER ORGANIZATION ARCHITECTURE Page 19
Inorder predecessor of 50 is 45.
Inorder successor of 50 is 65.
So, if it replaces by 45, so just replace it and no other changes required after
that and if it replace by 65, then 2 interchange require Ist 68 take it place than
65 take - place of 50.
Answer:45
31. Suppose we are storing binary tree in an array. What is the largest array that
might be necessary to hold a binary tree with 5 elements? ________
Solution:
32. How many n node binary trees with items 1, 2, ..., n have identical post order
and in-order traversals?
(a) 0 (b) 1 (c) n (d) n!
Answer: D
Solution:
A binary tree can have same Post-order and Inorder traversal if there is no
right sub-tree anywhere that means every root either has left child or is the
final node(leaf). That means this binary tree is just a list, hence total ways of
arranging are n!
36. A binary search tree is generated by inserting in order of the following integers:
50,15,62,5,20,58,91,3,8,37,60,24. The number of nodes in the left sub tree
and right sub tree of the root respectively is
(a) (4,7)
(b) (7,4)
(c) (8,3)
(d) (3,8)
Solution:
50,15,62,5,20,58,91,3,8,37,60,24.
The number of nodes in the left sub tree.
37. While inserting the elements 71, 65, 84, 69, 67, 83 in an empty binary search
38. The following numbers are inserted into an empty binary search tree in the
given order:10,1,3,5,15,12,16. What is the height of the binary search tree (the
height is the max distance of a leaf node from the root)?
(a) 2 (b) 3 (c) 4 (d)
1
Solution:
10,1,3,5,15,12,16
So height of tree is =3
Answer: B
39. Suppose that we have numbers between 1 and 1000 in a binary search tree
202 240
925 911
40. Suppose that you are searching for the key 70 in a binary search tree Which of
the following sequences is/are the sequence of nodes visited in the search?
1. 77, 41, 99, 20, 85, 70
2. 99, 10, 80, 20, 60, 70
3. 5, 10, 80, 40, 32, 50, 70
4. 22, 58, 81, 70
(a)Only 2 and 4
(b) Only 3
(c) Only 1, 2 and 4
(d) Only 3 and 4
Solution:
22 58
81
So 70 is in this sequence.
so sequence 4 is also possible.
Answer:A
41. Consider the Binary Search Tree built by inserting the following sequence of
integers, one at a time: 5, 4, 7, 9, 8, 6, 2, 3, 1. If the node containing 5 were
removed from the tree, what would be the right child of the node containing 2?
(a) Null
(b) The node containing 3.
(c) The node containing 4.
(d) The node containing 6.
Solution:
5, 4, 7, 9, 8, 6, 2, 3, 1
43. What is the time complexity for removing the root element from an AVL tree
containing N elements?
(a)θ(n)
(b)θ (logn)
(c) θ (n2)
(d) θ (nlog n)
Answer: B
Solution:
The rotation operations (left and right rotate) take constant time as only few
pointers are being changed there. Updating the height and getting the balance
factor also take constant time. So the time complexity of AVL delete remains
same as BST delete which is O(h) where h is height of the tree. Since AVL tree
is balanced, the height is O(Logn). So time complexity of AVL delete is O(Log n).
44. In the balanced binary tree (AVL) in the figure given below, how many nodes
will become unbalanced when a node W is inserted as a child of the node
“g”?____
Solution:
Solution:
248579
(1) RR :- RR rotation .
(3) RR Rotation
After deleting 31 from the tree by replacing it by its inorder predecessor. What
is the pre-order traversal sequence after deletion?
(a) 10, 19, 21, 25, 35,40, 49, 60
(b) 10, 21, 19, 35, 40, 60, 49, 25
(c) 25, 19, 49, 10, 21, 40, 60, 35
(d) 25, 19, 10, 21, 49, 40, 35, 60
Solution:
49. What value does the following function returns called with the root of the
following binary tree as parameter?_________
so minimum is 2 at root
so 2+1 =3
Answer: 3
50. What sequence of values does the following function print called with the root
of the following binary tree as parameter?
24 9 3 1 1 3 4 4 9 15 15 24
So possible sequence is
24,9,3,1,1,3,4,4,9,15,15,24.
Answer:C
51. Consider a level order traversal of the following binary tree. Which node is the
last node enqueued before the node containing y is dequeued?
The above 3 line of code interchange left and right children of all non- leaf
nodes.
Answer is B
Consider the following next three questions:
57. What will be the printed value when called printOrder1with the root node of a
given binary tree?
void printOrder1(struct node* node)
{
if (node == NULL)
return;
printOrder1(node->left);
58. What will be the printed value when called printOrder2with the root node of a
given binary tree?
voidprintOrder2(struct node* node)
{
if (node == NULL)
return;
printOrder2(node->left);
printf("%d\t ", node->data);
printOrder2(node->right);
}
(a) 5 15 12 9 23 50 52 49 20
(b) 20 9 49 5 12 23 52 15 50
(c) 5 9 12 15 20 23 49 50 52
(d) 20 9 5 12 15 49 23 52 50
Solution: PrintOrder2 return In-order traversal of given tree.
Printed value will be
5 9 12 15 20 23 49 50 52
Answer is C
59. What will be the printed value when called printOrder3with the root node of a
given binary tree?
D>C
(ii) D Could be grand – parent of x
It is true.
(iv) D could be left child of the right child of x.
It is true.
(v) D couldn’t be the right child of the right child of x, because there doesn’t
exist any letter which is greater than c and less than D.
Answer is D
Solution:
In BST, parent node > left child
Parent node < Right child
(a)
63. Suppose that we have numbers between 1 and 1000 in a binary search tree
and
want to search for the number 363. Which of the following sequences couldnot
be the sequences of nodes examined?
(I) 2,252,401,398,330,344,397,363.
(II) 924,220,911,244,898,258,362,363.
(III) 925,202,911,240,912,245,363.
(IV) 2,399,387,219,266,382,381,278,363.
(V) 935,278,347,621,299,392,358,363.
(a) I, II and III only (b) III and V only
(c) I and II only (d) I, II and IV only
Solution: We want to search for the 363.
I. 2,252,401,398,330,344,397,363
It is true.
II. 924,220,911,244,898,258,362,363
363<924, 220<363 (let x=363)
It is true.
III:- 925,202,911,240,912,245,363
It is true.
64. The following items are inserted into a binary search tree: 3, 6, 5, 2, 4, 7, 1.
which node is the deepest?
(a) 1 (b) 3 (c) 4 (d) 7
Solution: Order of insertion in BST
3, 6, 5, 2, 4, 7, 1
BST will be
Answer is C
i) ii)
(a)Only I
(b) Only II
(c) both I and II
(d) neither I nor II
Solution:
In- order : 3 5 6 7 10 11 14 17
If we delete 7 then there are two possible candidate to be root ( in place of 7 )
either in –order successor i.e. 10 or in-order predecessor i.e . 6.
Case I : - When we take in-order successor as a root then tree will be
Case II: When we take in-order predecessor as a root then tree will be
Solution:
Insert 90
RL
68. What is the height of the above AVL tree?(Assume root at height zero)
(a) 3 (b) 4 (c) 5 (d) 6
Solution: Height of above tree=3
Answer is A
69. Consider an AVL tree of height 5(Assume root at height zero). What is the
smallest
number of keys it can store? _____
Solution:
Height =5 (height of root =0)
Now, 12 is insertion
= A single rotation between some node and its right child is performed.
Answer: B
Which of the following discovering sequences is/are possible if we search the graph by
BFS and use B as the source vertex?
Answer:
(c) B, E, J, I, H, G, F, A, D, C (d) B, A, C, E, F, G, J, H, D, I
Answer: C
Answer: D
2. In a DFS(b) there is exactly one path from any node in T to the root
(A) 1-c, 2-a, 3-b (B) 1-b, 2-c, 3-a (C) 1-a, 2-b, 3-c (D) None
Answer: B
If we create recursive tree of DFS function, we will observe if there exist a path from
any vertex v to other vertex w then dfs(v) is called after dfs(w) but before dfs(w)
returns.
1. For a directed graph, the absence of back edges with respect to a BFS tree implies that
the graph is acyclic.
2. The depth of any DFS tree rooted at a vertex is at least as much as the depth of any
BFS tree rooted at the same vertex.
3. There is no edge in an undirected graph that jumps more than one level of any BFS
tree of the graph.
4. In an unweighted graph where the distance between any two vertices is at most T,
any BFS tree has depth at most T, but a DFS tree might have larger depth.
Ans: C
1. FALSE. It is true that the absence of back edges with respect to a DFS tree implies
that the graph is acyclic. However, the same is not true for a BFS tree. There may be
cross edges which go from one branch of the BFS tree to a lower level of another branch
of the BFS tree. It is possible to construct a cycle using such cross edges (which decrease
the level) and using forward edges (which increase the level).
2. TRUE. Since all vertices are connected by a path with at most T edges, and since BFS
always finds the path with the fewest edges, the BFS tree will have depth at most T. A
DFS tree may have depth up to V −1 (for example, in a complete graph).
3. TRUE. If such an edge existed, it would provide a shorter path to some node than the
path found by BFS (in terms in the number of edges). This cannot happen, as BFS
4. Ref Point(2).
82. Which sequence corresponds to that of depth first search for the graph given below?
The search starts at vertex 0 and lexicographic ordering is assumed for the edges
emanating from each vertex.
(A) 0 1 2 4 3 5 (B) 0 1 2 5 4 3
(C) 0 1 2 3 4 5 (D) 0 1 3 4 2 5
[43-2017]
83. Given a rooted tree, one desires to find the shortest path from the root to a given node
v. Which algorithm would one use to find this shortest path?
Answer: C
A tree has a unique path between any two pairs of nodes. Any traversal strategy would
give us the path (which is the shortest path).
84. Consider an undirected graph G. Let T be a depth first search traversal tree. Let u be a
vertex and v be the first unvisited vertex after visiting u. Which of the following
statements is always true?
Ans:
In DFS, after visiting a node, we first recur for all unvisited children. If there are no unvisited
children (u is leaf), then control goes back to parent and parent then visits next unvisited children.
85. Consider a graph G. Let T be a BFS tree with root r. Let d(u,v) denote the length of the
shortest path between the nodes u and v. If v is visited before u in the breadth first
Answer is (C). BFS is used to count shortest path from source (If all path costs are 1)
Now, if u is visited before v it means 2 things: Either u is closer to v, or, If u & v are
same distance from r, then our BFS algo chose to visit u before v.
1 1 0 0 0 1 1 1 1 1 1 0 1 1 1 0
1 0 1 0 1 0 0 0 1 0 0 0 1 0 0 0
(a) (b) (c) (d)
1 0 0 1 1 0 0 1 0 1 0 1 0 1 1 1
0 0 1 1 1 0 1 0 0 0 1 1 0 0 1 1
Answer: C
Answer: B
Solution:
ABC, ACB
ABCD
ACBD
ACDB
ADCB
ABDC
ADBC
number of spanning trees =6
The number of non-isomorphic tree, after applying DFS on Kn, if initial vertex is
given. For starting vertex 1, For other vertex, (n-1)!. number of spanning trees
=1* (n-1)!.
Answer is (n-1)!
88. How many different BFS output are possible for following graph considering 1 as a
source
vertex? _______
Solution:
1 2 3 4 5 6 8 7 9
1 2 4 3 5 6 8 7 9
1 3 4 2 6 7 8 5 9
1 3 2 4 6 5 8 7 9
1 2 4 3 5 8 6 9 7
1 4 2 3 6 8 5 7 9
1 4 2 3 8 6 5 9 7
1 4 3 2 6 8 5 7 9
1 4 3 2 8 6 5 9 7
Answer:9
89. [MSQ]
Answer: a, b, d
Solution:
ABEHGDCFJ is valid.
ABDCFGHEJ is valid.
ABDEHJGFC is valid.
ACDEHJBFG is not valid because in backtracking if we backtrack then after D, G must be traversed
but it traverses F hence not possible output.
Answer: C
Solution:
BFS so option B and C, D both are there but we have to choose alphabetic order hence (c) is
correct.
Answer: C
91. [MSQ]
Which of the following statements are correct?
(a) For a directed graph, the absence of back edges with respect to a BFS tree implies
that the graph is acyclic.
(b) There is no edge in an undirected graph that jumps more than one level of any BFS
tree of the graph.
(c) The depth of any DFS tree rooted at a vertex is at least as much as the depth of any
BFS tree rooted at the same vertex.
(d) None of the above
Answer: b, c
Solution:
(a) For a directed graph, the absence of bake edges with respect to a BFS tree implies that the graph
is acyclic (False)..
Reason: Because, there may be cross edges which go form one branch of the BFS tree to a lower
level of another branch of the BFS tree. It is possible to construct a cycle using such cross edges
(Which decrease the level) and using forward edges (which increase the level).
(True) .
Reason: If such an edge existed, it would provide a shorter path to some node than the path found
by BFS (in terms of number of edges). This cannot happen as BFS always finds the path with
fewest edges.
(c) The depth of any DFS tree rooted at a vertex is at least as much as the depth of any BFS tree
rooted at the same vertex. (True).
Reason: Since BFS finds paths using the fewest number of edges, the BFS depth of any vertex is at
least as small as the DFS depth of the same vertex.
Answer: b, c
92. Suppose that you perform a DFS on an undirected graph G = (V, E) and for each vertex
u ∈ V. You compute the discovery time d[u] and finish time f[u]. Let u be a descendant
of v in the DFS tree. What can be said about the relative order of d[u], f[u], d[v], and
f[v]? ___________
(a) d[v] < d[u] < f[u] < f[v]. (b) d[u] < d[v] < f[v] < f[u].
(c) d[v] > d[u] > f[u] > f[v]. (d) d[v] < d[u] < f[v] < f[u].
Solution:
DFS an undirected graph G= (V, E) and for each vertex uv. you compute the
discovery
time d[u] and finish time f[u] . u be a descendant of v in DFS tree. So here is DFS we
know parent discover first then child but finish after child. as here v is parent and u is
child. Order will be d[v] < d[u] < f[u] < f[v].
Answer: A
Answer: None
Solution: All are incorrect.
DFS order: ABEFCHDG
Suppose a depth first traversal of this graph is performed starting vertex ‘a’,
assuming that whenever there is a choice, the vertex earlier in the alphabetical order is
to be chosen. Then:
Solution:
I :- Number of tree edge = 5
Tree edges will be
Suppose a breadth first traversal of this graph is performed with starting vertex ‘a’,
96. Let A be the adjacency matrix of the graph G given below. For n ≥ 1, let pn denote the
entry of An in the row labeled a and the column labeled c. Select the correct answer:
(A) p1 = 0, p2 = 1, p3 = 1 (B) p1 = 0, p2 = 2, p3 = 2
(C) p1 = 0, p2 = 2, p3 = 2 (D) p1 = 0, p2 = 2, p3 = 3
Solution:
a 0 1 0 0 0 1 1 1 0
b 1 0 1 0 0 0 0 0 0
c 0 1 0 1 0 0 0 1 1
d 0 0 1 0 1 0 0 0 0
e 0 0 0 1 0 1 1 0 1
f 1 0 0 0 1 0 0 0 0
g 1 0 0 0 1 0 0 1 1
h 1 0 1 0 0 0 1 0 1
i 0 0 1 0 1 0 1 1 0
p2=2
Answer is D
[Q65- 2019]
Solution: a, b, c, e, d
Answer is B
98. Let M = [mi,j] be the adjacency matrix of a normal graph (that is, not a digraph). If m1,2 =
1, then what is m2,1?
(A) -1 (B)0
Answer: C
Since graph is undirected, therefore if there is an edge from vertex 1 to vertex 2 then definitely
there will be an edge from vertex 2 to vertex 1 and hence m2,1= 1.
99. If G is a directed graph with 20 vertices, how many Boolean values will be needed to
represent G using an adjacency matrix?
Ans: A
Since there are 20 vertices therefore the matrix size will be 20x20 and hence we will
need 400 values.
100. Which choice lists all vertices that have incorrect rows in the adjacency matrix for this
graph?
Ans: D
Entries for vertex B is wrong as clearly edge B-I has weight 3 and meanwhile matrix has entry 4.
Vertex E has 3 entries and E has degree 2 hence it is wrong. Same reason for vertices F, I and K as
well.
101. Four graphs are represented using an adjacency matrix as shown below. Which of the
following adjacency matrix represents a connected graph?
Ans:
Answer: a, b
Answer: 13
Solution:
105. [MSQ]
For the above graph which of the following order is/are correct topological ordering?
(a) 1 2 3 4 5 6
(b) 1 2 3 4 6 5
(c) 1 3 2 4 6 5
(d) 1 3 2 4 5 6
Answer: a, b, c, d
Solution:
Answer: D
Solution: The equivalent graph and adjacency matrix are shown below:
and
Answer: 4
Data for next four questions: Consider the following simple directed graph G:
Suppose a breadth first traversal of this graph is performed with starting vertex ‘a’, assuming
that whenever there is multiple choice, the vertex earlier in the alphabetical order is to be
chosen.
Common Solution: The tree of BFS traversal of given graph is shown below:
Answer: A
Answer: 0
(a){ } (b) {ei, di, cg} (c) {ia, jh} (d) {ia}
Answer: c
(a){ag, ai} (b) {ei, di, cg} (c) {ei, bi, cg} (d) {cg,
ei}
Answer: B
Suppose a depth first traversal of this graph is performed starting vertex ‘a’.
Assuming that whenever there is multiple choice, the vertex earlier in the alphabetical order
is to be chosen.
Common Solution: The tree of DFS traversal of given graph is shown below:
(a) {ab, be, ei, ad, dh, hj, ac, cg, cf}
Answer: A
Answer: A
Answer: D