GATE 2008
185 questions
MCQ 1851-mark 52 · 2-mark 133
Questions 91–95 of 185
Answer: (B)
The number of distinct BSTs with distinct keys is the Catalan number:
Here , so
Then
The same count comes from choosing the root. With root , the smaller keys form the left sub-tree and the other form the right sub-tree, so (with ).
Answer: (B)
I. 81,537,102,439,285,376,305
II. 52,97,121,195,242,381,472
III. 142,248,520,386,345,270,307
IV. 550,149,507,395,463,402,270
Which of the following statements is TRUE?
Answer: (C)
- (A) is wrong, because I and IV are not in ascending order. The inorder sequence of a BST is always in increasing order.
- (B) "I is a preorder sequence of some BST with as the root" is false. If were the root, it would have to be the first element in the preorder sequence, but the first element of I is .
- (C) is true. II is in increasing order, so it is a valid inorder sequence. Take as the root. The keys before it, , form the left sub-tree: make its root and the left child of . Then has no children, so it is a leaf. The keys after it, , form the right sub-tree in any valid shape.
- (D) "IV is a postorder sequence of some BST with as the root" is false. If were the root, it would have to be the last element in the postorder sequence, but the last element of IV is .
Answer: (C)
I. 81,537,102,439,285,376,305
II. 52,97,121,195,242,381,472
III. 142,248,520,386,345,270,307
IV. 550,149,507,395,463,402,270
Suppose the BST has been unsuccessfully searched for key 273. Which all of the above sequences list nodes in the order in which we could have encountered them in the search?
Answer: (D)
In a search for , at each node we go left if is smaller than the key and right if it is larger. A sequence can be the list of nodes visited only if each next key is on the side we went and stays inside the range set by all earlier nodes. The figures show each sequence as a path, with boxed where it would be placed.
In sequence , after we must go left because , so the next node cannot be . Not possible.
In sequence , after we must go left because , so the next node cannot be . Not possible.
In sequence , after we must go left because , so the next node cannot be . Not possible.
In sequence , every step is consistent: (right), (right), (left), (left), (left), (right, as ), and is within the range . At we go left and reach an empty place, so the search ends unsuccessfully. Possible.
So only sequence III works.
Answer: (D)
Answer: (A)
A) is true. An AVL tree is a balanced search tree, so searching takes . A binary search tree can be completely left or right skewed. In that case, searching takes .
Answer: (B)
The keys are exactly , so there is no need to search for anything. Take a subtree with keys that sits at positions of the postorder. Its root is at the last position. The left subtree has the keys , which is keys, and they fill the first positions. The right subtree has the keys and fills the positions after them, up to the one before the root. These positions are found in time, and the tree is fixed by them, so it is unique.
Then do the same on both parts. Every key becomes a node exactly once with work, and there are at most calls including empty subtrees. So the time is . Every key must be read at least once, so it is also . The time is .
(A) is impossible because just reading the input takes steps. (C) is not the most efficient. (D) is wrong because the tree is unique.
Answer: (B)