Next: Priority Queues
Up: Array Representation
Previous: Array Representation
  Index
Arithmetic of Tree Traversal
An important benefit of the array representation is that we can move
around the tree, from parent to children or from child to parent, by
simple arithmetic. In general
- the children of node
are at
and
- the parent of node
is at
where division, to obtain the parent, returns the integer part. Of
course node
may not have any children, or it may only have one
child. Suppose there are
nodes in the tree. Then node
has a
left child if
and a right child if
. Conversely,
node
has a parent if
, otherwise it is the root.
You should note that some authors leave the first element of the array
empty, so that the data items are stored in elements
.
The children are then at
and
and the parent is at
.
This might make for slightly more efficient code, but overall our
coding will be simpler if we load the array from
.
Next: Priority Queues
Up: Array Representation
Previous: Array Representation
  Index
Peter Williams
2005-06-07