Skip to main content

GATE CSE 2001Data Structures › Heaps & Priority QueuesVerified

GATE CSE 2001 PYQ: Consider any array representation of an n n n element binary heap where the elements are stored from index 1…

Consider any array representation of an nn element binary heap where the elements are stored from index 1 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

  1. A
    i−1i-1
  2. B
    ⌊i2⌋\lfloor \frac{i}{2} \rfloor
    Correct answer
  3. C
    ⌈i2⌉\lceil \frac{i}{2} \rceil
  4. D
    (i+1)2\frac{(i+1)}{2}

Answer

(B) ⌊ i 2 ⌋ \lfloor \frac{i}{2} \rfloor ⌊ 2 i ​ ⌋

Approach

For a binary heap stored in a 1-indexed array, the heap property links a node at index $i$ to its children at indices $2i$ and $2i+1$. Reversing this relationship gives the parent index: if a node is at index $i\>1$, its parent must be at $\lfloor i/2\rfloor$. We can verify the formula using small indices.

In a 1-indexed array representation of a binary heap, children of index $i$ are at $2i$ and $2i+1$. Hence the parent of index $i$ is at $\left\lfloor \frac{i}{2} \right\rfloor$. Therefore option B is correct.

Reviewed by GateIQ Editorial Team: IIT & IISc Alumni.