Question 51

Computer Awareness Heaps Easy

Given the index i of a node in a heap, we can not compute:

(A) Parent (i)
(B) Heap size
(C) Left (i)
(D) Right (i)
View Dynamic Solution & Explanation
Correct Solution: Option B

Step-by-step Solution:

Solution

In a heap (implemented as an array), given the index i of a node:

  • The parent index can be calculated as \( \text{Parent}(i) = \lfloor i/2 \rfloor \) (for 1-based indexing) or \( \lfloor (i-1)/2 \rfloor \) (for 0-based indexing).
  • The left child index can be calculated as \( \text{Left}(i) = 2i \) (1-based) or \( 2i+1 \) (0-based).
  • The right child index can be calculated as \( \text{Right}(i) = 2i+1 \) (1-based) or \( 2i+2 \) (0-based).

However, the heap size is not computable from the index of a node alone; it is stored separately.

Answer: Option B → Heap size