GATE 2004
199 questions
MCQ 1991-mark 199
Questions 106–110 of 199
(i) preorder and postorder
(ii) inorder and postorder
(iii) preorder and inorder
(iv) level order and postorder
Answer: (D)
(iii) Preorder and inorder: the first label of the preorder is the root. Its place in the inorder splits the other labels into left and right subtrees. Repeat on each part. So the tree is unique.
(ii) Inorder and postorder: the last label of the postorder is the root. Its place in the inorder splits the left and right subtrees. Repeat. So the tree is unique.
(i) Preorder and postorder: not unique. Take a root A with a single child B. If B is the left child, the preorder is AB and the postorder is BA. If B is the right child, the preorder is again AB and the postorder is again BA.
(iv) Level order and postorder: not unique, for the same pair of trees: level order AB and postorder BA fit both.
Without the inorder, a lone child can be on either side, so only (ii) and (iii) work.
Answer: (D)
Answer: (D)
Preorder, in-order and depth first search go deep into one branch before the next. For example, take a root a with children b and c, and a child d under b. Level order is a, b, c, d, but preorder is a, b, d, c.
Answer: (D)
Answer: (B)
Tree after inserting the numbers:
10 is the root. 1 goes left of 10, then 3 goes right of 1, then 5 goes right of 3. 15 goes right of 10, 12 goes left of 15 and 16 goes right of 15.
The longest path from the root is 10, 1, 3, 5. It has 3 edges, and the height is counted in edges, so the height is 3. (Counting nodes would give 4, which is option C.)
Answer: (B)
Answer: (B)
At the root, the tree is balanced, so each side has about leaves and the cost is .
At the next level there are 2 nodes. The cost of computing for each of them is . So the total cost at the second level is . In the same way, at each level (this is the total cost for the level, not the cost for one node) the cost is . A balanced tree has levels, so the total is .
The nodes on one level have no leaves in common, so each level costs at most in any balanced tree. That gives the upper bound , and the example above shows it is reached.
If we change to in the definition of , we still get the same answer.
Answer: (B)
Answer: (B)
Sifting down a node of height (its longest path down to a leaf) takes at most swaps. A heap of nodes has at most nodes of height . So the total work is
, since .
The simple estimate (option D) assumes that each of the about internal nodes may move down levels. It overcounts, because most nodes are near the bottom and can only move a few levels. Option A is too small, since all nodes must be looked at.
Answer: (B)