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. |
TreeNode<T> |
childAt(int index)
Returns the child at the specified index in 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) |
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 . |
<B> TreeNode<B> |
map(Function<? super T,? extends B> mapper)
Returns a new
TreeNode consisting of all nodes of this
tree, but with a different value type, created by applying the given
function to the node values of this tree. |
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 . |
static <T,B> TreeNode<B> |
ofTree(Tree<? extends T,?> tree,
Function<? super T,? extends B> mapper)
Return a new
TreeNode from the given source tree . |
static TreeNode<String> |
parse(String tree)
Parses a (parentheses) tree string, created with
Tree.toParenthesesString() . |
static <B> TreeNode<B> |
parse(String tree,
Function<? super String,? extends B> mapper)
Parses a (parentheses) tree string, created with
Tree.toParenthesesString() . |
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 . |
boolean |
removeAtPath(Tree.Path path)
Removes the child at the given
path . |
TreeNode<T> |
replace(int index,
TreeNode<T> child)
Replaces the child at the give index with the given
child |
boolean |
replaceAtPath(Tree.Path path,
TreeNode<T> child)
Replaces the child at the given
path with the given new
child . |
void |
setValue(T value)
Sets the user object for this node.
|
String |
toString() |
clone, finalize, getClass, notify, notifyAll, wait, wait, wait
breadthFirstIterator, breadthFirstStream, childAfter, childAtPath, childAtPath, childBefore, childIterator, childPath, childStream, depth, depthFirstIterator, depthFirstStream, equals, firstChild, firstLeaf, getPath, getRoot, hashCode, identical, indexOf, isAncestor, isChild, isDescendant, isLeaf, isRelated, isRoot, isSibling, iterator, lastChild, lastLeaf, leafCount, level, nextLeaf, nextNode, nextSibling, path, pathElements, pathFromAncestorIterator, postorderIterator, postorderStream, preorderIterator, preorderStream, previousLeaf, previousNode, previousSibling, sharedAncestor, siblingCount, size, stream, toParenthesesString, toParenthesesString, toString
forEach, spliterator
public void setValue(T value)
value
- the node value
public TreeNode<T> childAt(int index)
public int childCount()
Tree
childCount
in interface Tree<T,TreeNode<T>>
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> replace(int index, TreeNode<T> child)
child
index
- the index of the child which will be replacedchild
- the new childthis
tree-node, for method chainingArrayIndexOutOfBoundsException
- if the 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
.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 boolean removeAtPath(Tree.Path path)
path
. If no child exists at the
given path, nothing is removed.path
- the path of the child to replacetrue
if a child at the given path
existed and
has been removedNullPointerException
- if one of the given argument is null
public boolean replaceAtPath(Tree.Path path, TreeNode<T> child)
path
with the given new
child
. If no child exists at the given path, nothing is replaced.path
- the path of the child to replacechild
- the new childtrue
if a child at the given path
existed and
has been replacedNullPointerException
- if one of the given argument is null
public 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 <B> TreeNode<B> map(Function<? super T,? extends B> mapper)
TreeNode
consisting of all nodes of this
tree, but with a different value type, created by applying the given
function to the node values of this
tree.B
- the new node typemapper
- the node value mapperthis
treeNullPointerException
- if the given mapper
function is
null
public static <T> TreeNode<T> of()
TreeNode
with a null
tree value.T
- the tree-node typepublic static <T> TreeNode<T> of(T value)
TreeNode
with the given node value
.T
- the tree-node typevalue
- the node valuepublic static <T,B> TreeNode<B> ofTree(Tree<? extends T,?> tree, Function<? super T,? extends B> mapper)
TreeNode
from the given source tree
. The
whole tree is copied.T
- the current tree value typeB
- the mapped tree value typetree
- the source tree the new tree-node is created frommapper
- the tree value mapper functionTreeNode
from the given source tree
NullPointerException
- if one of the arguments is null
public static <T> TreeNode<T> ofTree(Tree<? extends T,?> tree)
TreeNode
from the given source tree
. The
whole tree is copied.T
- the current 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 TreeNode<String> parse(String tree)
Tree.toParenthesesString()
. The tree string might look like this:
mul(div(cos(1.0),cos(π)),sin(mul(1.0,z)))The parse method doesn't strip the whitespace between the parentheses and the commas. If you want to remove this formatting whitespaces, you should do the parsing with an addition mapper function.
final TreeNode<String> tree = TreeNode.parse(
"mul( div(cos( 1.0) , cos(π )), sin(mul(1.0, z) ) )",
String::trim
);
tree
- the parentheses tree stringNullPointerException
- if the given tree
string is
null
IllegalArgumentException
- if the given tree string could not be
parsedTree.toParenthesesString(Function)
,
Tree.toParenthesesString()
,
parse(String, Function)
public static <B> TreeNode<B> parse(String tree, Function<? super String,? extends B> mapper)
Tree.toParenthesesString()
. The tree string might look like this
0(1(4,5),2(6),3(7(10,11),8,9))and can be parsed to an integer tree with the following code:
final Tree<Integer, ?> tree = TreeNode.parse(
"0(1(4,5),2(6),3(7(10,11),8,9))",
Integer::parseInt
);
B
- the tree node value typetree
- the parentheses tree stringmapper
- the mapper which converts the serialized string value to
the desired typeNullPointerException
- if one of the arguments is null
IllegalArgumentException
- if the given parentheses tree string
doesn't represent a valid treeTree.toParenthesesString(Function)
,
Tree.toParenthesesString()
,
parse(String)
© 2007-2019 Franz Wilhelmstötter (2019-11-18 20:30)