Skip to main content

GATE CSE 2024Data Structures › Heaps & Priority QueuesVerified

GATE CSE 2024 PYQ: Consider a binary min-heap containing 105 105 105 distinct elements. Let k k k be the index (in the…

Consider a binary min-heap containing 105 distinct elements. Let kk be the index (in the underlying array) of the maximum element stored in the heap. The number of possible values of kk is

  1. A
    53
    Correct answer
  2. B
    52
  3. C
    27
  4. D
    1

Answer

(A) 53 53 53

Approach

A binary min-heap is a complete binary tree stored in an array, and it satisfies the heap property: every parent is smaller than or equal to its children. Therefore no internal (non-leaf) node can hold the maximum element, because each such node has at least one child that is strictly smaller. The maximum must reside at a leaf, so k is exactly the set of leaf indices. Count the leaves of the complete binary tree with 105 nodes.

In a min-heap every internal node is smaller than at least one of its children, so the maximum element must be located at a leaf. A complete binary tree with 105 nodes has ⌊105/2⌋ = 52 internal nodes, hence 105 − 52 = 53 leaves, giving 53 possible values of k. Answer: A.

Reviewed by GateIQ Editorial Team: IIT & IISc Alumni.