trees
Class Tree<V>

java.lang.Object
  extended by trees.Tree<V>
Type Parameters:
V - The type of value allowed in Tree nodes.

public class Tree<V>
extends java.lang.Object

General tree API.

Version:
January 30, 2007
Author:
David Matuszek

Constructor Summary
Tree(V value)
          Constructor for Tree objects.
 
Method Summary
 void addChild(Tree<V> newChild)
          Adds the given node as the new last child of this node.
 void addChildren(java.util.ArrayList<Tree<V>> newChildren)
          Adds the given nodes as new children of this node.
 java.util.ArrayList<Tree<V>> children()
          Returns a genericized list of the children of this node.
 int depth()
          Returns the depth of this node in the tree; that is, it returns the distance to the root.
 boolean equals(java.lang.Object o)
           
 Tree<V> firstChild()
          Returns the first child of this node, or null if this node has no children.
 V getValue()
          Returns the value in this node of the tree.
 boolean hasAncestor(Tree<V> ancestor)
          Tests if the given node is the same as this node, or is an ancestor of this node.
 boolean hasChildren()
          Tests whether this node is a leaf.
 boolean hasNextSibling()
          Tests whether there are nodes "to the right" of this node.
 boolean hasPreviousSibling()
          Tests whether there are nodes "to the left" of this node.
 boolean isLeaf()
          Tests whether this node is a leaf.
 boolean isRoot()
          Tests whether this tree node is the root.
 java.util.Iterator<Tree<V>> iterator()
          Returns an iterator that will step through this tree in preorder.
 Tree<V> lastChild()
          Returns the last child of this node, or null if this node has no children.
static void main(java.lang.String[] args)
          Tests parsing and printing of trees; this is only a test method, not to be used for other purposes.
 Tree<V> nextSibling()
          Returns the next sibling of this node, or null if this node is the rightmost node.
 Tree<V> parent()
          Returns the parent of this node, or null if this is a root node.
static Tree<java.lang.String> parse(java.lang.String s)
          Parses a string of the general form value(child, child, ..., child) and returns the corresponding tree.
 Tree<V> previousSibling()
          Returns the previous sibling of this node, or null if this node is the leftmost node.
 void print()
          Prints this tree as an indented structure.
 void remove()
          Removes this node from whatever tree it may be in.
 void setValue(V value)
          Sets the value in this node of the tree.
 java.lang.String toString()
          Returns a string representing this tree.
 
Methods inherited from class java.lang.Object
clone, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Constructor Detail

Tree

public Tree(V value)
Constructor for Tree objects. Creates a single node containing the specified value.

Parameters:
value - The value to put in the new tree node.
Method Detail

getValue

public V getValue()
Returns the value in this node of the tree.

Returns:
The value in this node of the tree.

setValue

public void setValue(V value)
Sets the value in this node of the tree.

Parameters:
value - The value to put in this node.

equals

public boolean equals(java.lang.Object o)
Overrides:
equals in class java.lang.Object
See Also:
Object.equals(java.lang.Object)

isRoot

public boolean isRoot()
Tests whether this tree node is the root.

Returns:
true if this node has no parent.

isLeaf

public boolean isLeaf()
Tests whether this node is a leaf.

Returns:
true if this node has no children.

hasChildren

public boolean hasChildren()
Tests whether this node is a leaf.

Returns:
true if this node has children.

hasNextSibling

public boolean hasNextSibling()
Tests whether there are nodes "to the right" of this node.

Returns:
true if this node is not the last child.

hasPreviousSibling

public boolean hasPreviousSibling()
Tests whether there are nodes "to the left" of this node.

Returns:
true if this node is not the first child.

parent

public Tree<V> parent()
Returns the parent of this node, or null if this is a root node.

Returns:
The parent of this node.

firstChild

public Tree<V> firstChild()
Returns the first child of this node, or null if this node has no children.

Returns:
The first child of this node.

lastChild

public Tree<V> lastChild()
Returns the last child of this node, or null if this node has no children.

Returns:
The last child of this node.

children

public java.util.ArrayList<Tree<V>> children()
Returns a genericized list of the children of this node. If the node has no children, an empty list is returned.

Returns:
The children of this node.

nextSibling

public Tree<V> nextSibling()
Returns the next sibling of this node, or null if this node is the rightmost node.

Returns:
The next sibling of this node.

previousSibling

public Tree<V> previousSibling()
Returns the previous sibling of this node, or null if this node is the leftmost node.

Returns:
The previous sibling of this node.

depth

public int depth()
Returns the depth of this node in the tree; that is, it returns the distance to the root.

Returns:
The depth of this node.

hasAncestor

public boolean hasAncestor(Tree<V> ancestor)
Tests if the given node is the same as this node, or is an ancestor of this node.

Parameters:
ancestor - The node that may be an ancestor of this node.
Returns:
True if the given node is an ancestor.

addChild

public void addChild(Tree<V> newChild)
Adds the given node as the new last child of this node.

Parameters:
newChild - The node to be added.

addChildren

public void addChildren(java.util.ArrayList<Tree<V>> newChildren)
Adds the given nodes as new children of this node. The nodes are added after any existing nodes.

Parameters:
newChildren - The nodes to be added.

remove

public void remove()
Removes this node from whatever tree it may be in. The children of this node (if any) stay with this node.


iterator

public java.util.Iterator<Tree<V>> iterator()
Returns an iterator that will step through this tree in preorder.

Returns:
A preorder iterator.

toString

public java.lang.String toString()
Returns a string representing this tree. The string does not contain newlines. The general form of the output is:
value(child, child, ..., child).

Overrides:
toString in class java.lang.Object
See Also:
Object.toString()

print

public void print()
Prints this tree as an indented structure.


parse

public static Tree<java.lang.String> parse(java.lang.String s)
Parses a string of the general form value(child, child, ..., child) and returns the corresponding tree. Children may be separated by commas and/or spaces. Node values are all Strings.

Parameters:
s - The String to be parsed.
Returns:
The resultant Tree.

main

public static void main(java.lang.String[] args)
Tests parsing and printing of trees; this is only a test method, not to be used for other purposes.

Parameters:
args - Unused.