/** The root of the tree. Set to null if there are no elements in
the tree. */
TreeNode root;
/** The current element of the tree. Set to null if it's unclear what
the current element is. */
TreeNode current;
/**
* Create an empty tree.
* pre: There is sufficient memory.
* post: The tree is initialized.
*/
public Tree() {
root = null;
current = null;
} // Tree()
/**
* Create a tree with one node.
* pre: There is sufficient memory to create the tree.
* pre: The value is non-null.
* post: The tree is initialized.
* post: The root has zero-arity.
*/
public Tree(Object rootvalue) {
root = new TreeNode(rootvalue,0);
current = root;
} // Tree(Object)
/**
* Get the contents of the current postion in the tree.
* pre: There is a current position.
* post: The value at that position is returned.
*
* @throws TreeException when there is no current node.
*/
public Object getContents() throws TreeException {
if (current == null) {
throw new TreeException("No current node.");
}
return current.getContents();
} // getContents()
/**
* Get the arity of the current position in the tree.
* pre: The current position is known.
* post: The number of direct subtrees is returned.
*/
public Object getArity() throws TreeException {
if (current == null) {
throw new TreeException("No current node.");
}
return current.getArity();
} // getArity()
setContents() and setArity() methods
are similar enough that we won't worry about writing them.
setChild() we'll need to do a little bit more work,
including
/**
* Set one of the children of the current position to a leaf with a
* particular value.
* pre: There is a current position.
* pre: The arity of the current position is sufficient to permit
* setting one of its children.
* pre: Memory is available to create a new node.
* post: The tree is modified appropriately.
*/
public void setChild(int childnum, Object value) throws TreeException {
if (current == null) {
throw new TreeException("No current position.");
}
// We don't need to check arity because the TreeNode
// setChild does so.
TreeNode child = new TreeNode(value,0);
current.setChild(childnum, child);
} // setChild
/**
* Advance the cursor to a child.
* pre: There is a current position.
* pre: The current position has sufficient arity.
* pre: The current position has the appropriate child.
* post: The cursor is advanced.
* post: If any of the preconditions are not met, the cursor
* remains on the current node.
*
* @throws TreeException if any of the preconditions fail.
*/
public void down(int childnum) throws TreeException {
// Get the child, throwing exceptions when necessary
current = verifiedGetChild(current,childnum);
} // down()
/**
* Ensure that a node has a child and return that child.
* pre: There is a current position.
* pre: The current position has sufficient arity.
* pre: The current position has the appropriate child.
* post: The child is returned.
*
* @throws TreeException if any of the preconditions are not met.
*/
private static void verifiedGetChild(TreeNode node, int childnum)
throws TreeException
{
// Ensure current node.
if (node == null) {
throw new TreeException("No current position.");
}
// Ensure arity
if (childnum >= node.getArity()) {
throw new TreeException("Insufficient arity.");
}
// Ensure child
TreeNode child = node.getChild(childnum);
if (child == null) {
throw new TreeException("No such child.");
}
// Otherwise we're done
return child;
} // verifiedGetChild
/**
* Move the cursor up the tree.
* pre: The current position is defined.
* pre: There is a parent of the current position.
* post: The current position is changed to the parent.
* post: If any precondition fails, the cursor stays in the
* same place.
*
* @throws TreeException if any of the preconditions are not met.
*/
public void up() {
// Ensure current node
if (current == null) {
throw new TreeException("No current position.");
}
// Ensure parent
TreeNode tmp = current.getParent();
if (tmp == null) {
throw new TreeException("No parent.");
}
// Do it
current = tmp;
} up()
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.
Source text last modified Mon Nov 24 08:06:27 1997.
This page generated on Mon Nov 24 09:00:45 1997 by SiteWeaver.
Contact our webmaster at rebelsky@math.grin.edu