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 givenchildfrom its parent and makes it a child of this node by adding it to the end of this node's child array.Attaches the givenchildtothisnode.Attaches the givenchildrentothisnode.childAt(int index) Returns the child at the specified index in this node's child array.intReturn the number of children this tree node consists of.Return an iterator of the children of thisTreenode.Return a forward-order stream of this node's children.copy()Return a new copy of typeT.detach()Detaches the subtree rooted atthisnode from the tree, givingthisnode anullparent.booleaninthashCode()Removes thechildfrom 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 newTreeNodeconsisting of all nodes ofthistree, but with a different value type, created by applying the given function to the node values ofthistree.static <T> TreeNode<T> of()Return a newTreeNodewith anulltree value.static <T> TreeNode<T> of(T value) Return a newTreeNodewith the given nodevalue.static <T> TreeNode<T> Return a newTreeNodefrom the given sourcetree.static <T,B> TreeNode <B> Return a newTreeNodefrom 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.voidRemove thechildfromthisnode's child array, giving it anullparent.voidRemoves all children fothisnode and setting their parents tonull.booleanremoveAtPath(Tree.Path path) Removes the child at the givenpath.Replaces the child at the give index with the givenchildbooleanreplaceAtPath(Tree.Path path, TreeNode<T> child) Replaces the child at the givenpathwith the given newchild.toString()value()Return the node valuevoidSets the user object for this node.Methods inherited from class java.lang.Object
clone, finalize, getClass, notify, notifyAll, wait, wait, waitMethods inherited from interface java.lang.Iterable
forEach, spliteratorMethods 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:TreeReturn the number of children this tree node consists of.- Specified by:
childCountin interfaceTree<T,TreeNode<T>> - Returns:
- the number of children this tree node consists of
-
childIterator
Description copied from interface:TreeReturn an iterator of the children of thisTreenode.- Specified by:
childIteratorin interfaceTree<T,TreeNode<T>> - Returns:
- an iterator of the children of this
Treenode.
-
childStream
Description copied from interface:TreeReturn a forward-order stream of this node's children.- Specified by:
childStreamin interfaceTree<T,TreeNode<T>> - Returns:
- a stream of children of
thisnode
-
insert
Removes thechildfrom 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 newchildmust not benulland must not be an ancestor ofthisnode.- Parameters:
index- the index in the child array where this node is to be insertedchild- the sub-node to be inserted- Returns:
thistree-node, for method chaining- Throws:
ArrayIndexOutOfBoundsException- ifindexis out of boundsIllegalArgumentException- ifchildis an ancestor ofthisnodeNullPointerException- if the givenchildisnull
-
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:
thistree-node, for method chaining- Throws:
ArrayIndexOutOfBoundsException- if theindexis out of boundsIllegalArgumentException- ifchildis an ancestor ofthisnodeNullPointerException- if the givenchildisnull
-
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:
thistree-node, for method chaining- Throws:
ArrayIndexOutOfBoundsException- if theindexis 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:
trueif a child at the givenpathexisted and has been removed- Throws:
NullPointerException- if one of the given arguments isnull- Since:
- 4.4
-
replaceAtPath
Replaces the child at the givenpathwith 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:
trueif a child at the givenpathexisted and has been replaced- Throws:
NullPointerException- if one of the given arguments isnull- Since:
- 4.4
-
detach
Detaches the subtree rooted atthisnode from the tree, givingthisnode anullparent. Does nothing ifthisnode is the root of its tree.- Returns:
this
-
remove
Remove thechildfromthisnode's child array, giving it anullparent.- Parameters:
child- the child of this node to remove- Throws:
NullPointerException- if the givenchildisnullIllegalArgumentException- if the givenchildis not a child of this node
-
removeAllChildren
Removes all children fothisnode and setting their parents tonull. Ifthisnode has no children, this method does nothing. -
attach
Remove the givenchildfrom 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:
thistree-node, for method chaining- Throws:
NullPointerException- if the givenchildisnull
-
attach
Attaches the givenchildrentothisnode.- Parameters:
children- the children to attach tothisnode- Returns:
thistree-node, for method chaining- Throws:
NullPointerException- if the givenchildrenarray isnull
-
attach
Attaches the givenchildtothisnode.- Parameters:
child- the child to attach tothisnode- Returns:
thistree-node, for method chaining
-
copy
Description copied from interface:CopyableReturn a new copy of typeT. -
map
Returns a newTreeNodeconsisting of all nodes ofthistree, but with a different value type, created by applying the given function to the node values ofthistree.- Type Parameters:
B- the new node type- Parameters:
mapper- the node value mapper- Returns:
- a new tree consisting of all nodes of
thistree - Throws:
NullPointerException- if the givenmapperfunction isnull
-
hashCode
-
equals
-
toString
-
of
Return a newTreeNodewith anulltree value.- Type Parameters:
T- the tree-node type- Returns:
- a new tree-node
-
of
Return a newTreeNodewith 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 newTreeNodefrom 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
TreeNodefrom the given sourcetree - Throws:
NullPointerException- if one of the arguments isnull
-
ofTree
Return a newTreeNodefrom 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
TreeNodefrom the given sourcetree - Throws:
NullPointerException- if the sourcetreeisnull
-
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.The code above will trim all tree nodes during the parsing process.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 giventreestring isnullIllegalArgumentException- 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 isnullIllegalArgumentException- if the given parentheses tree string doesn't represent a valid tree- Since:
- 4.3
- See Also:
-