In a binary heap stored in an array with indices starting at 0, what are the indices of the children of the node at index i?
Correct answer A. 2i + 1 and 2i + 2
Explanation
With 0-based indexing the root is at 0 and its children at 1 and 2, so node i has children at 2i + 1 and 2i + 2, and its parent is at floor((i − 1) / 2). The formulas 2i and 2i + 1, with parent at floor(i / 2), belong to 1-based indexing, used by many textbooks that leave index 0 empty.
Answer checked How we write and check questions
Discussion