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.

154 Chap. 5 Binary Trees5.3 Binary Tree Node Implement<strong>at</strong>ionsIn this section we examine ways to implement binary tree nodes. We begin withsome options for pointer-based binary tree node implement<strong>at</strong>ions. Then comes adiscussion on techniques for determining the space requirements for a given implement<strong>at</strong>ion.The section concludes with an introduction to the array-based implement<strong>at</strong>ionfor complete binary trees.5.3.1 Pointer-Based Node Implement<strong>at</strong>ionsBy definition, all binary tree nodes have two children, though one or both childrencan be empty. Binary tree nodes typically contain a value field, with the type ofthe field depending on the applic<strong>at</strong>ion. The most common node implement<strong>at</strong>ionincludes a value field <strong>and</strong> pointers to the two children.Figure 5.7 shows a simple implement<strong>at</strong>ion for the BinNode abstract class,which we will name BSTNode. Class BSTNode includes a d<strong>at</strong>a member of typeE, (which is the second generic parameter) for the element type. To support searchstructures such as the Binary Search Tree, an additional field is included, withcorresponding access methods, to store a key value (whose purpose is explainedin Section 4.4). Its type is determined by the first generic parameter, named Key.Every BSTNode object also has two pointers, one to its left child <strong>and</strong> another to itsright child. Figure 5.8 illustr<strong>at</strong>es the BSTNode implement<strong>at</strong>ion.Some programmers find it convenient to add a pointer to the node’s parent,allowing easy upward movement in the tree. Using a parent pointer is somewh<strong>at</strong>analogous to adding a link to the previous node in a doubly linked list. In practice,the parent pointer is almost always unnecessary <strong>and</strong> adds to the space overhead forthe tree implement<strong>at</strong>ion. It is not just a problem th<strong>at</strong> parent pointers take space.More importantly, many uses of the parent pointer are driven by improper underst<strong>and</strong>ingof recursion <strong>and</strong> so indic<strong>at</strong>e poor programming. If you are inclined towardusing a parent pointer, consider if there is a more efficient implement<strong>at</strong>ion possible.An important decision in the design of a pointer-based node implement<strong>at</strong>ionis whether the same class definition will be used for leaves <strong>and</strong> internal nodes.Using the same class for both will simplify the implement<strong>at</strong>ion, but might be aninefficient use of space. Some applic<strong>at</strong>ions require d<strong>at</strong>a values only for the leaves.Other applic<strong>at</strong>ions require one type of value for the leaves <strong>and</strong> another for the internalnodes. Examples include the binary trie of Section 13.1, the PR quadtree ofSection 13.3, the Huffman coding tree of Section 5.6, <strong>and</strong> the expression tree illustr<strong>at</strong>edby Figure 5.9. By definition, only internal nodes have non-empty children.If we use the same node implement<strong>at</strong>ion for both internal <strong>and</strong> leaf nodes, then bothmust store the child pointers. But it seems wasteful to store child pointers in theleaf nodes. Thus, there are many reasons why it can save space to have separ<strong>at</strong>eimplement<strong>at</strong>ions for internal <strong>and</strong> leaf nodes.

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

Saved successfully!

Ooh no, something went wrong!