java.lang.Object
io.jenetics.ext.util.TreeNode<T>
- Type Parameters:
T
- the value type of the tree node
- All Implemented Interfaces:
Tree<T,
,TreeNode<T>> Copyable<TreeNode<T>>
,Self<TreeNode<T>>
,Serializable
,Iterable<TreeNode<T>>
public final class TreeNode<T>
extends Object
implements Tree<T,TreeNode<T>>, Iterable<TreeNode<T>>, Copyable<TreeNode<T>>, Serializable
A general purpose node in a tree data-structure. The
TreeNode
is a
mutable implementation of the Tree
interface.- Since:
- 3.9
- Version:
- 5.2
- See Also:
-
Nested Class Summary
-
Method Summary
Modifier and TypeMethodDescriptionRemove the givenchild
from its parent and makes it a child of this node by adding it to the end of this node's child array.Attaches the givenchild
tothis
node.Attaches the givenchildren
tothis
node.childAt
(int index) Returns the child at the specified index in this node's child array.int
Return the number of children this tree node consists of.Return an iterator of the children of thisTree
node.Return a forward-order stream of this node's children.copy()
detach()
Detaches the subtree rooted atthis
node from the tree, givingthis
node anull
parent.boolean
int
hashCode()
Removes thechild
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 indexindex
.<B> TreeNode
<B> Returns a newTreeNode
consisting of all nodes ofthis
tree, but with a different value type, created by applying the given function to the node values ofthis
tree.static <T> TreeNode
<T> of()
Return a newTreeNode
with anull
tree value.static <T> TreeNode
<T> of
(T value) Return a newTreeNode
with the given nodevalue
.static <T> TreeNode
<T> Return a newTreeNode
from the given sourcetree
.static <T,
B> TreeNode <B> Return a newTreeNode
from the given sourcetree
.parent()
Returns this node's parent if available.Parses a (parentheses) tree string, created withTree.toParenthesesString()
.static <B> TreeNode
<B> Parses a (parentheses) tree string, created withTree.toParenthesesString()
.remove
(int index) Removes the child at the specified index from this node's children and sets that node's parent tonull
.void
Remove thechild
fromthis
node's child array, giving it anull
parent.void
Removes all children fothis
node and setting their parents tonull
.boolean
removeAtPath
(Tree.Path path) Removes the child at the givenpath
.Replaces the child at the give index with the givenchild
boolean
replaceAtPath
(Tree.Path path, TreeNode<T> child) Replaces the child at the givenpath
with the given newchild
.toString()
value()
Return the node valuevoid
Sets the user object for this node.Methods inherited from class java.lang.Object
clone, finalize, getClass, notify, notifyAll, wait, wait, wait
Methods inherited from interface java.lang.Iterable
forEach, spliterator
Methods inherited from interface io.jenetics.ext.util.Tree
breadthFirstIterator, breadthFirstStream, childAfter, childAtPath, childAtPath, childBefore, childPath, depth, depthFirstIterator, depthFirstStream, firstChild, firstLeaf, identical, indexOf, isAncestor, isChild, isDescendant, isEmpty, isLeaf, isRelated, isRoot, isSibling, iterator, lastChild, lastLeaf, leafCount, leaves, level, nextLeaf, nextNode, nextSibling, path, pathElements, pathFromAncestorIterator, postorderIterator, postorderStream, preorderIterator, preorderStream, previousLeaf, previousNode, previousSibling, reduce, root, sharedAncestor, siblingCount, size, stream, toParenthesesString, toParenthesesString
-
Method Details
-
value
Sets the user object for this node.- Parameters:
value
- the nodevalue
-
value
Return the node value -
parent
Returns this node's parent if available. -
childAt
Returns the child at the specified index in this node's child array. -
childCount
Description copied from interface:Tree
Return the number of children this tree node consists of.- Specified by:
childCount
in interfaceTree<T,
TreeNode<T>> - Returns:
- the number of children this tree node consists of
-
childIterator
Description copied from interface:Tree
Return an iterator of the children of thisTree
node.- Specified by:
childIterator
in interfaceTree<T,
TreeNode<T>> - Returns:
- an iterator of the children of this
Tree
node.
-
childStream
Description copied from interface:Tree
Return a forward-order stream of this node's children.- Specified by:
childStream
in interfaceTree<T,
TreeNode<T>> - Returns:
- a stream of children of
this
node
-
insert
Removes thechild
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 indexindex
. The newchild
must not benull
and must not be an ancestor ofthis
node.- Parameters:
index
- the index in the child array where this node is to be insertedchild
- the sub-node to be inserted- Returns:
this
tree-node, for method chaining- Throws:
ArrayIndexOutOfBoundsException
- ifindex
is out of boundsIllegalArgumentException
- ifchild
is an ancestor ofthis
nodeNullPointerException
- if the givenchild
isnull
-
replace
Replaces the child at the give index with the givenchild
- Parameters:
index
- the index of the child which will be replacedchild
- the new child- Returns:
this
tree-node, for method chaining- Throws:
ArrayIndexOutOfBoundsException
- if theindex
is out of boundsIllegalArgumentException
- ifchild
is an ancestor ofthis
nodeNullPointerException
- if the givenchild
isnull
-
remove
Removes the child at the specified index from this node's children and sets that node's parent tonull
.- Parameters:
index
- the index in this node's child array of the child to remove- Returns:
this
tree-node, for method chaining- Throws:
ArrayIndexOutOfBoundsException
- if theindex
is out of bounds
-
removeAtPath
Removes the child at the givenpath
. If no child exists on the given path, nothing is removed.- Parameters:
path
- the path of the child to replace- Returns:
true
if a child at the givenpath
existed and has been removed- Throws:
NullPointerException
- if one of the given arguments isnull
- Since:
- 4.4
-
replaceAtPath
Replaces the child at the givenpath
with the given newchild
. If no child exists on the given path, nothing is replaced.- Parameters:
path
- the path of the child to replacechild
- the new child- Returns:
true
if a child at the givenpath
existed and has been replaced- Throws:
NullPointerException
- if one of the given arguments isnull
- Since:
- 4.4
-
detach
Detaches the subtree rooted atthis
node from the tree, givingthis
node anull
parent. Does nothing ifthis
node is the root of its tree.- Returns:
this
-
remove
Remove thechild
fromthis
node's child array, giving it anull
parent.- Parameters:
child
- the child of this node to remove- Throws:
NullPointerException
- if the givenchild
isnull
IllegalArgumentException
- if the givenchild
is not a child of this node
-
removeAllChildren
Removes all children fothis
node and setting their parents tonull
. Ifthis
node has no children, this method does nothing. -
attach
Remove the givenchild
from its parent and makes it a child of this node by adding it to the end of this node's child array.- Parameters:
child
- the new child added to this node- Returns:
this
tree-node, for method chaining- Throws:
NullPointerException
- if the givenchild
isnull
-
attach
Attaches the givenchildren
tothis
node.- Parameters:
children
- the children to attach tothis
node- Returns:
this
tree-node, for method chaining- Throws:
NullPointerException
- if the givenchildren
array isnull
-
attach
Attaches the givenchild
tothis
node.- Parameters:
child
- the child to attach tothis
node- Returns:
this
tree-node, for method chaining
-
copy
-
map
Returns a newTreeNode
consisting of all nodes ofthis
tree, but with a different value type, created by applying the given function to the node values ofthis
tree.- Type Parameters:
B
- the new node type- Parameters:
mapper
- the node value mapper- Returns:
- a new tree consisting of all nodes of
this
tree - Throws:
NullPointerException
- if the givenmapper
function isnull
-
hashCode
-
equals
-
toString
-
of
Return a newTreeNode
with anull
tree value.- Type Parameters:
T
- the tree-node type- Returns:
- a new tree-node
-
of
Return a newTreeNode
with the given nodevalue
.- Type Parameters:
T
- the tree-node type- Parameters:
value
- the node value- Returns:
- a new tree-node
-
ofTree
public static <T,B> TreeNode<B> ofTree(Tree<? extends T, ?> tree, Function<? super T, ? extends B> mapper) Return a newTreeNode
from the given sourcetree
. The whole tree is copied.- Type Parameters:
T
- the current tree value typeB
- the mapped tree value type- Parameters:
tree
- the source tree the new tree-node is created frommapper
- the tree value mapper function- Returns:
- a new
TreeNode
from the given sourcetree
- Throws:
NullPointerException
- if one of the arguments isnull
-
ofTree
Return a newTreeNode
from the given sourcetree
. The whole tree is copied.- Type Parameters:
T
- the current tree value type- Parameters:
tree
- the source tree the new tree-node is created from- Returns:
- a new
TreeNode
from the given sourcetree
- Throws:
NullPointerException
- if the sourcetree
isnull
-
parse
Parses a (parentheses) tree string, created withTree.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 space between the parentheses and the commas. If you want to remove this formatting space, 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 );
- Parameters:
tree
- the parentheses tree string- Returns:
- the parsed tree
- Throws:
NullPointerException
- if the giventree
string isnull
IllegalArgumentException
- if the given tree string could not be parsed- Since:
- 4.3
- See Also:
-
parse
Parses a (parentheses) tree string, created withTree.toParenthesesString()
. The tree string might look like this0(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 );
- Type Parameters:
B
- the tree node value type- Parameters:
tree
- the parentheses tree stringmapper
- the mapper which converts the serialized string value to the desired type- Returns:
- the parsed tree object
- Throws:
NullPointerException
- if one of the arguments isnull
IllegalArgumentException
- if the given parentheses tree string doesn't represent a valid tree- Since:
- 4.3
- See Also:
-