com.discoversites.util.collections.tree.array
Class ArrayTree<T>
java.lang.Object
com.discoversites.util.collections.tree.array.ArrayTree<T>
- Type Parameters:
T
- The type of element to be stored in the ArrayTree
- All Implemented Interfaces:
- Tree<T>, Serializable, Iterable<T>
public class ArrayTree<T>
- extends Object
- implements Tree<T>
Implementation of Tree
backed by an ArrayList
- Author:
- Mark
- See Also:
- Serialized Form
Methods inherited from class java.lang.Object |
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait |
ArrayTree
public ArrayTree()
ArrayTree
public ArrayTree(T element)
- Parameters:
element
- Element data to be placed in the root node of the tree.
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.
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.
getNodes
protected List<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
List
of the nodes at this level. The order of the nodes in the
list may be insertion order, but is not guaranteed and should not be relied upon.
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.
getBreadthFirstElements
public List<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 List<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 List<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 List<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.
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.
size
public int size()
- Specified by:
size
in interface Tree<T>
- Returns:
- The number of elements in the tree.
getElements
public List<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 List<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.