A / \ B C / \ | D E F
parent()
and root()
methods like those Bailey recommends, it becomes necessary for nodes
to include links to parents, and not just children.
parent()
method may
require some serious thought as to precise meaning.
/** The unique parent of the current node. Set to null for the root. */ TreeNode parent;
parent = null;
/** Set the parent link of all of our children. pre: The children array is initialized. post: Each child in that array considers the current node to be its parent. */ private void claimPaternity() { for(int i = 0; i < children.length; ++i) { if (children[i] != null) { children[i].parent = this; } } // for } // claimPaternity
setChild()
will clearly need to be modified to make this
same link to the parent (and unlinking for the old child).
public void setChild(int childnum, TreeNode child) { // Unlink the old child if (children[childnum] != null) children[childnum].parent = null; // Insert the child children[childnum] = child; // Update the child's paternity child.parent = this; } // setChild
parent()
is as simple as the other basic access methods.
public TreeNode parent() { return this.parent(); } // parent()
root()
requires us to continue to use the parent link
until we reach a node without a parent link.
/** Return the root node of the tree that contains the current node. pre: The tree is cycle-free (no node is its own ancestor). post: The root node is returned. */ public TreeNode root() { // Base case if (parent == null) return this; // Recursive case else return parent.root(); } // root
setArity()
function, or do we
use setChild()
and removeChild()
to change
the arity? I'm in favor of an explicit setArity()
function.
setRoot()
. I've decided not
to have root-specific functions, and to make the others behave
appropriately.
Tree()
-- Create a new tree with no elements.
Tree(Object rootvalue)
-- Create a new tree with a root
element.
Object getContents()
-- Get the contents of the current node,
if there is one.
Object getArity()
-- Get the arity of the current node,
if there is one.
void reset()
-- Move the cursor to the root of the tree.
void down(int childnum)
-- Move the cursor down to one of
the subtrees, given that the subtree exists.
void up()
-- Move the cursor up in the tree.
void setChild(int childnum, Object value)
-- Set one of the
children of the current node to a new leaf with the given value.
void setContents(Object value)
-- Set the contents of the
current
node to the given value. If the tree is empty, sets the value of the
root node to the given value.
void setArity(int arity)
-- Set the arity of the current
node.
void removeChild(int childnum)
-- Remove one of the children
of a node. Note that this is different than
setChild(childnum,null)
, which creates a child with value
null
.
boolean isEmpty()
-- Test whether or not the tree is empty.
Test before reset()
, setChild()
, and more.
boolean hasParent()
-- Test whether the current node has
a parent node. Test before calling up()
.
boolean hasChild(int childnum)
-- Test whether the current
node has a particular child. Test before calling
down(childnum)
.
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 Fri Nov 21 08:28:20 1997.
This page generated on Fri Nov 21 09:18:33 1997 by SiteWeaver.
Contact our webmaster at rebelsky@math.grin.edu