BinaryTrees
Question 1 
I. The smallest element in a maxheap is always at a leaf node.
II. The second largest element in a maxheap is always a child of the root node.
III. A maxheap can be constructed from a binary search tree in Θ(n) time.
IV. A binary search tree can be constructed from a maxheap in Θ(n) time.
Which of the above statements are TRUE?
I, II and III  
II, III and IV  
I, III and IV  
I, II and IV 
(ii) TRUE: The second smallest element in a heap is always a child of root node.
(iii) TRUE: Converting from binary search tree to max heap will take O(n) time as well as O(n) space complexity.
(iv) FALSE: We can’t convert max heap to binary search tree in O(n) time.
Question 2 
Let T be a full binary tree with 8 leaves. (A full binary tree has every level full). Suppose two leaves a and b of T are chosen uniformly and independently at random. The expected value of the distance between a and b in T (i.e., the number of edges in the unique path between a and b) is (rounded off to 2 decimal places) _____.
5.54  
1.34  
4.25  
3.82 
A node can be chosen twice and the path from that node to itself will be zero.
∴ Path 1 = 0
Similarly,
Path 2 = 2
Path 3 = 4
Path 4 = 4
Path 5 = 6
Path 6 = 6
Path 7 = 6
Path 8 = 6
∴ Expected value = Σ Path length × Probability of selecting path
= 2×1/8 + 4×2/8 + 6×4/8 + 0×1/8
= 1/4 + 1/1 + 3/1 + 0
= 4 + 1/4
= 17/4
= 4.25
Question 3 
Let T be a binary search tree with 15 nodes. The minimum and maximum possible heights of T are:
Note: The height of a tree with a single node is 0.4 and 15 respectively  
3 and 14 respectively  
4 and 14 respectively  
3 and 15 respectively 
The height of a tree with single node is 0.
Minimum possible height is when it is a complete binary tree.
Maximum possible height is when it is a skewed tree left/right.
So the minimum and maximum possible heights of T are: 3 and 14 respectively.
Question 4 
The preorder traversal of a binary search tree is given by 12, 8, 6, 2, 7, 9, 10, 16, 15, 19, 17, 20. Then the postorder traversal of this tree is:
2, 6, 7, 8, 9, 10, 12, 15, 16, 17, 19, 20  
2, 7, 6, 10, 9, 8, 15, 17, 20, 19, 16, 12  
7, 2, 6, 8, 9, 10, 20, 17, 19, 15, 16, 12  
7, 6, 2, 10, 9, 8, 15, 16, 17, 20, 19, 12 
From these 2 orders, we can say 12 is the root & 8 is the root of left subtree & 16 is the root of right subtree.
From 2, 6, 7 we can identify 6 is the root from preorder traversal and from 9, 10 → 9 is root.
From <17, 19, 20>, 19 as root.
Hence, 2,7,6,10,9,8 ,15,17,20,19,16 12 is the postorder traversal.
Question 5 
Consider the following Neworder strategy for traversing a binary tree:
 Visit the root;
 Visit the right subtree using Neworder;
 Visit the left subtree using Neworder;
The Neworder traversal of the expression tree corresponding to the reverse polish expression 3 4 * 5  2 ˆ 6 7 * 1 +  is given by:
+  1 6 7 * 2 ˆ 5  3 4 *  
 + 1 * 6 7 ˆ 2  5 * 3 4  
 + 1 * 7 6 ˆ 2  5 * 4 3  
1 7 6 * + 2 5 4 3 *  ˆ  
Given Reverse Polish Notation as:
3 4 * 5  2 ^ 6 7 * 1 + 
We know Reverse Polish Notation takes Left, Right, Root.
So the expression tree looks like
From the tree, we can write the New Order traversal as: Root, Right, Left.
 + 1 * 7 6 ^ 2  5 * 4 3
Question 6 
A binary tree T has 20 leaves. The number of nodes in T having two children is _________.
19  
20  
21  
22 
(i) p vertices (i.e., leaves) of degree 1
(ii) one vertex (i.e., root of T) of degree 2
(iii) 'n  p  1' (i.e., interval) vertices of degree 3
(iv) n  1 edges
∴ By Handshaking theorem,
p × 1 + 1 × 2 + (n  p  1) × 3 = 2(n  1)
⇒n = 2p  1
= 39 as p = 20
∴ n  p = 19 vertices have exactly two children
Question 7 
Consider a binary tree T that has 200 leaf nodes. Then, the number of nodes in T that have exactly two children are ____.
199  
198  
197  
196 
Question 8 
The height of a tree is defined as the number of edges on the longest path in the tree. The function shown in the pseudocode below is invoked as height (root) to compute the height of a binary tree rooted at the tree pointer root.
The appropriate expression for the two boxes B1 and B2 are
B1: (1+height(n→right)) B2: (1+max(h1,h2))  
B1: (height(n→right)) B2: (1+max(h1,h2))  
B1: height(n→right) B2: max(h1,h2)  
B1: (1+ height(n→right)) B2: max(h1,h2) 
Now, we analyze the code,
The box B1 gets executed when left sub tree of n is NULL & right subtree is not NULL.
In such case, height of n will be the height of the right subtree + 1.
The box B2 gets executed when both left & right subtrees of n are not NULL.
In such case, height of n will be max of heights of left & right subtrees of n+1.
Question 9 
We are given a set of n distinct elements and an unlabeled binary tree with n nodes. In how many ways can we populate the tree with the given set so that it becomes a binary search tree?
0  
1  
n!  
Question 10 
In a binary tree with n nodes, every node has an odd number of descendants. Every node is considered to be its own descendant. What is the number of nodes in the tree that have exactly one child?
0  
1  
(n1)/2  
n1 
Every node has an odd number of descendants.
Also given every node is considered to be its own descendant.
― This is even number of descendants (2), because A is also considered as a descendant.
― This is odd number of descendants (3), A, B, C are descendants here.
Condition satisfied – odd number, but number of nodes in a tree that has exactly one child is 0.
Question 11 
The height of a binary tree is the maximum number of edges in any root to leaf path. The maximum number of nodes in a binary tree of height h is:
2^{h}−1  
2^{h−1} – 1  
2^{h+1}– 1  
2^{h+1}

1, 3, 7, 15, 31, ... = 2^{h+1}  1
Question 12 
The maximum number of binary trees that can be formed with three unlabeled nodes is:
1  
5  
4  
3 
Total no. of possible trees is 5.
Total = 5
Question 13 
The inorder and preorder traversal of a binary tree are d b e a f c g and a b d e c f g, respectively. The postorder traversal of the binary tree is:
d e b f g c a  
e d b g f c a  
e d b f g c a  
d e f g b c a 
Pre order: Root Left Right
Post order: Left Right Root
Inorder: d b e a f c g
Pre order: a b d e c f g
Post order: d e b f g c a
Question 14 
Consider the following C program segment where CellNode represents a node in a binary tree:
struct CellNode { struct CellNOde *leftChild; int element; struct CellNode *rightChild; }; int GetValue(struct CellNode *ptr) { int value = 0; if (ptr != NULL) { if ((ptr>leftChild == NULL) && (ptr>rightChild == NULL)) value = 1; else value = value + GetValue(ptr>leftChild) + GetValue(ptr>rightChild); } return(value); }The value returned by GetValue() when a pointer to the root of a binary tree is passed as its argument is:
the number of nodes in the tree
 
the number of internal nodes in the tree  
the number of leaf nodes in the tree  
the height of the tree 
So from applying algorithm to above tree we got the final value v=3 which is nothing but no. of leaf nodes.
Note that height of the tree is also 3 but it is not correct because in algorithm the part
if ((ptr → leafchild = = NULL) && (ptr → rightchild = = NULL)) value = 1;
Says that if there is only one node the value is 1 which cannot be height of the tree because the tree with one node has height '0'.
Question 15 
A scheme for storing binary trees in an array X is as follows. Indexing of X starts at 1 instead of 0. The root is stored at X[1]. For a node stored at X[i], the left child, if any, is stored in X[2i] and the right child, if any, in X[2i+1]. To be able to store any binary tree on n vertices the minimum size of X should be.
log_{2}n  
n  
2n+1  
2^{n}1 
Note: Assume level numbers are start with 0.
Question 16 
Consider the label sequences obtained by the following pairs of traversals on a labeled binary tree. Which of these pairs identify a tree uniquely?
(i) preorder and postorder (ii) inorder and postorder (iii) preorder and inorder (iv) level order and postorder
(i) only  
(ii), (iii)
 
(iii) only
 
(iv) only 
(i) Inorder and Preorder
(ii) Inorder and Postorder
(iii) Inorder and Levelorder
→ And following are do not
(i) Post order and Preorder
(ii) Pre order and Level order
(iii) Post order and Level order
Question 17 
Consider the following C program segment
struct CellNode { struct CelINode *leftchild; int element; struct CelINode *rightChild; } int Dosomething(struct CelINode *ptr) { int value = 0; if (ptr != NULL) { if (ptr>leftChild != NULL) value = 1 + DoSomething(ptr>leftChild); if (ptr>rightChild != NULL) value = max(value, 1 + DoSomething(ptr>rightChild)); } return (value); }
The value returned by the function DoSomething when a pointer to the root of a nonempty tree is passed as argument is
The number of leaf nodes in the tree  
The number of nodes in the tree  
The number of internal nodes in the tree  
The height of the tree 
→ So given that pointer to root of tree is passed to DoSomething ( ), it will return height of the tree. It is done when the height of single node is '0'.
Question 18 
Draw all binary trees having exactly three nodes labeled A, B and C on which Preorder traversal gives the sequence C, B, A.
Theory Explanation is given below. 
Total 5 binary trees are possible with the preorder C, B, A.
Question 19 
Consider the following nested representation of binary trees: (X Y Z) indicates Y and Z are the left and right sub stress, respectively, of node X. Note that Y and Z may be NULL, or further nested. Which of the following represents a valid binary tree?
(1 2 (4 5 6 7))  
(1 (2 3 4) 5 6) 7)  
(1 (2 3 4) (5 6 7))  
(1 (2 3 NULL) (4 5)) 
(Proper Representation)
Question 20 
Let LASTPOST, LASTIN and LASTPRE denote the last vertex visited in a postorder, inorder and preorder traversal. Respectively, of a complete binary tree. Which of the following is always tree?
LASTIN = LASTPOST  
LASTIN = LASTPRE  
LASTPRE = LASTPOST  
None of the above 
But in case of complete binary last level need not to be full in that case LASTPRE is not equal to LASTIN.
Question 21 
Which of the following statements is false?
A tree with a n nodes has (n – 1) edges  
A labeled rooted binary tree can be uniquely constructed given its postorder and preorder traversal results  
A complete binary tree with n internal nodes has (n + 1) leaves  
Both B and C 
D: The maximum no. of nodes in a binary tree of height h is 2^{h+1}  1.
h=2 ⇒ 2^{3}  1 ⇒ 7
Question 22 
Draw the binary tree with node labels a, b, c, d, e, f and g for which the inorder and postorder traversals result in the following sequences:
Inorder a f b c d g e Postorder a f c g e d b
Theory Explanation. 
Question 23 
A binary search tree contains the value 1, 2, 3, 4, 5, 6, 7, 8. The tree is traversed in preorder and the values are printed out. Which of the following sequences is a valid output?
5 3 1 2 4 7 8 6  
5 3 1 2 6 4 8 7  
5 3 2 4 1 6 7 8  
5 3 1 2 4 7 6 8 
Option D:
Let draw binary search tree for the given sequence,
After traversing through this tree we will get same sequence.
Question 24 
In the balanced binary tree in the below figure, how many nodes will become unbalanced when a node is inserted as a child of the node “g”?
1  
3  
7  
8 
a, b, c are going to unbalance.
Question 25 
Which of the following sequences denotes the post order traversal sequence of the tree of question 14?
f e g c d b a  
g c b d a f e  
g c d b f e a  
f e d g c b a 
Left → Right → Root
g c d b f e a
Question 26 
A binary search tree is generated by inserting in order the following integers:
50, 15, 62, 5, 20, 58, 91, 3, 8, 37, 60, 24
The number of nodes in the left subtree and right subtree of the root respectively is
(4, 7)  
(7, 4)  
(8, 3)  
(3, 8) 
So greater than 50 will be in right subtree of 50 and less than 50 in left subtree.
So, answer will be (7, 4).
Question 27 
What is the number of binary trees with 3 nodes which when traversed in postorder give the sequence A, B, C? Draw all these binary trees.
Theory Explanation. 
Question 28 
A rooted tree with 12 nodes has its nodes numbered 1 to 12 in preorder. When the tree is traversed in postorder, the nodes are visited in the order 3, 5, 4, 2, 7, 8, 6, 10, 11, 12, 9, 1.
Reconstruct the original tree from this information, that is, find the parent of each node, and show the tree diagrammatically.
Theory Explanation. 
Question 29 
The preorder traversal of a binary search tree is 15, 10, 12, 11, 20, 18, 16, 19.
Which one of the following is the postorder traversal of the tree?
20, 19, 18, 16, 15, 12, 11, 10  
11, 12, 10, 16, 19, 18, 20, 15
 
10, 11, 12, 15, 16, 18, 19, 20  
19, 16, 18, 20, 11, 12, 10, 15 
Postorder:
11, 12, 10, 16, 19, 18, 20, 15
Question 30 
What is the worst case time complexity of inserting n^{2} elements into an AVLtree with n elements initially?
θ(n^{4})
 
θ(n^{2})
 
θ(n^{3})  
θ(n^{2} log n) 
In question they asked about n^{2} elements.
So, In worst case it will take o(n^{2} log n) time.
Question 31 
In a balanced binary search tree with n elements, what is the worst case time complexity of reporting all elements in range [a,b]? Assume that the number of reported elements is k.
θ(n log k)  
θ(log n + k)  
θ(k log n)  
θ(log n) 
Time complexity of the above program is O(h + k) where h is the height of BST and k is the number of nodes in a given range.
Here h is log n, hence O(log n+k).
Question 32 
Prove by the principal of mathematical induction that for any binary tree, in which every nonleaf node has 2 descendants, the number of leaves in the tree is one more than the number of nonleaf nodes.
Theory Explanation. 
Question 33 
The weighted external path length of the binary tree in figure is _____
144 
Question 34 
Consider the binary tree in Figure:
(a) What structure is represented by the binary tree?
(b) Give the different steps for deleting the node with key 5 so that the structure is preserved.
(c) Outline a procedure in pseudocode to delete an arbitrary node from such a binary tree with n nodes that preserves the structure. What is the worstcase timecomplexity of your procedure?
Theory Explanation. 
Question 35 
Choose the correct alternatives (More than one may be correct). The number of rooted binary trees with n nodes is,
Equal to the number of ways of multiplying (n+1) matrices.  
Equal to the number of ways of arranging n out of 2n distinct elements.  
Equal to n!  
Both (A) and (C). 
Here, both options A and C are true as option A corresponds to n multiply operations of (n+1) matrices, the no. of ways for this is again given by the n^{th} catalan number.