repast.simphony.util.collections
Class NaryTree<T>

java.lang.Object
  extended by repast.simphony.util.collections.NaryTree<T>
All Implemented Interfaces:
Tree<T>

public class NaryTree<T>
extends Object
implements Tree<T>

A rooted tree where each node can have n number of children.

Version:
$Revision$ $Date$
Author:
Nick Collier

Nested Class Summary
protected  class NaryTree.ChildIterator<T>
           
protected  class NaryTree.NodeComparator<T>
           
 
Constructor Summary
NaryTree(T rootObj)
          Creates a NaryTree with the specified object as the root
 
Method Summary
 void addNode(T parent, T child)
          Adds the specified child to the tree as a child of the specified parent.
 boolean contains(T parent, T child)
          Checks if the tree contains the specified node with the specified parent.
protected  boolean containsChecker(repast.simphony.util.collections.NaryTree.Node<T> node, T parent, T child)
           
 Collection<T> getChildren(T obj)
          Gets the direct children of the specified node.
 T getRoot()
          Gets the root of the tree.
 Collection<T> getSiblings(T obj)
          Retrieves the siblings of the specified object in the tree.
protected  void preOrderTraveralsOfNodes(repast.simphony.util.collections.NaryTree.Node<T> node, TreeVisitor<repast.simphony.util.collections.NaryTree.Node<T>> visitor)
           
 void preOrderTraversal(TreeVisitor<T> visitor)
          Traverse the tree in preOrder - depth first, processing parents before children - applying the visitor to the nodes.
 boolean removeNode(T obj)
          Removes the specified object from the tree.
 void replaceNode(T oldObj, T newObj)
          Replaces the old object in the tree with the new one.
 int size()
          Gets the number of nodes currently in the tree.
 void sortChildren(Comparator<T> comparator)
          Sorts the children of each node w/r to each other according the specified comparator
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

NaryTree

public NaryTree(T rootObj)
Creates a NaryTree with the specified object as the root

Parameters:
rootObj - the root object
Method Detail

getRoot

public T getRoot()
Gets the root of the tree.

Specified by:
getRoot in interface Tree<T>
Returns:
the root of the tree.

getChildren

public Collection<T> getChildren(T obj)
Gets the direct children of the specified node. This will not return grand children etc.

Specified by:
getChildren in interface Tree<T>
Returns:
the direct children of the specified node.
Throws:
IllegalArgumentException - if the object is not currently in the tree.

addNode

public void addNode(T parent,
                    T child)
Adds the specified child to the tree as a child of the specified parent.

Specified by:
addNode in interface Tree<T>
Parameters:
parent - the parent node
child - the child node
Throws:
IllegalArgumentException - if the parent is not currently in the tree.

getSiblings

public Collection<T> getSiblings(T obj)
Retrieves the siblings of the specified object in the tree.

Parameters:
obj - the object whose siblings to get
Returns:
null if the passed in object is null, otherwise the object's siblings

removeNode

public boolean removeNode(T obj)
Removes the specified object from the tree. You cannot replace the root node, only #replaceNode(T, T) it.

Specified by:
removeNode in interface Tree<T>
Parameters:
obj - the object to remove
Returns:
true if the object was successfully removed, otherwise false.

replaceNode

public void replaceNode(T oldObj,
                        T newObj)
Replaces the old object in the tree with the new one.

Specified by:
replaceNode in interface Tree<T>
Parameters:
oldObj - the old object to replace
newObj - the new object
Throws:
IllegalArgumentException - if the old object is not currently in the tree.

sortChildren

public void sortChildren(Comparator<T> comparator)
Sorts the children of each node w/r to each other according the specified comparator

Specified by:
sortChildren in interface Tree<T>
Parameters:
comparator - the comparator used to sort the children

size

public int size()
Gets the number of nodes currently in the tree.

Specified by:
size in interface Tree<T>
Returns:
the number of nodes currently in the tree.

preOrderTraversal

public void preOrderTraversal(TreeVisitor<T> visitor)
Traverse the tree in preOrder - depth first, processing parents before children - applying the visitor to the nodes.

Specified by:
preOrderTraversal in interface Tree<T>
Parameters:
visitor - the visitor to apply to the nodes

preOrderTraveralsOfNodes

protected void preOrderTraveralsOfNodes(repast.simphony.util.collections.NaryTree.Node<T> node,
                                        TreeVisitor<repast.simphony.util.collections.NaryTree.Node<T>> visitor)

containsChecker

protected boolean containsChecker(repast.simphony.util.collections.NaryTree.Node<T> node,
                                  T parent,
                                  T child)

contains

public boolean contains(T parent,
                        T child)
Checks if the tree contains the specified node with the specified parent. This will stop when it finds the first node corresponding to the parent, meaning, if multiple nodes correspond to the parent, only the first one will be checked.

Specified by:
contains in interface Tree<T>
Parameters:
parent - the parent node
node - the child of the parent
Returns:
true if the tree contains the specified child node pair