Skip to main content
ICT
Lesson AB33 - PriorityQueues
 
Main   Previous Next
 

D. Storage of Complete Trees page 6 of 9

  1. The properties of a complete tree lead to a very efficient storage mechanism using n sequential locations in an array.

  2. 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. If we number the nodes from 1 at the root then

    1. the left and right children of node i, if they are present, are at 2i and 2i+1
    2. the parent of node i is at i/2 (truncated to an integer)

  3. If items is the array, the root corresponds to Items[1]; subsequent slots in the array store the nodes in each consecutive level from left to right.

  4. In a Java implementation, it is convenient to leave Items[0] unused. With this numbering of nodes, the children of the node Items[i] can be found in Items[2*i] and Items[2*i+1], and the parent of Items[i] is in Items[i/2].

 

Main   Previous Next
Contact
 © ICT 2006, All Rights Reserved.