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.

212 Chap. 6 Non-Binary Trees(a)(b)Figure 6.16 Full <strong>and</strong> complete 3-ary trees. (a) This tree is full (but not complete).(b) This tree is complete (but not full).6.5 Sequential Tree Implement<strong>at</strong>ionsNext we consider a fundamentally different approach to implementing trees. Thegoal is to store a series of node values with the minimum inform<strong>at</strong>ion needed toreconstruct the tree structure. This approach, known as a sequential tree implement<strong>at</strong>ion,has the advantage of saving space because no pointers are stored. It hasthe disadvantage th<strong>at</strong> accessing any node in the tree requires sequentially processingall nodes th<strong>at</strong> appear before it in the node list. In other words, node access muststart <strong>at</strong> the beginning of the node list, processing nodes sequentially in wh<strong>at</strong>everorder they are stored until the desired node is reached. Thus, one primary virtueof the other implement<strong>at</strong>ions discussed in this section is lost: efficient access (typicallyΘ(log n) time) to arbitrary nodes in the tree. Sequential tree implement<strong>at</strong>ionsare ideal for archiving trees on disk for l<strong>at</strong>er use because they save space, <strong>and</strong> thetree structure can be reconstructed as needed for l<strong>at</strong>er processing.Sequential tree implement<strong>at</strong>ions can be used to serialize a tree structure. Serializ<strong>at</strong>ionis the process of storing an object as a series of bytes, typically so th<strong>at</strong> thed<strong>at</strong>a structure can be transmitted between computers. This capability is importantwhen using d<strong>at</strong>a structures in a distributed processing environment.A sequential tree implement<strong>at</strong>ion typically stores the node values as they wouldbe enumer<strong>at</strong>ed by a preorder traversal, along with sufficient inform<strong>at</strong>ion to describethe tree’s shape. If the tree has restricted form, for example if it is a full binary tree,then less inform<strong>at</strong>ion about structure typically needs to be stored. A general tree,because it has the most flexible shape, tends to require the most additional shapeinform<strong>at</strong>ion. There are many possible sequential tree implement<strong>at</strong>ion schemes. Wewill begin by describing methods appropri<strong>at</strong>e to binary trees, then generalize to animplement<strong>at</strong>ion appropri<strong>at</strong>e to a general tree structure.Because every node of a binary tree is either a leaf or has two (possibly empty)children, we can take advantage of this fact to implicitly represent the tree’s structure.The most straightforward sequential tree implement<strong>at</strong>ion lists every nodevalue as it would be enumer<strong>at</strong>ed by a preorder traversal. Unfortun<strong>at</strong>ely, the nodevalues alone do not provide enough inform<strong>at</strong>ion to recover the shape of the tree. Inparticular, as we read the series of node values, we do not know when a leaf nodehas been reached. However, we can tre<strong>at</strong> all non-empty nodes as internal nodes

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

Saved successfully!

Ooh no, something went wrong!