Package io.jenetics.ext.util
Class FlatTreeNode<V>
- java.lang.Object
 - 
- io.jenetics.ext.util.FlatTreeNode<V>
 
 
- 
- All Implemented Interfaces:
 FlatTree<V,FlatTreeNode<V>>,Tree<V,FlatTreeNode<V>>,Serializable,Iterable<FlatTreeNode<V>>
public final class FlatTreeNode<V> extends Object implements FlatTree<V,FlatTreeNode<V>>, Serializable
Default implementation of theFlatTreeinterface. Beside the flattened and dense layout it is also an immutable implementation of theTreeinterface. It can only be created from an existing tree.final Tree<String, ?> immutable = FlatTreeNode.ofTree(TreeNode.parse(...));- Since:
 - 3.9
 - Version:
 - 6.0
 - See Also:
 - Serialized Form
 - Implementation Note:
 - This class is immutable and thread-safe.
 
 
- 
- 
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Deprecated Methods Modifier and Type Method Description Iterator<FlatTreeNode<V>>breadthFirstIterator()Return an iterator that traverses the subtree rooted atthisnode in breadth-first order.Stream<FlatTreeNode<V>>breadthFirstStream()Return a stream that traverses the subtree rooted atthisnode in breadth-first order.FlatTreeNode<V>childAt(int index)Return the child node with the given index.intchildCount()Return the number of children this tree node consists of.intchildOffset()Return the index of the first child node in the underlying node array.booleanequals(Object obj)ISeq<FlatTreeNode<V>>flattenedNodes()Return the whole flattened tree values in breadth-first order.inthashCode()booleanidentical(Tree<?,?> other)Tests whetherthisnode is the same as theothernode.booleanisRoot()Returnstrueif this node is the root of the tree.<B> ISeq<B>map(Function<? super FlatTreeNode<V>,? extends B> mapper)Return a sequence of all mapped nodes of the whole underlying tree.static <V> FlatTreeNode<V>of(Tree<? extends V,?> tree)Deprecated, for removal: This API element is subject to removal in a future version.UseofTree(Tree)insteadstatic <V> FlatTreeNode<V>ofTree(Tree<? extends V,?> tree)Create a new, immutableFlatTreeNodefrom the giventree.Optional<FlatTreeNode<V>>parent()Return the parent node of this tree node.static FlatTreeNode<String>parse(String tree)Parses a (parentheses) tree string, created withTree.toParenthesesString().static <B> FlatTreeNode<B>parse(String tree, Function<? super String,? extends B> mapper)Parses a (parentheses) tree string, created withTree.toParenthesesString().FlatTreeNode<V>root()Returns the root of the tree that contains this node.intsize()Return the number of nodes ofthisnode (sub-tree).StringtoString()Vvalue()Return the value of the currentTreenode.- 
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
childAfter, childAtPath, childAtPath, childBefore, childIterator, childPath, childStream, depth, depthFirstIterator, depthFirstStream, firstChild, firstLeaf, indexOf, isAncestor, isChild, isDescendant, isLeaf, isRelated, isSibling, iterator, lastChild, lastLeaf, leafCount, level, nextLeaf, nextNode, nextSibling, path, pathElements, pathFromAncestorIterator, postorderIterator, postorderStream, preorderIterator, preorderStream, previousLeaf, previousNode, previousSibling, sharedAncestor, siblingCount, stream, toParenthesesString, toParenthesesString 
 - 
 
 - 
 
- 
- 
Method Detail
- 
root
public FlatTreeNode<V> root()
Returns the root of the tree that contains this node. The root is the ancestor with no parent. This implementation have a runtime complexity of O(1).- Specified by:
 rootin interfaceTree<V,FlatTreeNode<V>>- Returns:
 - the root of the tree that contains this node
 
 
- 
isRoot
public boolean isRoot()
Description copied from interface:TreeReturnstrueif this node is the root of the tree.- Specified by:
 isRootin interfaceTree<V,FlatTreeNode<V>>- Returns:
 trueif this node is the root of its tree,falseotherwise
 
- 
value
public V value()
Description copied from interface:TreeReturn the value of the currentTreenode. The value may benull.- Specified by:
 valuein interfaceTree<V,FlatTreeNode<V>>- Returns:
 - the value of the current 
Treenode 
 
- 
parent
public Optional<FlatTreeNode<V>> parent()
Description copied from interface:TreeReturn the parent node of this tree node.- Specified by:
 parentin interfaceTree<V,FlatTreeNode<V>>- Returns:
 - the parent node, or 
Optional.empty()if this node is the root of the tree 
 
- 
childAt
public FlatTreeNode<V> childAt(int index)
Description copied from interface:TreeReturn the child node with the given index.- Specified by:
 childAtin interfaceTree<V,FlatTreeNode<V>>- Parameters:
 index- the child index- Returns:
 - the child node with the given index
 
 
- 
childCount
public int childCount()
Description copied from interface:TreeReturn the number of children this tree node consists of.- Specified by:
 childCountin interfaceTree<V,FlatTreeNode<V>>- Returns:
 - the number of children this tree node consists of
 
 
- 
childOffset
public int childOffset()
Return the index of the first child node in the underlying node array.-1is returned ifthisnode is a leaf.- Specified by:
 childOffsetin interfaceFlatTree<V,FlatTreeNode<V>>- Returns:
 - Return the index of the first child node in the underlying node
         array, or 
-1ifthisnode is a leaf 
 
- 
flattenedNodes
public ISeq<FlatTreeNode<V>> flattenedNodes()
Description copied from interface:FlatTreeReturn the whole flattened tree values in breadth-first order. This is equivalent tofinal ISeq<T> seq = getRoot().breadthFirstStream() .collect(ISeq.toISeq());- Specified by:
 flattenedNodesin interfaceFlatTree<V,FlatTreeNode<V>>- Returns:
 - the flattened tree values in breadth-first order
 
 
- 
breadthFirstIterator
public Iterator<FlatTreeNode<V>> breadthFirstIterator()
Description copied from interface:TreeReturn an iterator that traverses the subtree rooted atthisnode in breadth-first order. The first node returned by the iterator isthisnode.Modifying the tree by inserting, removing, or moving a node invalidates any iterator created before the modification.
- Specified by:
 breadthFirstIteratorin interfaceTree<V,FlatTreeNode<V>>- Returns:
 - an iterator for traversing the tree in breadth-first order
 - See Also:
 Tree.depthFirstIterator()
 
- 
breadthFirstStream
public Stream<FlatTreeNode<V>> breadthFirstStream()
Description copied from interface:TreeReturn a stream that traverses the subtree rooted atthisnode in breadth-first order. The first node returned by the stream isthisnode.- Specified by:
 breadthFirstStreamin interfaceTree<V,FlatTreeNode<V>>- Returns:
 - a stream for traversing the tree in breadth-first order
 - See Also:
 Tree.depthFirstIterator(),Tree.stream()
 
- 
map
public <B> ISeq<B> map(Function<? super FlatTreeNode<V>,? extends B> mapper)
Return a sequence of all mapped nodes of the whole underlying tree. This is a convenient method forfinal ISeq<B> seq = stream() .map(mapper) .collect(ISeq.toISeq())- Type Parameters:
 B- the mapped type- Parameters:
 mapper- the mapper function- Returns:
 - a sequence of all mapped nodes
 
 
- 
identical
public boolean identical(Tree<?,?> other)
Description copied from interface:TreeTests whetherthisnode is the same as theothernode. The default implementation returns the object identity,this == other, of the two objects, but other implementations may use different criteria for checking the identity.- Specified by:
 identicalin interfaceTree<V,FlatTreeNode<V>>- Parameters:
 other- theothernode- Returns:
 trueif theothernode is the same asthisnode.
 
- 
size
public int size()
Description copied from interface:TreeReturn the number of nodes ofthisnode (sub-tree).- Specified by:
 sizein interfaceTree<V,FlatTreeNode<V>>- Returns:
 - the number of nodes of 
thisnode (sub-tree) 
 
- 
of
@Deprecated(since="6.1", forRemoval=true) public static <V> FlatTreeNode<V> of(Tree<? extends V,?> tree)
Deprecated, for removal: This API element is subject to removal in a future version.UseofTree(Tree)insteadCreate a new, immutableFlatTreeNodefrom the giventree.- Type Parameters:
 V- the tree value types- Parameters:
 tree- the source tree- Returns:
 - a new 
FlatTreeNodefrom the giventree - Throws:
 NullPointerException- if the giventreeisnull
 
- 
ofTree
public static <V> FlatTreeNode<V> ofTree(Tree<? extends V,?> tree)
Create a new, immutableFlatTreeNodefrom the giventree.- Type Parameters:
 V- the tree value types- Parameters:
 tree- the source tree- Returns:
 - a new 
FlatTreeNodefrom the giventree - Throws:
 NullPointerException- if the giventreeisnull
 
- 
parse
public static FlatTreeNode<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)))
- 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:
 - 5.0
 - See Also:
 Tree.toParenthesesString(Function),Tree.toParenthesesString(),TreeNode.parse(String)
 
- 
parse
public static <B> FlatTreeNode<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 = FlatTreeNode.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:
 - 5.0
 - See Also:
 Tree.toParenthesesString(Function),Tree.toParenthesesString(),TreeNode.parse(String, Function)
 
 - 
 
 -