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 right
child, where will the right child's position be in the array?
2*i+2
i+1
i+2
2*i+1
\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 left child, it is at 2*i+1
.
If you have a right child, it is at 2*i+2
.