12.07.2015 Views

A Practical Introduction to Data Structures and Algorithm Analysis

A Practical Introduction to Data Structures and Algorithm Analysis

A Practical Introduction to Data Structures and Algorithm Analysis

SHOW MORE
SHOW LESS
  • No tags were found...

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

Sec. 6.5 Sequential Tree Implementations 225two children (which may be subtrees) immediately follow X in the node list. If Xis a leaf node, then the next node in the list is the right child of some ances<strong>to</strong>rof X, not the right child of X. In particular, the next node will be the child of X’smost recent ances<strong>to</strong>r that has not yet seen its right child. However, this assumesthat each internal node does in fact have two children, in other words, that thetree is full. Empty children must be indicated in the node list explicitly. Assumethat internal nodes are marked with a prime ( ′ ) <strong>and</strong> that leaf nodes show no mark.Empty children of internal nodes are indicated by ‘/’, but the (empty) children ofleaf nodes are not represented at all. Note that a full binary tree s<strong>to</strong>res no nullvalues with this implementation, <strong>and</strong> so requires less overhead.Example 6.6 We can represent the tree of Figure 6.17 as follows:A ′ B ′ /DC ′ E ′ G/F ′ HI (6.2)Note that slashes are needed for the empty children because this is not a fullbinary tree.S<strong>to</strong>ring n bits can be a considerable savings over s<strong>to</strong>ring n null values. InExample 6.6, each node is shown with a mark if it is internal, or no mark if it is aleaf. This requires that each node value has space <strong>to</strong> s<strong>to</strong>re the mark bit. This mightbe true if, for example, the node value were s<strong>to</strong>red as a 4-byte integer but the rangeof the values sored was small enough so that not all bits are used. An examplewould be if all node values must be positive. Then the high-order (sign) bit of theinteger value could be used as the mark bit.Another approach is <strong>to</strong> s<strong>to</strong>re a separate bit vec<strong>to</strong>r <strong>to</strong> represent the status of eachnode. In this case, each node of the tree corresponds <strong>to</strong> one bit in the bit vec<strong>to</strong>r. Avalue of ‘1’ could indicate an internal node, <strong>and</strong> ‘0’ could indicate a leaf node.Example 6.7 The bit vec<strong>to</strong>r for the tree if Figure 6.17 would be11001100100 (6.3)S<strong>to</strong>ring general trees by means of a sequential implementation requires thatmore explicit structural information be included with the node list. Not only mustthe general tree implementation indicate whether a node is leaf or internal, it mustalso indicate how many children the node has. Alternatively, the implementationcan indicate when a node’s child list has come <strong>to</strong> an end. The next example dispenseswith marks for internal or leaf nodes. Instead it includes a special mark (we

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!