[Instructions] [Search] [Current] [Syllabus] [Links] [Handouts] [Outlines] [Labs] [More Labs] [Assignments] [Quizzes] [Examples] [Book] [Tutorial] [API]
Back to Project Discussion, Continued. On to Heaps and Heap Sort.
Held Monday, April 26
Summary
Contents
Handouts
Notes
Does it fly?
yes / \ no
/ \
Is it a bird? Does it swim?
yes / \ no ...
/ \
... Is it nocternal?
yes / \
/ ...
Bat
+
/ \
3 -
/ \
* 6
/ \
4 5
getChild) or do we have cursors that
traverse the tree?
atLeaf operation).
/**
* Evaluate a binary "expression tree" at or below a cursor.
*/
public int evaluate(Cursor expression) {
// If we're at a leaf, it's a number.
if (expression.isLeaf()) return
computeBaseValue(expression.getValue());
// If we're not at a leaf, it's an operator.
else {
Operator op = (Operator) expression.getValue();
Cursor left = expression.getChild(0);
Cursor right = expression.getChild(1);
int leftVal = evaluate(left);
int rightVal = evaluate(right);
return apply(op, leftVal, rightVal);
}
} // evaluate(Cursor)
public interface Tree {
/** Create a new empty tree. */
public Tree();
/** Determine the depth of the tree. */
public int depth();
/** Determine the number of values in the tree. */
public int size();
/** Return a cursor for the root. */
public Cursor root();
} // interface Tree
public interface Cursor {
/** Create a new cursor for a tree. */
public Cursor(Tree t);
/**
* Get the cursor for a child. The children are numbered
* from 0 to #children-1.
* Pre: The current subtree has that child.
*/
public Cursor getChild(int childNum);
/** Find out how many children are below the current cursor. */
public int numChildren();
/** Ensure that the nth child is defined. */
public boolean hasChild(int childNum);
...
} // interface Cursor
public class TreeNode {
/** The contents of the current node. */
protected Object contents;
/** The children of the current node. */
protected Vector children;
/** Create a new node with no children. */
public TreeNode(Object value) {
this.contents = value;
this.children = new Vector();
} // TreeNode(Object)
/** Set a child. */
public void setChild(int childNum, TreeNode child) {
// Make sure the vector is big enough.
if (children.size() <= childNum) {
children.setSize(childNum+1);
}
// Set the appropriate element.
this.setElementAt(child, childNum);
} // setChild(int, TreeNode)
/** Check if it has a child. */
public boolean hasChild(int childNum) {
return (children.size() <= childNum) &&
(children.elementAt(childNum) != null);
} // hasChild(int)
/**
* Get a child.
* Pre: Has that child.
*/
public TreeNode getChild(int childNum) {
return children.elementAt(childNum);
} // getChild
// ...
} // class TreeNode
History
Back to Project Discussion, Continued. On to Heaps and Heap Sort.
[Instructions] [Search] [Current] [Syllabus] [Links] [Handouts] [Outlines] [Labs] [More Labs] [Assignments] [Quizzes] [Examples] [Book] [Tutorial] [API]
Disclaimer Often, these pages were created "on the fly" with little, if any, proofreading. Any or all of the information on the pages may be incorrect. Please contact me if you notice errors.
This page may be found at http://www.math.grin.edu/~rebelsky/Courses/CS152/99S/Outlines/outline.45.html
Source text last modified Mon Apr 26 10:19:13 1999.
This page generated on Mon Apr 26 10:25:34 1999 by SiteWeaver. Validate this page's HTML.
Contact our webmaster at rebelsky@math.grin.edu