Skip to content
Official PYQ · 2001

GATE CS 2001 Q1.15 — Binary Heaps

Explain with AI

Consider any array representation of an nn element binary heap where the elements are stored from index 11 to index nn of the array. For the element stored at index ii of the array (i≤n)(i \leq n), the index of the parent is

Choose one option
View worked solution

Answer: B

In a standard 1-based array representation of a binary heap, the elements are mapped as follows:

  • The root of the heap is stored at index 11.
  • For any node at index ii (1≤i≤n1 \leq i \leq n):
    • Its left child is located at index 2i2i (if 2i≤n2i \leq n).
    • Its right child is located at index 2i+12i + 1 (if 2i+1≤n2i + 1 \leq n).

To find the parent of a node at index ii (where i>1i > 1), we reverse this relationship:

  • If the node is a left child, its index is 2k2k, so its parent is at index k=i2k = \frac{i}{2}.
  • If the node is a right child, its index is 2k+12k + 1, so its parent is at index k=i−12k = \frac{i - 1}{2}.

Combining both cases using the floor function, the parent of the node at index ii is always located at index:

⌊i2⌋\lfloor \frac{i}{2} \rfloor

Example:

  • For the node at index 33, its parent is at ⌊32⌋=1\lfloor \frac{3}{2} \rfloor = 1.
  • For the node at index 22, its parent is at ⌊22⌋=1\lfloor \frac{2}{2} \rfloor = 1.

Therefore, the correct option is B.

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

Published
Report a correction
Keep exploringMore Binary Heaps questions