Skip to content
Official PYQ · 2019

GATE CS 2019 Q40 — Binary Heaps

Explain with AI

Consider the following statements:

  1. The smallest element in a max-heap is always at a leaf node
  2. The second largest element in a max-heap is always a child of a root node
  3. A max-heap can be constructed from a binary search tree in Θ(n)\Theta(n) time
  4. A binary search tree can be constructed from a max-heap in Θ(n)\Theta(n) time

Which of the above statements are TRUE?

Choose one option
View worked solution

Answer: A

Let us analyze each statement individually:

  • Statement I: The smallest element in a max-heap is always at a leaf node. (TRUE)
    In a max-heap, the parent node is always greater than or equal to its children. If the smallest element were an internal node, its children would have to be smaller than or equal to it, which contradicts the assumption that it is the smallest element. Thus, the minimum element must reside in one of the leaf nodes.

  • Statement II: The second largest element in a max-heap is always a child of a root node. (TRUE)
    The root node contains the largest element of the heap. Since any node in the heap is less than or equal to its parent, the second largest element cannot be further down the tree (as it would be smaller than its ancestors). Therefore, it must be one of the direct children of the root (either the left child or the right child).

  • Statement III: A max-heap can be constructed from a binary search tree in Θ(n)\Theta(n) time. (TRUE)
    We can perform an in-order (or any) traversal of the BST to copy its nn elements into an array in Θ(n)\Theta(n) time. Then, we can apply the standard bottom-up Build-Max-Heap algorithm on this array, which runs in Θ(n)\Theta(n) time. Thus, the overall construction takes Θ(n)\Theta(n) time.

  • Statement IV: A binary search tree can be constructed from a max-heap in Θ(n)\Theta(n) time. (FALSE)
    If we could construct a BST from a max-heap in Θ(n)\Theta(n) time, we could sort nn elements in O(n)O(n) comparison-based time as follows:
    1. Build a max-heap from nn unsorted elements in O(n)O(n) time.
    2. Convert the max-heap to a BST in O(n)O(n) time.
    3. Perform an in-order traversal of the BST in O(n)O(n) time to get the sorted order.
    This would violate the Ω(nlog⁡n)\Omega(n \log n) lower bound for comparison-based sorting. Hence, this conversion is not possible in Θ(n)\Theta(n) time.

Since statements I, II, and III are true, the correct option is A.

Free practice · Your answers on this page are not saved.

Published
Report a correction
Keep exploringMore Binary Heaps questions