T
- the value type of the tree nodepublic final class TreeNode<T> extends Object implements Tree<T,TreeNode<T>>, Iterable<TreeNode<T>>, Copyable<TreeNode<T>>, Serializable
TreeNode
is a
mutable implementation of the Tree
interface.Modifier and Type | Method and Description |
---|---|
TreeNode<T> |
attach(T... children)
Attaches the given
children to this node. |
TreeNode<T> |
attach(T child)
Attaches the given
child to this node. |
TreeNode<T> |
attach(TreeNode<T> child)
Remove the given
child from its parent and makes it a child of
this node by adding it to the end of this node's child array. |
int |
childCount()
Return the number of children this tree node consists of.
|
TreeNode<T> |
copy() |
TreeNode<T> |
detach()
Detaches the subtree rooted at
this node from the tree, giving
this node a null parent. |
boolean |
equals(Object obj) |
TreeNode<T> |
getChild(int index)
Returns the child at the specified index in this node's child array.
|
Optional<TreeNode<T>> |
getParent()
Returns this node's parent if available.
|
T |
getValue()
Return the node value
|
int |
hashCode() |
TreeNode<T> |
insert(int index,
TreeNode<T> child)
Removes the
child from its present parent (if it has one), sets
the child's parent to this node, and then adds the child to this node's
child array at index index . |
Iterator<TreeNode<T>> |
iterator()
Return an iterator that traverses the subtree rooted at
this node
in pre-order. |
static <T> TreeNode<T> |
of()
Return a new
TreeNode with a null tree value. |
static <T> TreeNode<T> |
of(T value)
Return a new
TreeNode with the given node value . |
static <T> TreeNode<T> |
ofTree(Tree<? extends T,?> tree)
Return a new
TreeNode from the given source tree . |
TreeNode<T> |
remove(int index)
Removes the child at the specified index from this node's children and
sets that node's parent to
null . |
void |
remove(Tree<?,?> child)
Remove the
child from this node's child array, giving it
a null parent. |
void |
removeAllChildren()
Removes all children fo
this node and setting their parents to
null . |
void |
setValue(T value)
Sets the user object for this node.
|
String |
toString() |
clone, finalize, getClass, notify, notifyAll, wait, wait, wait
breadthFirstIterator, breadthFirstStream, childAfter, childBefore, childIterator, childStream, depth, depthFirstIterator, depthFirstStream, equals, firstChild, firstLeaf, getIndex, getPath, getRoot, hashCode, identical, isAncestor, isChild, isDescendant, isLeaf, isRelated, isRoot, isSibling, lastChild, lastLeaf, leafCount, level, nextLeaf, nextNode, nextSibling, pathFromAncestorIterator, postorderIterator, postorderStream, preorderIterator, preorderStream, previousLeaf, previousNode, previousSibling, sharedAncestor, siblingCount, size, stream, toCompactString, toDottyString, toString
forEach, spliterator
public void setValue(T value)
value
- the node value
public TreeNode<T> getChild(int index)
public int childCount()
Tree
childCount
in interface Tree<T,TreeNode<T>>
public Iterator<TreeNode<T>> iterator()
this
node
in pre-order. The first node returned by the iterator is this
node.
Modifying the tree by inserting, removing, or moving a node invalidates any iterator created before the modification.
public TreeNode<T> insert(int index, TreeNode<T> child)
child
from its present parent (if it has one), sets
the child's parent to this node, and then adds the child to this node's
child array at index index
. The new child
must not be
null
and must not be an ancestor of this
node.index
- the index in the child array where this node is to be
insertedchild
- the sub-node to be insertedthis
tree-node, for method chainingArrayIndexOutOfBoundsException
- if index
is out of boundsIllegalArgumentException
- if child
is an ancestor of
this
nodeNullPointerException
- if the given child
is null
public TreeNode<T> remove(int index)
null
. The child node to remove must be
a MutableTreeNode
.index
- the index in this node's child array of the child to removethis
tree-node, for method chainingArrayIndexOutOfBoundsException
- if the index
is out of
boundspublic TreeNode<T> detach()
this
node from the tree, giving
this
node a null
parent. Does nothing if this
node is the root of its tree.this
public void remove(Tree<?,?> child)
child
from this
node's child array, giving it
a null
parent.child
- the child of this node to removeNullPointerException
- if the given child
is null
IllegalArgumentException
- if the given child
is not a
child of this nodepublic void removeAllChildren()
this
node and setting their parents to
null
. If this
node has no children, this method does
nothing.public TreeNode<T> attach(TreeNode<T> child)
child
from its parent and makes it a child of
this node by adding it to the end of this node's child array.child
- the new child added to this nodethis
tree-node, for method chainingNullPointerException
- if the given child
is null
@SafeVarargs public final TreeNode<T> attach(T... children)
children
to this
node.children
- the children to attach to this
nodethis
tree-node, for method chainingNullPointerException
- if the given children
array is
null
public TreeNode<T> attach(T child)
child
to this
node.child
- the child to attach to this
nodethis
tree-node, for method chainingpublic static <T> TreeNode<T> ofTree(Tree<? extends T,?> tree)
TreeNode
from the given source tree
. The
whole tree is copied.T
- the tree value typetree
- the source tree the new tree-node is created fromTreeNode
from the given source tree
NullPointerException
- if the source tree
is null
public static <T> TreeNode<T> of(T value)
TreeNode
with the given node value
.T
- the tree-node typevalue
- the node value© 2007-2018 Franz Wilhelmstötter (2018-05-31 12:39)