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

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

Sec. 6.5 Sequential Tree Implement<strong>at</strong>ions 213ABCDEFGHIFigure 6.17 Sample binary tree for sequential tree implement<strong>at</strong>ion examples.with two (possibly empty) children. Only null values will be interpreted as leafnodes, <strong>and</strong> these can be listed explicitly. Such an augmented node list providesenough inform<strong>at</strong>ion to recover the tree structure.Example 6.5 For the binary tree of Figure 6.17, the corresponding sequentialrepresent<strong>at</strong>ion would be as follows (assuming th<strong>at</strong> ‘/’ st<strong>and</strong>s fornull):AB/D//CEG///FH//I// (6.1)To reconstruct the tree structure from this node list, we begin by settingnode A to be the root. A’s left child will be node B. Node B’s left child isa null pointer, so node D must be B’s right child. Node D has two nullchildren, so node C must be the right child of node A.To illustr<strong>at</strong>e the difficulty involved in using the sequential tree represent<strong>at</strong>ionfor processing, consider searching for the right child of the root node. We must firstmove sequentially through the node list of the left subtree. Only <strong>at</strong> this point dowe reach the value of the root’s right child. Clearly the sequential represent<strong>at</strong>ionis space efficient, but not time efficient for descending through the tree along somearbitrary p<strong>at</strong>h.Assume th<strong>at</strong> each node value takes a constant amount of space. An examplewould be if the node value is a positive integer <strong>and</strong> null is indic<strong>at</strong>ed by the valuezero. From the Full Binary Tree Theorem of Section 5.1.1, we know th<strong>at</strong> the sizeof the node list will be about twice the number of nodes (i.e., the overhead fractionis 1/2). The extra space is required by the null pointers. We should be able tostore the node list more compactly. However, any sequential implement<strong>at</strong>ion mustrecognize when a leaf node has been reached, th<strong>at</strong> is, a leaf node indic<strong>at</strong>es the endof a subtree. One way to do this is to explicitly list with each node whether it isan internal node or a leaf. If a node X is an internal node, then we know th<strong>at</strong> its

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

Saved successfully!

Ooh no, something went wrong!