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

Create successful ePaper yourself

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

Sec. 5.3 Binary Tree Node Implementations 165ABCDEFGHIFigure 5.8 Illustration of a typical pointer-based binary tree implementation,where each node s<strong>to</strong>res two child pointers <strong>and</strong> a value.−∗c∗+4 x ∗a2xFigure 5.9 An expression tree for 4x(2x + a) − c.larger <strong>to</strong> h<strong>and</strong>le the wider range of possible values. At the same time, leaf nodesneed not s<strong>to</strong>re child pointers.Java allows us <strong>to</strong> differentiate leaf from internal nodes through the use of classinheritance. As we have seen with lists <strong>and</strong> with class BinNode, a base classprovides a general definition for an object, <strong>and</strong> a subclass modifies a base class <strong>to</strong>add more detail. A base class can be declared for nodes in general, with subclassesdefined for the internal <strong>and</strong> leaf nodes. The base class of Figure 5.10 is namedVarBinNode. It includes a virtual member function named isLeaf, which indicatesthe node type. Subclasses for the internal <strong>and</strong> leaf node types each implementisLeaf. Internal nodes s<strong>to</strong>re child pointers of the base class type; they do not distinguishtheir children’s actual subclass. Whenever a node is examined, its versionof isLeaf indicates the node’s subclass.

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

Saved successfully!

Ooh no, something went wrong!