Practice mode Data Structures

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.

Report a problem

Answer checked How we write and check questions

Tip: press A, B, C or D to answer, and N for the next question.

Advertisement

Have a question about this answer?

Your email address will not be published. Required fields are marked *

Links and website addresses are not allowed in comments.