T - The type of value held in each node of this Tree.public class Tree<T>
extends java.lang.Object
| Constructor and Description |
|---|
Tree(T value)
Constructor for Tree objects.
|
| Modifier and Type | Method and Description |
|---|---|
void |
addChild(Tree<T> newChild)
Adds a child to this node, following any previously existing
children.
|
void |
addChildren(Tree<T>... newChildren)
Adds any number of children to this node, following any
previously existing children.
|
java.util.ArrayList<Tree<T>> |
children()
Returns a list of the children of this node.
|
int |
depth()
Returns the distance from the root of this node.
|
boolean |
equals(java.lang.Object o) |
Tree<T> |
firstChild()
Returns the leftmost child of this node, or
null if
this node has no children. |
T |
getValue()
Returns the value in this node of the tree.
|
boolean |
hasAncestor(Tree<T> ancestor)
Tests whether its parameter is an ancestor node of this node.
|
boolean |
hasChildren()
Tests whether this node is a leaf.
|
int |
hashCode() |
boolean |
hasNextSibling()
Tests whether this node has a sibling on its right.
|
boolean |
hasPreviousSibling()
Tests whether this node has a sibling on its left.
|
boolean |
isLeaf()
Tests whether this node is a leaf.
|
boolean |
isRoot()
Tests whether this tree node is the root.
|
java.util.Iterator<Tree<T>> |
iterator()
Returns a preorder iterator for this Tree.
|
Tree<T> |
lastChild()
Returns the rightmost child of this node, or
null if
this node has no children. |
Tree<T> |
nextSibling()
Returns the next child of the parent of this node,
or
null if there are no additional children. |
Tree<T> |
parent()
Returns the parent of this node, or
null if
this node is the root. |
Tree<T> |
previousSibling()
Returns the previous child of the parent of this node,
or
null if there are no prior children. |
void |
print()
Prints this Tree, in indented fashion.
|
void |
remove()
Detaches this node from its parent.
|
void |
setValue(T value)
Sets the value in this node of the tree.
|
java.lang.String |
toString()
Returns a single-line string representation of this Tree.
|
public Tree(T value)
value - The value to put in the new tree node.public T getValue()
public void setValue(T value)
value - The value to put in this node.public boolean equals(java.lang.Object o)
equals in class java.lang.ObjectObject.equals(java.lang.Object)public int hashCode()
hashCode in class java.lang.ObjectObject.hashCode()public boolean isRoot()
true if this node has no parent.public boolean isLeaf()
true if this node has no children.public boolean hasChildren()
true if this node has children.public boolean hasNextSibling()
true if this node has a right sibling.public boolean hasPreviousSibling()
true if this node has a left sibling.public Tree<T> parent()
null if
this node is the root.public Tree<T> firstChild()
null if
this node has no children.public Tree<T> lastChild()
null if
this node has no children.public java.util.ArrayList<Tree<T>> children()
public Tree<T> nextSibling()
null if there are no additional children.public Tree<T> previousSibling()
null if there are no prior children.public int depth()
public boolean hasAncestor(Tree<T> ancestor)
ancestor - A possible ancestor of this node.true if the parameter is an ancestor of this node.public void addChild(Tree<T> newChild)
newChild - The node to be added as a child.@SafeVarargs public final void addChildren(Tree<T>... newChildren)
newChildren - The nodes to be added as children.public void remove()
public java.util.Iterator<Tree<T>> iterator()
public java.lang.String toString()
toString in class java.lang.ObjectObject.toString()public void print()