Question
Consider the following statements:
I. The smallest element in a max-heap is always at a leaf node
II. The second largest element in a max-heap is always a child of the root node
III. A max-heap can be constructed from a binary search tree in time
IV. A binary search tree can be constructed from a max-heap in time
Which of the above statements are TRUE?
Official answer and solution
- Official answer
- A
- From
- Official GATE 2019 answer key
Solution
Evaluate each statement concerning max-heaps and binary search trees.
Statement I: In a max-heap, every node is greater than or equal to its children (). If the smallest element were at an internal node, its children would have to be smaller than it, contradicting that it is the smallest element. Thus, the smallest element must be at a leaf node. (TRUE)
Statement II: The root holds the maximum element. The second largest element cannot be dominated by any element other than the root. Therefore, it must be located directly at one of the children of the root (index 1 or 2). (TRUE)
Statement III: To construct a max-heap from a BST of elements, we can extract the elements via inorder traversal in time and place them into an array. Then, running Floyd's build_max_heap on the array takes time. The total time is . (TRUE)
Statement IV: If a BST could be constructed from a max-heap in time, then since an inorder traversal of a BST outputs elements in sorted order in time, we would be able to sort an arbitrary array in comparison time (build max-heap in , construct BST in , traverse BST in ). This contradicts the well-established lower bound for comparison-based sorting. Hence, a BST cannot be constructed from a max-heap in time in general. (FALSE)
Thus, statements I, II, and III are TRUE.
A
The answer is the one in the official GATE answer key. The solution is this site's own explanation.
Source
- Paper
- GATE 2019 Computer Science and Information Technology (CS)
- Question
- 50 of 65, printed as CS Q.40
- Section
- Computer Science and Information Technology
Report an error in this question
GATE questions are the work of the organising institutes. This site is independent and is not affiliated with them.