com.discoversites.util.collections.tree.set
Class SetTree<T>

java.lang.Object
  extended by com.discoversites.util.collections.tree.set.SetTree<T>
Type Parameters:
T - The type of element to be stored in the SetTree
All Implemented Interfaces:
Tree<T>, Serializable, Iterable<T>

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

Implementation of Tree backed by a HashSet (actually a LinkedHashSet to maintain insertion order). This implementation works just like ArrayTree, except a given node can not have duplicate children.

Author:
Mark
See Also:
Serialized Form

Constructor Summary
SetTree()
           
SetTree(T element)
           
 
Method Summary
 int depth()
           
 Set<T> getBreadthFirstElements()
           
 Set<TreeNode<T>> getBreadthFirstNodes()
           
 Set<T> getDepthFirstElements()
           
 Set<TreeNode<T>> getDepthFirstNodes()
           
 Set<T> getElements()
           
 Set<TreeNode<T>> getNodes()
           
protected  Set<TreeNode<T>> getNodes(int depth)
          Gets all the nodes at a certain depth in the tree.
 TreeRoot<T> getRoot()
           
 Iterator<T> iterator()
           
 Iterator<TreeNode<T>> nodeIterator()
           
protected  void removeNode(TreeNode<T> node, int depth)
           
protected  void saveNode(TreeNode<T> node, int depth)
           
 int size()
           
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

SetTree

public SetTree()

SetTree

public SetTree(T element)
Parameters:
element - Element data to be placed in the root node of the tree.
Method Detail

saveNode

protected void saveNode(TreeNode<T> node,
                        int depth)
Parameters:
node - Node to be added to this tree.
depth - Depth (i.e. level) at which the node is to be added.

depth

public int depth()
Specified by:
depth in interface Tree<T>
Returns:
The depth of the tree, i.e. the number of levels the tree has.

getBreadthFirstElements

public Set<T> getBreadthFirstElements()
Specified by:
getBreadthFirstElements in interface Tree<T>
Returns:
An Collection that contains the elements stored in the tree's nodes. The elements are returned in breadth first order.

getBreadthFirstNodes

public Set<TreeNode<T>> getBreadthFirstNodes()
Specified by:
getBreadthFirstNodes in interface Tree<T>
Returns:
An Collection that contains the tree's nodes. The nodes are returned in breadth first order.

getDepthFirstElements

public Set<T> getDepthFirstElements()
Specified by:
getDepthFirstElements in interface Tree<T>
Returns:
An Collection that contains the elements stored in the tree's nodes. The elements are returned in depth first order.

getDepthFirstNodes

public Set<TreeNode<T>> getDepthFirstNodes()
Specified by:
getDepthFirstNodes in interface Tree<T>
Returns:
An Collection that contains the tree's nodes. The nodes are returned in depth first order.

getElements

public Set<T> getElements()
Specified by:
getElements in interface Tree<T>
Returns:
A Collection of all the elements in the tree. The order of the elements is not gauranteed by this method.

getNodes

public Set<TreeNode<T>> getNodes()
Specified by:
getNodes in interface Tree<T>
Returns:
A Collection of all nodes in the tree. The order of the nodes is not gauranteed by this method.

getRoot

public TreeRoot<T> getRoot()
Specified by:
getRoot in interface Tree<T>
Returns:
The root node of the tree

iterator

public Iterator<T> iterator()
Specified by:
iterator in interface Tree<T>
Specified by:
iterator in interface Iterable<T>
Returns:
An Iterator that iterates over the elements stored in the tree's nodes. The order the elements are returned is not gauranteed by this method.

nodeIterator

public Iterator<TreeNode<T>> nodeIterator()
Specified by:
nodeIterator in interface Tree<T>
Returns:
An Iterator that iterates over the tree's nodes. The order the nodes are returned is not gauranteed by this method.

size

public int size()
Specified by:
size in interface Tree<T>
Returns:
The number of elements in the tree.

getNodes

protected Set<TreeNode<T>> getNodes(int depth)
Gets all the nodes at a certain depth in the tree.

Parameters:
depth - The depth to retrieve nodes from.
Returns:
A Set of the nodes at this level.

removeNode

protected void removeNode(TreeNode<T> node,
                          int depth)
Parameters:
node - Node to be removed from this tree.
depth - Depth at which the node should be removed from.