11.07.2015 Views

Data Structures and Algorithm Analysis - Computer Science at ...

Data Structures and Algorithm Analysis - Computer Science at ...

Data Structures and Algorithm Analysis - Computer Science at ...

SHOW MORE
SHOW LESS

Create successful ePaper yourself

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

214 Chap. 6 Non-Binary Treestwo children (which may be subtrees) immedi<strong>at</strong>ely follow X in the node list. If Xis a leaf node, then the next node in the list is the right child of some ancestorof X, not the right child of X. In particular, the next node will be the child of X’smost recent ancestor th<strong>at</strong> has not yet seen its right child. However, this assumesth<strong>at</strong> each internal node does in fact have two children, in other words, th<strong>at</strong> thetree is full. Empty children must be indic<strong>at</strong>ed in the node list explicitly. Assumeth<strong>at</strong> internal nodes are marked with a prime ( ′ ) <strong>and</strong> th<strong>at</strong> leaf nodes show no mark.Empty children of internal nodes are indic<strong>at</strong>ed by ‘/’, but the (empty) children ofleaf nodes are not represented <strong>at</strong> all. Note th<strong>at</strong> a full binary tree stores no nullvalues with this implement<strong>at</strong>ion, <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 th<strong>at</strong> slashes are needed for the empty children because this is not a fullbinary tree.Storing n extra bits can be a considerable savings over storing n null values.In Example 6.6, each node is shown with a mark if it is internal, or no mark if itis a leaf. This requires th<strong>at</strong> each node value has space to store the mark bit. Thismight be true if, for example, the node value were stored as a 4-byte integer butthe range of the values sored was small enough so th<strong>at</strong> not all bits are used. Anexample would be if all node values must be positive. Then the high-order (sign)bit of the integer value could be used as the mark bit.Another approach is to store a separ<strong>at</strong>e bit vector to represent the st<strong>at</strong>us of eachnode. In this case, each node of the tree corresponds to one bit in the bit vector. Avalue of ‘1’ could indic<strong>at</strong>e an internal node, <strong>and</strong> ‘0’ could indic<strong>at</strong>e a leaf node.Example 6.7 The bit vector for the tree if Figure 6.17 (including positionsfor the null children of nodes B <strong>and</strong> E) would be11001100100 (6.3)Storing general trees by means of a sequential implement<strong>at</strong>ion requires th<strong>at</strong>more explicit structural inform<strong>at</strong>ion be included with the node list. Not only mustthe general tree implement<strong>at</strong>ion indic<strong>at</strong>e whether a node is leaf or internal, it mustalso indic<strong>at</strong>e how many children the node has. Altern<strong>at</strong>ively, the implement<strong>at</strong>ioncan indic<strong>at</strong>e when a node’s child list has come to 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!