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. 5.3 Binary Tree Node Implement<strong>at</strong>ions 155/** Binary tree node implement<strong>at</strong>ion: Pointers to children@param E The d<strong>at</strong>a element@param Key The associ<strong>at</strong>ed key for the record */class BSTNode implements BinNode {priv<strong>at</strong>e Key key;// Key for this nodepriv<strong>at</strong>e E element;// Element for this nodepriv<strong>at</strong>e BSTNode left; // Pointer to left childpriv<strong>at</strong>e BSTNode right; // Pointer to right child}/** Constructors */public BSTNode() {left = right = null; }public BSTNode(Key k, E val){ left = right = null; key = k; element = val; }public BSTNode(Key k, E val,BSTNode l, BSTNode r){ left = l; right = r; key = k; element = val; }/** Get <strong>and</strong> set the key value */public Key key() { return key; }public void setKey(Key k) { key = k; }/** Get <strong>and</strong> set the element value */public E element() { return element; }public void setElement(E v) { element = v; }/** Get <strong>and</strong> set the left child */public BSTNode left() { return left; }public void setLeft(BSTNode p) { left = p; }/** Get <strong>and</strong> set the right child */public BSTNode right() { return right; }public void setRight(BSTNode p) { right = p; }/** @return True if a leaf node, false otherwise */public boolean isLeaf(){ return (left == null) && (right == null); }Figure 5.7 A binary tree node class implement<strong>at</strong>ion.As an example of a tree th<strong>at</strong> stores different inform<strong>at</strong>ion <strong>at</strong> the leaf <strong>and</strong> internalnodes, consider the expression tree illustr<strong>at</strong>ed by Figure 5.9. The expressiontree represents an algebraic expression composed of binary oper<strong>at</strong>ors such as addition,subtraction, multiplic<strong>at</strong>ion, <strong>and</strong> division. Internal nodes store oper<strong>at</strong>ors,while the leaves store oper<strong>and</strong>s. The tree of Figure 5.9 represents the expression4x(2x + a) − c. The storage requirements for a leaf in an expression tree are quitedifferent from those of an internal node. Internal nodes store one of a small set ofoper<strong>at</strong>ors, so internal nodes could store a small code identifying the oper<strong>at</strong>or suchas a single byte for the oper<strong>at</strong>or’s character symbol. In contrast, leaves store variablenames or numbers, which is considerably larger in order to h<strong>and</strong>le the widerrange of possible values. At the same time, leaf nodes need not store child pointers.

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

Saved successfully!

Ooh no, something went wrong!