Consider any array representation of an element binary heap where the elements are stored from index 1 to index of the array. For the element stored at index of the array , the index of the parent is
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…
- A
- BCorrect answer
- C
- D
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.