java.lang.Object
io.jenetics.ext.util.FlatTreeNode<V>
- All Implemented Interfaces:
FlatTree<V,,FlatTreeNode<V>> Tree<V,,FlatTreeNode<V>> Self<FlatTreeNode<V>>,Serializable,Iterable<FlatTreeNode<V>>
public final class FlatTreeNode<V>
extends Object
implements FlatTree<V,FlatTreeNode<V>>, Serializable
Default implementation of the
FlatTree interface. Beside the
flattened and dense layout it is also an immutable implementation of
the Tree interface. It can only be created from an existing tree.
final Tree<String, ?> immutable = FlatTreeNode.ofTree(TreeNode.parse(...));
- Since:
- 3.9
- Version:
- 7.1
- See Also:
- Implementation Note:
- This class is immutable and thread-safe.
-
Nested Class Summary
-
Method Summary
Modifier and TypeMethodDescriptionReturn an iterator that traverses the subtree rooted atthisnode in breadth-first order.Return a stream that traverses the subtree rooted atthisnode in breadth-first order.childAt(int index) Return the child node with the given index.intReturn the number of children this tree node consists of.intReturn the index of the first child node in the underlying node array.booleanReturn the whole flattened tree values in breadth-first order.inthashCode()booleanTests 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> Create a new, immutableFlatTreeNodefrom the giventree.parent()Return the parent node of this tree node.static FlatTreeNode<String> Parses a (parentheses) tree string, created withTree.toParenthesesString().static <B> FlatTreeNode<B> Parses a (parentheses) tree string, created withTree.toParenthesesString().<U> Ureduce(U[] neutral, BiFunction<? super V, ? super U[], ? extends U> reducer) Performs a reduction on the elements ofthistree, using an associative reduction function.root()Returns the root of the tree that contains this node.intsize()Return the number of nodes ofthisnode (subtree).toString()value()Return the value of the currentTreenode.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
childAfter, childAtPath, childAtPath, childBefore, childIterator, childPath, childStream, depth, depthFirstIterator, depthFirstStream, firstChild, firstLeaf, indexOf, isAncestor, isChild, isDescendant, isEmpty, isLeaf, isRelated, isSibling, iterator, lastChild, lastLeaf, leafCount, leaves, level, nextLeaf, nextNode, nextSibling, path, pathElements, pathFromAncestorIterator, postorderIterator, postorderStream, preorderIterator, preorderStream, previousLeaf, previousNode, previousSibling, sharedAncestor, siblingCount, stream, toParenthesesString, toParenthesesString
-
Method Details
-
root
Returns the root of the tree that contains this node. The root is the ancestor with no parent. This implementation has a runtime complexity of O(1).- Specified by:
rootin interfaceTree<V,FlatTreeNode<V>> - Returns:
- the root of the tree that contains this node
-
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
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
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
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
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
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
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
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:
-
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:
-
map
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
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.
-
reduce
Description copied from interface:TreePerforms a reduction on the elements ofthistree, using an associative reduction function. This can be used for evaluating a given expression tree in pre-order.final Tree<String, ?> formula = TreeNode.parse("add(sub(6,div(230,10)),mul(5,6))"); final double result = formula.reduce(new Double[0], (op, args) -> switch (op) { case "add" -> args[0] + args[1]; case "sub" -> args[0] - args[1]; case "mul" -> args[0] * args[1]; case "div" -> args[0] / args[1]; default -> Double.parseDouble(op); } ); assert result == 13.0;- Specified by:
reducein interfaceTree<V,FlatTreeNode<V>> - Type Parameters:
U- the result type- Parameters:
neutral- the neutral element of the reduction. In most cases this will benew U[0].reducer- the reduce function- Returns:
- the result of the reduction, or
nullifthistree is empty (isEmpty() == true)
-
hashCode
-
equals
-
toString
-
size
Description copied from interface:TreeReturn the number of nodes ofthisnode (subtree).- Specified by:
sizein interfaceTree<V,FlatTreeNode<V>> - Returns:
- the number of nodes of
thisnode (subtree)
-
ofTree
Create a new, immutableFlatTreeNodefrom the giventree.- Type Parameters:
V- the tree value types- Parameters:
tree- the source tree- Returns:
- a
FlatTreeNodefrom the giventree - Throws:
NullPointerException- if the giventreeisnull
-
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)))
- 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:
-
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 = 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:
-