CDS S2
CDS S2
(SET: 2)
Solutions
1. What is the maximum height of any AVL tree with 7 nodes? Assume that the height of a tree
with single node is 0.
(a) 2 (b) 3
(c) 4 (d) 5
2. In a binary tree with n nodes every node has an odd no. of descendants. Every node is
considered to be its own descendant. What is the no. of nodes in the tree that has exactly one
child?
(a) 0 (b) 1
(c) (n-1)/ 2 (d) n-1
Explanation:
A full binary tree is a tree in which every node other than the leaves has 2 children. A complete
binary tree is a binary tree in which every level, except possibly that last, is completely filled,
and all nodes are as far left as possible.
(a) is incorrect. For eg—the following binary tree is neither complete nor full:
(b) is incorrect. The following binary tree is complete but not full:
5. In a complete K-ary tree, every internal node has exactly K children or no child. The no. of
leaves in such a tree with n internal nodes is:
Let N( n ) be the number of leaf nodes in K ary tree , having n iinternal nodes. The recurrence
equation is N(n) =N( n-1) + K-1 , now N(1) = K.
6. The no. of leaf nodes in a rooted tree of n nodes, with each node having 0 or 3 children is:
Explanation:
The total number of nodes given is n. Now the total number of nodes is divided into leaf+
internal nodes. Let c be the number of internal nodes, therefore we have n-c leaf nodes.Now
using the result we obtained in previous question we obtain that number of leaf nodes with c
internal nodes in a 3 ary tree (K=3) , is c(3-1) + 1= 2c+1 . Now, number of leaf nodes +number
of internal nodes= n , therefore
c+ 2c+1= n
implies c= (n-1) /3
7. A weight balanced tree is a binary tree in which for each node, the no. of nodes in the left sub
tree is atleast half and at most twice the no. of nodes in the right sub tree. The maximum possible
height of such a tree with n nodes is best described by which of the following?
Explanation:
8. A schema for storing a binary tree in an array X is as follows. Indexing of X starts at 1 instead
3
of 0. The root is stored at X[1]. For a node stored at X[i] the left childs if any is stored at X[2i]
and right child at X[2i + 1]. To be able to store any binary tree of n vertices, the minimum size of
X should be:
Explanation:
10, 9, 23, 22, 27, 25, 15, 50, 95, 60, 40, 29
(a) 9, 10, 15, 22, 23, 25, 27, 29, 40, 50, 60, 95
(b) 9, 10, 15, 22, 40, 50, 60, 95, 23, 25, 27, 29
(c) 29, 15, 9, 10, 25, 22, 23, 27, 40, 60, 50, 95
(d) 95, 50, 60, 40, 27, 23, 22, 25, 10, 9, 15, 29
Explanation:
Inorder traversal produces sorted order of elements of a BST. The inorder traversal is
9 10 15 22 23 25 27 29 40 50 60 95.
Using the postorder and inorder traversal we can get a unique BST .
4
10. Consider a node X in a Binary tree. Given that X has 2 children. Y be a inorder successor of
X. Which of the following is true about Y?
Explanation:
Since X has both children, Y must be leftmost node in the right subtree of X, hence Y cannot
have a left child as that would contradict the question statement.
11. The height of a tree is the length of the longest root-to-leaf path in it. The max and min no. of
nodes in a binary tree of height 5 are:
Explanation:
12. A binary tree T has 20 leaves. The no. of nodes in T having 2 children is:
(a) 18 (b) 19
(c) 17 (d) any number between 10 and 20
Explanation:
The no. of nodes with 2 children is always one less than no. of leaves.
13. Consider a complete binary tree where the left and right subtrees of the root are max- heaps.
5
The lower bound of the no. of operations to convert the tree to a heap is:
Explanation:
It is simple to call max-heapify which recurses atmost through height of the heap.
14. The inorder and preorder traversal of a binary tree is—d b e a f c g and a b d e c f g
respectively. The postorder traversal of the binary tree is:
(a) d e b f g c a (b) e d b g f c a
(c) e d b f g c a (d) d e f g b c a
Explanation :
From the preo order and inorder traversal we can uniquely obtain a BST.
15. Which of the following pair of traversals is not sufficient to build a binary tree from the
given traversals?
16. Which traversal of tree resembles the breadth first search of the graph?
6
17. Which of the following tree traversal uses a queue data structure?
Explanation:
Level order traversal is the implementation of breadth first search. And we use a queue for
breadth first search.
18. What is the worst case complexity for search, insert and delete operations in a general binary
search tree?
19. 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 BST (the height is the max. distance of a leaf node from the root)?
(a) 2 (b) 3
(c) 4 (d) 6
Explanation: