Package io.jenetics.ext.util
Class TreeNode<T>
- 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>>,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. TheTreeNodeis a mutable implementation of theTreeinterface.- Since:
 - 3.9
 - Version:
 - 5.2
 - See Also:
 - Serialized Form
 
 
- 
- 
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description TreeNode<T>attach(TreeNode<T> child)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.TreeNode<T>attach(T child)Attaches the givenchildtothisnode.TreeNode<T>attach(T... children)Attaches the givenchildrentothisnode.TreeNode<T>childAt(int index)Returns the child at the specified index in this node's child array.intchildCount()Return the number of children this tree node consists of.Iterator<TreeNode<T>>childIterator()Return an iterator of the children of thisTreenode.Stream<TreeNode<T>>childStream()Return a forward-order stream of this node's children.TreeNode<T>copy()TreeNode<T>detach()Detaches the subtree rooted atthisnode from the tree, givingthisnode anullparent.booleanequals(Object obj)inthashCode()TreeNode<T>insert(int index, TreeNode<T> child)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>map(Function<? super T,? extends B> mapper)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>ofTree(Tree<? extends T,?> tree)Return a newTreeNodefrom the given sourcetree.static <T,B>
TreeNode<B>ofTree(Tree<? extends T,?> tree, Function<? super T,? extends B> mapper)Return a newTreeNodefrom the given sourcetree.Optional<TreeNode<T>>parent()Returns this node's parent if available.static TreeNode<String>parse(String tree)Parses a (parentheses) tree string, created withTree.toParenthesesString().static <B> TreeNode<B>parse(String tree, Function<? super String,? extends B> mapper)Parses a (parentheses) tree string, created withTree.toParenthesesString().TreeNode<T>remove(int index)Removes the child at the specified index from this node's children and sets that node's parent tonull.voidremove(Tree<?,?> child)Remove thechildfromthisnode's child array, giving it anullparent.voidremoveAllChildren()Removes all children fothisnode and setting their parents tonull.booleanremoveAtPath(Tree.Path path)Removes the child at the givenpath.TreeNode<T>replace(int index, TreeNode<T> child)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.StringtoString()Tvalue()Return the node valuevoidvalue(T value)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, isLeaf, isRelated, isRoot, isSibling, iterator, lastChild, lastLeaf, leafCount, level, nextLeaf, nextNode, nextSibling, path, pathElements, pathFromAncestorIterator, postorderIterator, postorderStream, preorderIterator, preorderStream, previousLeaf, previousNode, previousSibling, root, sharedAncestor, siblingCount, size, stream, toParenthesesString, toParenthesesString 
 - 
 
 - 
 
- 
- 
Method Detail
- 
value
public void value(T value)
Sets the user object for this node.- Parameters:
 value- the nodevalue
 
- 
childAt
public TreeNode<T> childAt(int index)
Returns the child at the specified index in this node's child array. 
- 
childCount
public int 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
public Iterator<TreeNode<T>> 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
public Stream<TreeNode<T>> 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
public TreeNode<T> insert(int index, TreeNode<T> child)
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
public TreeNode<T> replace(int index, TreeNode<T> child)
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
public TreeNode<T> remove(int index)
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
public boolean removeAtPath(Tree.Path path)
Removes the child at the givenpath. If no child exists at 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 argument isnull- Since:
 - 4.4
 
 
- 
replaceAtPath
public boolean replaceAtPath(Tree.Path path, TreeNode<T> child)
Replaces the child at the givenpathwith the given newchild. If no child exists at 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 argument isnull- Since:
 - 4.4
 
 
- 
detach
public TreeNode<T> detach()
Detaches the subtree rooted atthisnode from the tree, givingthisnode anullparent. Does nothing ifthisnode is the root of its tree.- Returns:
 this
 
- 
remove
public void remove(Tree<?,?> child)
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
public void removeAllChildren()
Removes all children fothisnode and setting their parents tonull. Ifthisnode has no children, this method does nothing. 
- 
attach
public TreeNode<T> attach(TreeNode<T> child)
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
@SafeVarargs public final TreeNode<T> attach(T... children)
Attaches the givenchildrentothisnode.- Parameters:
 children- the children to attach tothisnode- Returns:
 thistree-node, for method chaining- Throws:
 NullPointerException- if the givenchildrenarray isnull
 
- 
attach
public TreeNode<T> attach(T child)
Attaches the givenchildtothisnode.- Parameters:
 child- the child to attach tothisnode- Returns:
 thistree-node, for method chaining
 
- 
map
public <B> TreeNode<B> map(Function<? super T,? extends B> mapper)
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
 
- 
of
public static <T> TreeNode<T> of()
Return a newTreeNodewith anulltree value.- Type Parameters:
 T- the tree-node type- Returns:
 - a new tree-node
 
 
- 
of
public static <T> TreeNode<T> of(T value)
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
public static <T> TreeNode<T> ofTree(Tree<? extends T,?> tree)
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
public static TreeNode<String> parse(String tree)
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 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.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:
 Tree.toParenthesesString(Function),Tree.toParenthesesString(),parse(String, Function)
 
- 
parse
public static <B> TreeNode<B> parse(String tree, Function<? super String,? extends B> mapper)
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:
 Tree.toParenthesesString(Function),Tree.toParenthesesString(),parse(String)
 
 - 
 
 -