Tree - DPP 02 - Merged
Tree - DPP 02 - Merged
[MCQ]
1. 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?
(a) (1 2 (4 5 6 7))
(b) (1 (2 3 4) 5 6) 7)
(c) (1 (2 3 4) (5 6 7)) The in-order traversal of T is-
(d) (1 (2 3 NULL) (4 5)) (a) 7 1 3 4 6 5 2 8
(b) 4 1 6 7 3 2 5 8
[MCQ] (c) 4 6 1 2 8 5 3 7
2. Consider the following two statements: (d) 7 1 4 6 3 5 2 8
S1: It is possible to construct a binary tree uniquely
whose post-order and pre-order traversals are given. [MCQ]
S2: It is possible to construct a binary tree uniquely 5. Consider the following binary tree T-
whose in-order and pre-order traversals are given.
S3: It is possible to construct a binary tree uniquely
whose post-order and level-order traversals are given.
Which of the following statement(s) IS/ARE
INCORRECT?
(a) S1 only
(b) S2 only
(c) S1 and S3
(d) S3 only The pre-order traversal of T is-
(a) 7 1 3 4 6 5 2 8
[MCQ] (b) 4 1 6 7 3 2 5 8
3. Let LASTPOST, LASTIN and LASTPRE denote the (c) 4 6 1 2 8 5 3 7
last vertex visited in a postorder, inorder and preorder (d) 7 1 4 6 3 5 2 8
traversal respectively, of a complete binary tree.
Which of the following is always true? [MCQ]
(a) LASTIN = LASTPOST 6. Consider the following binary tree T-
(b) LASTIN = LASTPRE
(c) LASTPRE = LASTPOST
(d) None of the above
[MCQ]
4. Consider the following binary tree T-
(a) 71346528
(b) 41673258 [MCQ]
(c) 46128537 8. The post-order traversal of a binary tree is 9, 7, 4, 8,
(d) 71463528 2, 5, 1, 3, 6. The in-order traversal of the same tree is
9, 7, 8, 4, 5, 2, 6, 3, 1. The pre-order traversal of the
[NAT] above binary tree is-
7. The pre-order traversal of a binary tree is 1, 2, 4, 7, 8, (a) 1, 2, 4, 7, 9, 8, 5, 3, 6
3, 5, 6, 9. The in-order traversal of the same tree is 7 (b) 1, 2, 4, 7, 8, 9, 5, 3, 6
4 8 2 1 5 3 6 9. The height of a tree is the length of the (c) 1, 2, 3, 4, 5, 6, 7, 8, 9
longest path from the root to any leaf. The height of (d) None of the above.
the binary tree above is ______.
3
Answer Key
1. (c) 6. (c)
2. (c) 7. (3)
3. (d) 8. (a)
4. (b)
5. (d)
4
1. (c) 6. (c)
Correct The post-order traversal of T is - 4 6 1 2 8 5 3 7
7. (3)
2. (c)
It is possible to construct a binary tree uniquely whose
in-order and pre-order/post-order traversals are given.
Height of the above binary tree = 3
3. (d)
8. (a)
In order: 1 4 2 6 3 5
Pre-order: 6 4 1 2 5 3
Post-order: 1 2 4 3 5 6
Clearly, LASTIN LASTPRE LASTPOST
4. (b)
The in-order traversal of T is- 4 1 6 7 3 2 5 8
5. (d)
The pre-order traversal of T is- 7 1 4 6 3 5 2 8 The pre-order traversal of the above binary tree is-
1, 2, 4, 7, 9, 8, 5, 3, 6
Answer Key
1. (14) 5. (b)
2. (336) 6. (a)
3. (14) 7. (d)
4. (b)
3
4. (b)
7. (d)
Pre-order traversal of BST:
struct treenode{
7 3 2 1 5 4 6 8 10 9 11
struct treenode *left;
In-order traversal of BST:
int data;
1 2 3 4 5 6 7 8 9 10 11
struct treenode *right;
Tree is constructed as-
};
void f(struct treenode *t, int x){
if(t==NULL) return NULL;
elseif(x==t->data) return t;
else if (x<t->data) return f(t->left, x);
else return f(t->right, x);
}
CSE/IT Batch-Hinglish
Data Structure
Tree DPP-04
Answer Key
1. (b) 5. (c)
2. (a) 6. (b)
3. (a) 7. (c)
4. (c) 8. (d)
5
1. (b)
The function returns 1 iff the two trees are mirror 6. (b)
images of each other. The output string is “AEFFEABDDBCC”.
2. (a) 7. (c)
The function returns 1 iff the two trees are identical The function returns the minimum element in a binary
each other. search tree.
3. (a) 8. (b)
The function returns 1 iff the binary tree is left-skewed.
P: INCORRECT. The minimum number of nodes in a
4. (c) complete binary tree is 2h.
The function returns 1 iff the binary tree is right- Q: INCORRECT.A binary search tree is may not be a
skewed BST. complete binary tree.
5. (c)
The function computes the total number of nodes in a
binary tree.
[NAT] [MSQ]
1. The minimum number of nodes in AVL tree of height 5. Consider the following operations in a BST-
6 is _______. INSERT(23), INSERT(17), INSERT(25), INSERT(4),
(Assume that the height of the root node is 1) INSERT(21), INSERT(1), INSERT(7), DELETE(17),
DELETE(23).
[MCQ] The post-order traversal of the resultant BST is-
2. Consider the following statements: (a) 1, 7, 4, 21, 25
P: An AVL tree is a height-balanced complete binary (b) 1, 4, 7, 25, 21
tree. (c) 1, 4, 21, 7, 25
Q: A heap is necessarily a complete binary tree. (d) None of the above
Which of the following statement(s) is/are
CORRECT? [MSQ]
(a) P only 6. Which of the following sequence(s) of array form a
(b) Q only heap?
(c) Both P and Q (a) 23, 17, 14, 6, 13, 10, 1, 12, 7, 5
(d) Neither P nor Q (b) 1, 5, 10, 6, 7, 12, 13, 14, 17, 23
(c) 23, 17, 14, 7, 13, 10, 1, 5, 6, 12
[NAT] (d) 1, 5, 10, 12, 13, 7, 14, 17, 23, 6
3. The total number of ways in which a max-heap can be
constructed with the keys-7, 6, 1, 4, 5, 2, 3 is ________. [NAT]
7. Consider the following statements:
[MCQ] P: The accepted balanced factor in an AVL tree are –1,
4. Consider the following statements: 0 and +1.
P: If the root node of a BST is deleted, it can be Q: The height of an AVL tree with n nodes is given as
replaced by inorder predecessor. log2n.
Q: If the root node of a BST is deleted, it can be The number of INCORRECT statements is ______.
replaced by preorder successor.
Which of the following is/are CORRECT? [NAT]
(a) P only 8. Construct an AVL tree with the following keys:
(b) Q only 12, 10, 15, 14, 13, 17, 8
(c) Both P and Q The immediate left child key value of the root node of
(d) Neither P nor Q the AVL tree is __________.
2
Answer Key
1. (20) 5. (a, b, c)
2. (b) 6. (b, c)
3. (80) 7. (0)
4. (a) 8. (12)
3
2. (b)
P: INCORRECT. An AVL tree is not necessarily a
complete binary tree.
Post order traversal: -
Q: CORRECT. A heap is necessarily a complete
binary tree. 1 7 4 21 25
3. (80)
T(n) = 1 * ( ) * T(k) * T(n – k – 1)
n –1
k
Here n = 7, k = 3
T(7) = 1 * ( 36 ) * T(3) * T(3)
Now, T(3) = 2
T(7) = 1 * ( 36 ) * 2 * 2 = 80 Post order traversal: -
1 4 7 25 21
4. (a)
If the root node of a BST is deleted, it can be replaced
by inorder predecessor/successor.
5. (a, b, c)
6. (b, c)
(a)
4
8. (12)
Resultant AVL tree:
Answer Key
1. (511) 5. (8)
2. (10) 6. (b)
3. (5) 7. (d)
4. (a)
3
1. (511)
The maximum element will be present in the leaf
nodes.
Number of leaf nodes in the min-heap of 1024 elements
= 1024/2 = 512
Maximum number of comparisons to find the
maximum element in a min heap of 1024 elements =
512 – 1 = 511. 3. (5)
2. (10)
4
4. (a)
6. (b)
The resultant max-heap is-
80, 70, 60, 50, 40, 10, 40, 5, 15
7. (d)
P: INCORRECT. The number of comparisons
required to find the minimum element in a min
heap of n elements is 1.
Q: INCORRECT. Only one comparison is not
sufficient to find the minimum element in a max
heap of n elements.
5. (8)
5
[MCQ]
1. Which of the following is/are correct inorder traversal
sequence(s) of binary search tree(s)?
I. 3, 5, 7, 8, 15, 19, 25
II. 5, 8, 9, 12, 10, 15, 25
III. 2, 7, 10, 8, 14, 16, 20
IV. 4, 6, 7, 9 18, 20, 25
(a) I and IV (b) II and III
(c) II and IV (d) II only
[MCQ]
2. What is the worst-case time complexity of inserting n2 [MCQ]
elements into an AVL-tree with n elements initially? 6. A Binary Search Tree (BST) stores values in the range
(a) O(n2) (b) O(n2logn) 37 to 573. Consider the following sequence of keys.
4
(c) O(n ) (d) O(n3)
I. 81, 537, 102, 439, 285, 376, 305
II. 52, 97, 121, 195, 242, 381, 472
[MCQ] III. 142, 248, 520, 386, 345, 270, 307
3. Suppose the numbers 7, 5, 1, 8, 3, 6, 0, 9, 4, 2 are IV. 550, 149, 507, 395, 463, 402, 270
inserted in that order into an initially empty binary
Suppose the BST has been unsuccessfully searched for
search tree. The binary search tree uses the usual
ordering on natural numbers. What is the pre-order key 273. Which all of the above sequences list nodes
traversal sequence of the resultant tree? in the order in which we could have encountered them
(a) 7 5 1 0 3 2 4 6 8 9 in the search?
(b) 0 2 4 3 1 6 5 9 8 7 (a) I and III (b) II and III
(c) 0 1 2 3 4 5 6 7 8 9 (c) III and IV (d) III only
(d) 9 8 6 4 2 3 0 1 5 7
[NAT]
[MCQ] 7. A complete n-ary tree is a tree in which each node has
4. Consider the following statements.
n children or no children. Let I be the number of
S1: The sequence of procedure calls corresponds to a
preorder traversal of the activation tree. internal nodes and L be the number of leaves in a
S2: The sequence of procedure returns corresponds to complete n-ary tree. If L = 41, and I = 10, what is the
a postorder traversal of the activation tree. value of n? _____________.
Which one of the following options is correct?
(a) S1 only (b) S2 only [MCQ]
(c) Both S1 and S2 (d) Neither S1 nor S2 8. A Priority-Queue is implemented as a Max-Heap.
Initially, it has 5 elements. The level-order traversal of
[NAT] the heap is given below: 10, 8, 5, 3, 2. Two new
5. Consider the expression tree shown. Each leaf elements ‘1’ and ‘7’ are inserted in the heap in that
represents a numerical value, which can either be 0 or
order. The level-order traversal of the heap after the
1. Over all possible choices of the values at the leaves,
the maximum possible value of the expression insertion of the elements is:
represented by the tree is ___. (a) 10, 8, 7, 3, 2, 1, 5
(b) 10, 8, 7, 2, 3, 1, 5
(c) 10, 8, 7, 3, 2, 5, 1
(d) None of the above
3
Answer Key
1. (a) 6. (d)
2. (b) 7. (5)
3. (a) 8. (a)
4. (c)
5. (6)
4
1. (a) 6. (d)
The inorder traversal of BST is always in sorted order.
2. (b)
The worst-case time complexity of inserting n2
elements into an AVL-tree with n elements initially is
O(n2logn)
3. (a)
Preorder traversal
7510324689
4. (c)
Both S1 and S2 are CORRECT.
5. (6)
5
8. (a)
7. (5)
L = (N –1) * I + 1
L = 41, I = 10 –
41 = (N –1) * 10 + 1
40
N –1 = =4
10
N=4+1=5