public class Tree<T>
extends java.lang.Object
List<Node<T>>
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.
Modified from: http://sujitpal.blogspot.ca/2006/05/java-data-structure-generic-tree.htmlConstructor and Description |
---|
Tree()
Default ctor.
|
Tree(Node<T> root)
Creates a tree from the supplied root
|
Tree(T data)
Creates a tree from the supplied root
|
Modifier and Type | Method and Description |
---|---|
void |
clear() |
void |
consolidate()
Merges branches if the nodes have equal content.
|
static void |
consolidate(Tree<?> tree)
Merges branches if the nodes have equal content.
|
boolean |
equalsTree(Tree<T> tree)
Check if the value of this tree equals another
|
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<T> as a List of Node<T> objects. |
java.lang.String |
toString()
Returns a String representation of the Tree.
|
public Tree()
public Tree(Node<T> root)
root
- the root element.public Tree(T data)
data
- the data for the root element.public boolean equalsTree(Tree<T> tree)
tree
- tree to compare withtrue
if tree
equals this treepublic Node<T> getRootElement()
public void setRootElement(Node<T> rootElement)
rootElement
- the root element to set.public java.util.List<Node<T>> toList()
Tree<T>
as a List of Node<T>
objects. The
elements of the List are generated from a pre-order traversal of the
tree.List<Node<T>>
.public java.lang.String toString()
toString
in class java.lang.Object
public void clear()
public static void consolidate(Tree<?> tree)
family -> sister -> Anne
-> sister -> Josephine
becomes
family -> sister -> Anne
-> Josephine
public void consolidate()
family -> sister -> Anne
-> sister -> Josephine
becomes
family -> sister -> Anne
-> Josephine