Consider a node R
of a complete binary tree
whose value is stored in position i
of an array
representation for the tree. If R
has a left
child, where will the left child's position be in the array?
2*i+1
i+1
i+2
2*i+2
\lfloor (i-1)/2 \rfloor
If you have a right sibling, it is at i+1
.
If you have a left sibling, it is at i-1
.
If you have a parent, it is at \lfloor (i-1)/2 \rfloor
.
If you have a right child, it is at 2*i+2
.
If you have a left child, it is at 2*i+1
.