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...

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

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

Sec. 5.3 Binary Tree Node Implementations 169hidden from users of that tree class. On the other h<strong>and</strong>, if the nodes are objectsthat have meaning <strong>to</strong> users of the tree separate from their existence as nodes in thetree, then the version of Figure 5.11 might be preferred because hiding the internalbehavior of the nodes becomes more important.5.3.2 Space RequirementsThis section presents techniques for calculating the amount of overhead required bya binary tree implementation. Recall that overhead is the amount of space necessary<strong>to</strong> maintain the data structure. In other words, it is any space not used <strong>to</strong> s<strong>to</strong>redata records. The amount of overhead depends on several fac<strong>to</strong>rs including whichnodes s<strong>to</strong>re data values (all nodes, or just the leaves), whether the leaves s<strong>to</strong>re childpointers, <strong>and</strong> whether the tree is a full binary tree.In a simple pointer-based implementation for the binary tree such as that ofFigure 5.7, every node has two pointers <strong>to</strong> its children (even when the children arenull). This implementation requires <strong>to</strong>tal space amounting <strong>to</strong> n(2P + D) for atree of n nodes. Here, P st<strong>and</strong>s for the amount of space required by a pointer, <strong>and</strong>D st<strong>and</strong>s for the amount of space required by a data value. The <strong>to</strong>tal overhead spacewill be 2P n for the entire tree. Thus, the overhead fraction will be 2P/(2P + D).The actual value for this expression depends on the relative size of pointers versusdata fields. If we arbitrarily assume that P = D, then a full tree has about twothirds of its <strong>to</strong>tal space taken up in overhead. Worse yet, Theorem 5.2 tells us thatabout half of the pointers are “wasted” null values that serve only <strong>to</strong> indicate treestructure, but which do not provide access <strong>to</strong> new data.If only leaves s<strong>to</strong>re data values, then the fraction of <strong>to</strong>tal space devoted <strong>to</strong> overheaddepends on whether the tree is full. If the tree is not full, then conceivablythere might only be one leaf node at the end of a series of internal nodes. Thus,the overhead can be an arbitrarily high percentage for non-full binary trees. Theoverhead fraction drops as the tree becomes closer <strong>to</strong> full, being lowest when thetree is truly full. In this case, about one half of the nodes are internal.Great savings can be had by eliminating the pointers from leaf nodes in fullbinary trees. Because about half of the nodes are leaves <strong>and</strong> half internal nodes,<strong>and</strong> because only internal nodes now have overhead, the overhead fraction in thiscase will be approximatelyn2n2 (2P ) P=(2P ) + Dn P + D .

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

Saved successfully!

Ooh no, something went wrong!