Skip to main content

GATE CSE 2021Data Structures › Heaps & Priority QueuesVerified

GATE CSE 2021 PYQ: ​​​​​Let H H H be a binary min-heap consisting of n n n elements implemented as an array. What is the worst…

​​​​​Let HH be a binary min-heap consisting of nn elements implemented as an array. What is the worst case time complexity of an optimal algorithm to find the maximum element in HH?

  1. A
    Θ(1)\Theta (1)
  2. B
    Θ(log⁡n)\Theta (\log n)
  3. C
    Θ(n)\Theta (n)
    Correct answer
  4. D
    Θ(nlog⁡n)\Theta (n \log n)

Answer

(C) Θ ( n ) \Theta (n) Θ ( n )

Approach

Use the structural property of a binary min-heap: every parent is ≤ its children, so the largest key can never sit at an internal node that has a child — it must be one of the leaves. Since leaf keys are not stored in sorted order, the only reliable strategy is to examine all leaves, and that linear scan is also a matching lower bound.

Because a min-heap's maximum is guaranteed to be a leaf and the leaves are mutually unordered, an optimal search examines every leaf once: ⌈n/2⌉ comparisons, i.e. Θ(n). Option C.

Reviewed by GateIQ Editorial Team: IIT & IISc Alumni.