2
$\begingroup$

I've been reading about min heaps, currently looking at this article, and I am very confused by something.

The article makes the following statement:

If a given node is located at index 'x' in the array, its left child exists at index = 2x, and its right child exists at index = 2x + 1. Each node's parent exists at index = x / 2 (rounded down).

It gives this example:

enter image description here

This min heap is represented in the array [100, 19, 36, 17, 3, 25, 1, 2, 7].

But this doesn't jive with the preceding statement about indexing. 36 can be found at index 2, and 2 * 2 is 4. But index 4 is 3-- the right child of the 19 node, not the left child of the 36 node.

What am I missing here?

$\endgroup$
2
$\begingroup$

Start indexing at $1$, then everything will be fine.

Want to start at $0$? That is no problem, but one has to adjust the formula's. Children of $x$ can be found at $2x+1$ and $2x+2$.

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.