Say you have [12, 11, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1
] in your heap. You would have 12 as the top parent. Would 11, and 10 be the only children of 12 because they're the highest of them all?
It sounds obvious, but my code looks right, and I'm get getting conflicting results. Instead of 11 and 10 as the children of 12, I'm getting 11 and 7. Both 11 and 7 have children of lower values than them, so my code doesn't want to keep sorting it.
My main question would be can 7 be the child of 12, as long as the children of 7 are of lower value?
Yes, the 7
can be the right child of the root in the Heap Tree, storing a sequence of integers from 1
to 12
(inclusive). Consider the following linear array, representing this tree with usual dependency between position of a node and positions of its children:
V = [12, 11, 7, 10, 9, 3, 2, 8, 6, 5, 4, 1]
The Heap Property is valid for this array, cause for all i
in the range [0, 6]
:
V[i] > V[2*i + 1]
V[i] > V[2*i + 2]