> of children. There is
no restriction on the number of children that a particular node may have.
This Tree provides a method to serialize the Tree into a List by doing a
pre-order traversal. It has several methods to allow easy updation of Nodes
in the Tree.
Taken from: http://sujitpal.blogspot.ca/2006/05/java-data-structure-generic-tree.html
Constructor Summary |
Tree()
Default ctor. |
Tree(Node<T> root)
Creates a tree from the supplied root |
Tree(T data)
Creates a tree from the supplied root |
Method Summary |
void |
clear()
|
void |
consolidate()
Merges branches if the nodes have equal content
i.e. |
static void |
consolidate(Tree<?> tree)
Merges branches if the nodes have equal content
i.e. |
Node<T> |
getRootElement()
Return the root Node of the tree. |
void |
setRootElement(Node<T> rootElement)
Set the root Element for the tree. |
java.util.List<Node<T>> |
toList()
Returns the Tree as a List of Node objects. |
java.lang.String |
toString()
Returns a String representation of the Tree. |
Methods inherited from class java.lang.Object |
equals, getClass, hashCode, notify, notifyAll, wait, wait, wait |
Tree
public Tree()
- Default ctor.
Tree
public Tree(Node<T> root)
- Creates a tree from the supplied root
- Parameters:
root
- the root element.
Tree
public Tree(T data)
- Creates a tree from the supplied root
- Parameters:
data
- the data for the root element.
getRootElement
public Node<T> getRootElement()
- Return the root Node of the tree.
- Returns:
- the root element.
setRootElement
public void setRootElement(Node<T> rootElement)
- Set the root Element for the tree.
- Parameters:
rootElement
- the root element to set.
toList
public java.util.List<Node<T>> toList()
- Returns the Tree as a List of Node objects. The elements of the
List are generated from a pre-order traversal of the tree.
- Returns:
- a List>.
toString
public java.lang.String toString()
- Returns a String representation of the Tree. The elements are generated
from a pre-order traversal of the Tree.
- Overrides:
toString
in class java.lang.Object
- Returns:
- the String representation of the Tree.
clear
public void clear()
consolidate
public static void consolidate(Tree<?> tree)
- Merges branches if the nodes have equal content
i.e. family -> sister -> Anne
-> sister -> Josephine
becomes
family -> sister -> Anne
-> Josephine
consolidate
public void consolidate()
- Merges branches if the nodes have equal content
i.e. family -> sister -> Anne
-> sister -> Josephine
becomes
family -> sister -> Anne
-> Josephine