Tree Solution
Tree Solution
Q1. In Binary tree what is the maximum number of node at depth „4‟ if depth start from 1.
(a)16 (b)15
(c)31 (d) 8
Ans D
At depth d there are 2d-1 nodes: - at depth 4 there will be 24-1 =23 nodes =8 nodes
Q2. In a complete k-ary tree, every internal node has exactly k children or no child. The number
of external node in such a tree with n internal nodes is:
(a) nk (b) (n – 1) k+ 1
(c) n(k – 1) + 1 (d) n(k – 1).
Ans (c)
K-ary tree means exactly k children on no child let consider 2-ary binary tree
with 3 internal nodes then 4 external nodes
Let consider 3 any or ternary tree with 4 internal node the external node
i.e (k-1) n+1
(k-1) n+1 satisfy all the cases
Q3. What is the maximum number of leaf nodes in a binary tree with 15 nodes?
(a)8 (b)15
(c)12 (d)7
Answer:A
We will get maximum no. of leaf nodes when the tree is balanced
Suppose we remove the root, replacing it with its in-order successor. What will be the new
root?
(a) 16 (b) 24
(c) 25 (d) 2
Ans a
In order =1 2 14 16 24 25
Indore successor of 14 (root nodes) is 16, hence new root will be 16 (as we are
replacing root with its in order successor).
Q6. What does the following algorithm calculate when called with the root of a binary tree as
the input?
int treeAlg(BinaryNode t)
{
if (t.left != null || t.right != null)
return 1 + treeAlg(t.left) + treeAlg(t.right);
else
return 0;
}
(a) the number of nodes in the tree
(b) the length of the shortest path from the root to a leaf
(c) the length of the longest path from the root to a leaf
(d) the number of internal (non-leaf) nodes
And D
Taking example
Stack
treeAlg ( (1) treeAlg( (1)
1+ treeAlg(2)+ treeAlg((3) 1+t(4)+t(5)
2 1 0 0
Ans :-3
Ans :-3
Total no of rotations =3
Q8. Which one of the following rates of growth is correct for the number of leaves L(N) in a
complete binary tree with N nodes?
(a) L(N) = θ(N) (b)L(N) = θ (logN)
(c)L(N) = θ (N logN) (d)L(N) = θ (N2)
Ans A
In complete binary if there are n nodes then the no .of leaf nodes are (n+1)/2
leaf nodes hence if we increase n linearly then no .of nodes will also increase
linearly L(N)=O(n).
Q9. Draw the binary search tree with keys 1, 2, 3, 4, 5, 6, 7, 8 such that the preorder starts with
3, 1, 2, 7 and the postorder ends with 5, 8, 7, 3. Then find the complete postorder traversal.
(a) 2 1 4 6 8 5 7 3 (b)2 1 4 6 5 8 3 7
(c)1 2 4 5 6 8 7 3 (d)2 1 4 6 5 8 7 3
Ans: D
Binary search tree with keys 1 2 3 4 5 6 7 8
Inorder: - 1 2 3 4 5 6 7 8
Preorder starts with:-3 1 2 7
Postorder ends with: - 5,8,7,3
Using Preorder and inorder
Now 4 and 6 are left as the given tree is binary search tree after adding 4 and
6 the tree will be:-
The post order of the tree will be:-
21465873
Q10. What would the recursive procedure below print if called on the root of the tree given
below?
void traverse(x)
{
if (x != null)
{
print x.data;
traverse(x.left);
traverse(x.right);
print x.data;
}}
(a) ABCDEABCDE (b) ABBEEDDCCA
(c) ABBCCDDEEA (d) ABBCDEEDCA
Ans :- D
Output ABBCDEEDCA
Q11. Which of the following are necessarily true of the linked implementation of a binary tree?
(i) The worst-case space complexity is (|V|)
(ii) Each internal vertex has neither a left pointer nor a right pointer of NIL
(iii) Each terminal vertex has both a left pointer and a right pointer of NIL
(a) (i) only (b) (i) and (ii) only
(c) (i) and (iii) (d) (ii) and (iii)
Ans :-A
(i)TRUE
(ii) FALSE internal vertex may have any one of the right pointer or left pointer as nil
(iii) True
Q12. Which node is the deepest when the items 3, 6, 5, 2, 4, 7, 1 are inserted into an initially
empty binary search tree?
(a) 1 (b) 3
(c) 4 (d) 7
1. Ans :-c
Q13. Suppose that we have a binary tree with fields left, right, and data as usual; the data fields
contain integers. An integer field value is also present in each node. The root of the tree is r.
assuming the procedure mystery is as shown below, what would be the effect of the call
mystery(r, 0)?
void mystery(x, i) {
if (x != null) {
x.value = x.data + i;
x.value = mystery(x.left, x.value);
x.value = mystery(x.right, x.value);
return x.value
}}
(a) For all nodes x in the tree, set the value field to be equal to the sum of the data fields of
the descendants of x.
(b) For all nodes x in the tree, set the value field to be equal to the sum of the data fields of
the ancestors of x.
(c) For all nodes x in the tree (except the root), set the value field to be equal to the sum of
the data fields in x and its parent.
(d) For all nodes x in the tree, set the value field to be equal to the sum of the data fields of
all nodes in the tree.
Q14. Suppose that we have the numbers between 1 and 500 in a binary search tree and we
perform a search for the number 331 starting at the root of the tree. Which of the following
cannot be a sequence of nodes examined?
(a) 492, 7, 105, 390, 331
(b) 9, 250, 390, 100, 331
(c) 5, 105, 401, 243, 331
(d) 1, 475, 252, 333,331
Ans :_B
(A)
492, 7, 105, 390, 331
7 105 331
492 390
This can be the sequence
(B)
9, 250, 390, 331,
Not in increasing order
100,
9 250 100
331
390
This cannot be sequence
(C)
1 252
331
475 333
This is also a possible sequence
Q15. Which of the following, from the following list produce the same BST as A E F G B D C
when inserted in the order given into an initially empty tree.
(i) A C B E G D F
(ii) A E B F D C G
(iii) A E B G C D F
(iv) A E G C F D B
(v) A E G D C B F
(vi) E A G B D F C
(a) i only (b) ii only
(c) iii, v and vi only (d) none
Ans :-b
Given order :- AEFGBDC
BST:-
only (II)
has same bst as given bst
Q16. 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
Ans (D)
Minimum height the minimum height of the tree will be when the tree will be
FULL binary tree.
2h+1-1=100
2h+1-1=100+1
2h+1-1=101
(h+1)_log22=log2101
h+1=log2101
h2=7-1
h=6
Maximum height when the tree will be in the form of chain
Height = no of edges =n-1
=100-1=99
dosomething (5)
dosomethind (15)
doeomthing (21) return =21
it return the largest element in non-empty binary search tree.
Q18. An AVL tree of minimal size with height 5 will have how many nodes? (Assuming root is
at height zero)
(a) 6 (b) 12
(c) 20 (d) 32
Ans (C)
n(h)total no of nodes with height h
n(n)=n(h-1)+n(h-2)+1
n(0)=1
n(!)=2
n(2)=1+2+1=4
n(3)=4+2+1=7
n(4)=7+4+1=12
n(5)=12+7+1=20
Q19. The worst case time complexity of search and insert operations in BST is :
(a)O(n),O(logn) (b) O(logn),O(logn)
(c) O(n),O(n) (d) O(logn),O(n)
Ans (C) BST (binary search tree)can be in the form of chain in the worst case
complexity of search and insert operation will be o(n) ando (n) respectively.
Q20. The worst case time complexity of insert , delete , Search operations in AVL tree is :
(a)O(n),O(logn),O(logn) (b) O(logn),O(logn)
(c) O(n),O(n),O(n) (d) O(logn),O(logn),O(logn)
Ans (D) AVL tree is always balanced hence complexity of insert delete and search
operation will be O(log n) , O(log n ),O(logn) respectively.
Q21. What is the balance factor for the highlighted node?
(a) -1 (b) -2
(c) 2 (d) 1
Ans :-C
Q22. If output of in-order traversal of a binary tree is GDBHEIACJF and output of post-order
traversal is GDHIEBJFCA what is the output of pre-order traversal?
(a) ABGDEHICJF (b) ABDGEHICJF
(c) ABDGEHICFJ (d) ABGDEHICFJ
Ans :-c
Inorder –GDBHEIACJF
Post order –GDHIEBJFCA
Tree:-
PREORDER: - ABDGEHICFJ
Q23. Suppose that we insert a 48 into the AVL tree (also known as height-balanced tree) shown
below. Which rotation will be performed?
Ans D
48 will be inserted here due to this 46 and 23 and 51 gets unbalanced 48 is
inserted to the left child of the right child of the 46(ie in the night then in the left
). Hence RL rotation will be required to balance it.
(Single double rotation at 46 is required)
# Note: - we balance the node which is closes to the leaf due to which other
nodes automatically gets balance .
Q24. Each of the following lists of numbers is inserted, in the order given, into a binary search
tree. Which list produces the most balanced tree?
(a) 2, 4, 7, 5, 8, 10 (b) 9, 7, 2, 1, 4, 0
(c) 5, 1, 2, 6, 3, 4 (d) 6, 4, 1, 8, 10, 5
Ans :-d
Ans:-D
Q25. How many AVL trees containing the keys 1, 2, 3, 4 can be built?
(a) 2 (b) 3
(c) 4 (d) 5
Ans :-C
Different AVL tree with the keys containing 1,2,3,4 are:-
(1) (2) (3)
Q33. If a node in a BST has two children, then its inorder predecessor has
(a) no left child (b) no right child
(c) two children (d) no child
And:-B
And:-B
If a node in a BST has two children then its in order pre decessor has no right child
Example
Inorder: - 1 2 4 5 6 7 8 9
Consider the node 4
Its in order predecessor or is 2, it does not have right child .
And:-B
Ans :-a
For next two questions consider that the following keys are inserted keys into an AVL tree
in the Following order: 40, 20, 15, 25, 30, 80, 75, 95, 90, 35, 100
Q36. Which key would be in the root of the tree after inserting all the keys?
(a)75 (b) 40
(c) 35 (d) 30
Ans :-A
40,20,15,25,30,80,75,95,90,35,100
Q37. What is the height of the above AVL tree?(Assume root at height zero)
(a) 3 (b) 4
(c) 5 (d) 6
Ans :-B height of the tree is (4)
Q38. What is the best Big-O runtime (e.g., O(1), O(log n), O(h), or O(n)) for searching for an
element in each hierarchical data structure with n elements and tree height h?
(a) BinaryTree - O(n), BinarySearchTree - O(h), AVL Tree - O(log n)
(b) BinaryTree - O(logn), BinarySearchTree - O(n), AVL Tree - O(log n)
(c) BinaryTree - O(n), BinarySearchTree - O(n), AVL Tree - O(n)
(d) BinaryTree - O(h), BinarySearchTree - O(h), AVL Tree - O(log n)
Ans :-A
Binary tree:-0(n)
In worst case we have traverse all nodes to search an elements.
Binary Search tree:-O (h)
In binary search tree. Searching depends upon height of the tree .if it is chain
then o(n),(and d in chain O (h)=(n)
If it is balanced searching requires o (log n) i e o (n) =o (log n)
It depends upon the height in BST (they are asking about best big O,
We will prefer o (n) over o (n)
AVL tree: - O (log n)
Q39. What is the best Big-O runtime (e.g., O(1), O(log n), O(h), or O(n)) for inserting an element
into each hierarchical data structure with n elements and tree height h?
(a) BinaryTree - O(n), BinarySearchTree - O(h), AVL Tree - O(log n)
(b) BinaryTree - O(h), BinarySearchTree - O(n), AVL Tree - O(h)
(c) BinaryTree - O(n), BinarySearchTree - O(n), AVL Tree - O(log n)
(d) BinaryTree - O(h), BinarySearchTree - O(h), AVL Tree - O(log n)
Ans :-a
For insertion
Binary tree: - O (n)
BST: - o(n) insertion complexity o (n) is also correct but here in the question
they are asking about best big O – run time
We will prefer O (n) once O (n)
Avl Tree :- o (logn )
Q40. In how many ways we can insert the elements {1, 2, . . . , 7} into an empty AVL tree so that
we don’t have to perform any rotations on it?___________
Ans :- 240
With 3 as root we can insert in 8 different combination of only
{3} {2, 6} {1, 3, 7} {4}
{3} {1,6} {2 5 7} {4}
{3} {2 5} {1, 4 6} {7}
{3} {1 5} {2 4 6} {7}
{3} {2 6} {1 4 7} {5}
{3} {1 6} {2 4 3} {5}
{3} {2 5} {1 4 7} {6}
{3} {1 5} {2 4 7} {6}
Where each of the combinations permutated in 2!3! =12 ways this give total if
12*8 i.e 96 incretion sequence with 3as root.
With 4 as root balanced all only kind of AVL is created and sequence is
{4}{2, 6}{1, 3, 5,7}which given 48 permutation
With 5 as root we have 8 insertion sequence with 12 permutation in each
{same as 3 as root} i.e total of 12* 8 ie 96 sequence
So total ways to insert keys in AVL without rotation is 48+2* 96=48+192=240
Q41. Suppose a binary tree holds 127 keys. Then our node-based implementation of that tree has
how many NULL pointers?
(a) 64 (b) 128
(c) 256
(d) The answer cannot be determined from the information given.
Ans :-b
If a tree holds n keys then there are n+1 null pointer
Example
Q42. Assuming each node in a BST takes 20 bytes of storage, how much memory is (in bytes)
necessary to store a “perfect” binary tree of height 3?__________
Ans :300
A perfect binary tree is a binary tree in which all interior nodes name two
children and all leaves have the same depth or same level
Total nodes=15
Total space =15x 20=300 byte.
Q43. Which of the following height is not possible for a binary tree with 90 nodes?
(a) 7 (b) 6
(c) 5 (d) None of these
Ans :-c
Considering height starts with O.
Maximum no.of nodes with
Height 7=255
Maximum no.of nodes with
Height 6 =26+1 -1=27
Maximum no of node with
Height 5 =25+1=63
With 90nodes height 5 is not possible .
Q44. 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 could not 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 & III (b) III & V
(c) III, IV & V (d) II, IV, V
Ans :- B
(I) 2, 252, 401, 393, 330, 344, 397, 363
2, 252, 33, 344
401, 298, 397
This is possible
(II) 924, 220, 911, 244, 898, 258, 362, 363
220, 244, 252, 362
363
924, 911, 898.
This is also possible
(III) 925, 202, 911, 240, 912, 245, 363
202, 240, 245
925, 911, 912
This is not possible because this should be in decreasing order
Q45. What does the following code mystery return when called with the root node of a given
binary tree?______
Assume isLeaf is the Boolean function return true if the left and right child of a node is
NULL.
int Mystery(Node *root)
{
int res = 0;
if (root != NULL)
{
if (isLeaf(root->left))
res += root->left->key;
else
res += Mystery(root->left);
res += Mystery(root->right);
}
return res;
}
Ans :- 48
My (49) my (52)
Q46. What does the following Mystery function return when called with the root node of a given
binary tree ____________
int Mystery ( Node *root)
{
int ms, ls, rs;
if ( root == NULL )
return 0;
else
{
ls = Mystery( root->left);
rs = Mystery( root->right);
ms = root->value + ls + rs;
return ms;
}
}
Ans :- 235
Post Order :- 5 15 12 9 23 50 52 49 20
Q49. What will be the printed value when called printOrder2with the root node of a given binary
tree?
void printOrder2(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
Ans C it is algorithm for inorder
Inorder =5,9,12,20,23,49,50,52
Q50. What will be the printed value when called printOrder3 with the root node of a given binary
tree?
void printOrder3(struct node* node)
{
if (node == NULL)
return;
printf("%d\t ", node->data);
printOrder3(node->left);
printOrder3(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
ans :d it is the algorithm for Preorder
Preorder :-20, 9 , 5,12,49,23,52,50