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 atthis
node in breadth-first order.Return a stream that traverses the subtree rooted atthis
node in breadth-first order.childAt
(int index) Return the child node with the given index.int
Return the number of children this tree node consists of.int
Return the index of the first child node in the underlying node array.boolean
Return the whole flattened tree values in breadth-first order.int
hashCode()
boolean
Tests whetherthis
node is the same as theother
node.boolean
isRoot()
Returnstrue
if 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, immutableFlatTreeNode
from 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> U
reduce
(U[] neutral, BiFunction<? super V, ? super U[], ? extends U> reducer) Performs a reduction on the elements ofthis
tree, using an associative reduction function.root()
Returns the root of the tree that contains this node.int
size()
Return the number of nodes ofthis
node (subtree).toString()
value()
Return the value of the currentTree
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
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:
root
in interfaceTree<V,
FlatTreeNode<V>> - Returns:
- the root of the tree that contains this node
-
isRoot
Description copied from interface:Tree
Returnstrue
if this node is the root of the tree.- Specified by:
isRoot
in interfaceTree<V,
FlatTreeNode<V>> - Returns:
true
if this node is the root of its tree,false
otherwise
-
value
Description copied from interface:Tree
Return the value of the currentTree
node. The value may benull
.- Specified by:
value
in interfaceTree<V,
FlatTreeNode<V>> - Returns:
- the value of the current
Tree
node
-
parent
Description copied from interface:Tree
Return the parent node of this tree node.- Specified by:
parent
in 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:Tree
Return the child node with the given index.- Specified by:
childAt
in interfaceTree<V,
FlatTreeNode<V>> - Parameters:
index
- the child index- Returns:
- the child node with the given index
-
childCount
Description copied from interface:Tree
Return the number of children this tree node consists of.- Specified by:
childCount
in 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.-1
is returned ifthis
node is a leaf.- Specified by:
childOffset
in interfaceFlatTree<V,
FlatTreeNode<V>> - Returns:
- Return the index of the first child node in the underlying node
array, or
-1
ifthis
node is a leaf
-
flattenedNodes
Description copied from interface:FlatTree
Return the whole flattened tree values in breadth-first order. This is equivalent tofinal ISeq<T> seq = getRoot().breadthFirstStream() .collect(ISeq.toISeq());
- Specified by:
flattenedNodes
in interfaceFlatTree<V,
FlatTreeNode<V>> - Returns:
- the flattened tree values in breadth-first order
-
breadthFirstIterator
Description copied from interface:Tree
Return an iterator that traverses the subtree rooted atthis
node in breadth-first order. The first node returned by the iterator isthis
node.Modifying the tree by inserting, removing, or moving a node invalidates any iterator created before the modification.
- Specified by:
breadthFirstIterator
in interfaceTree<V,
FlatTreeNode<V>> - Returns:
- an iterator for traversing the tree in breadth-first order
- See Also:
-
breadthFirstStream
Description copied from interface:Tree
Return a stream that traverses the subtree rooted atthis
node in breadth-first order. The first node returned by the stream isthis
node.- Specified by:
breadthFirstStream
in 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:Tree
Tests whetherthis
node is the same as theother
node. 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:
identical
in interfaceTree<V,
FlatTreeNode<V>> - Parameters:
other
- theother
node- Returns:
true
if theother
node is the same asthis
node.
-
reduce
Description copied from interface:Tree
Performs a reduction on the elements ofthis
tree, 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:
reduce
in 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
null
ifthis
tree is empty (isEmpty() == true
)
-
hashCode
-
equals
-
toString
-
size
Description copied from interface:Tree
Return the number of nodes ofthis
node (subtree).- Specified by:
size
in interfaceTree<V,
FlatTreeNode<V>> - Returns:
- the number of nodes of
this
node (subtree)
-
ofTree
Create a new, immutableFlatTreeNode
from the giventree
.- Type Parameters:
V
- the tree value types- Parameters:
tree
- the source tree- Returns:
- a
FlatTreeNode
from the giventree
- Throws:
NullPointerException
- if the giventree
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)))
- 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:
- 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 isnull
IllegalArgumentException
- if the given parentheses tree string doesn't represent a valid tree- Since:
- 5.0
- See Also:
-