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 theFlatTree
interface. Beside the flattened and dense layout it is also an immutable implementation of theTree
interface. 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 atthis
node in breadth-first order.Stream<FlatTreeNode<V>>
breadthFirstStream()
Return a stream that traverses the subtree rooted atthis
node in breadth-first order.FlatTreeNode<V>
childAt(int index)
Return the child node with the given index.int
childCount()
Return the number of children this tree node consists of.int
childOffset()
Return the index of the first child node in the underlying node array.boolean
equals(Object obj)
ISeq<FlatTreeNode<V>>
flattenedNodes()
Return the whole flattened tree values in breadth-first order.int
hashCode()
boolean
identical(Tree<?,?> other)
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>
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, immutableFlatTreeNode
from 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.int
size()
Return the number of nodes ofthis
node (sub-tree).String
toString()
V
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, 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:
root
in interfaceTree<V,FlatTreeNode<V>>
- Returns:
- the root of the tree that contains this node
-
isRoot
public boolean 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
public V 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
public Optional<FlatTreeNode<V>> 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
public FlatTreeNode<V> childAt(int index)
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
public int 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
public int 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
public ISeq<FlatTreeNode<V>> 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
public Iterator<FlatTreeNode<V>> 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:
Tree.depthFirstIterator()
-
breadthFirstStream
public Stream<FlatTreeNode<V>> 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:
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: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.
-
size
public int size()
Description copied from interface:Tree
Return the number of nodes ofthis
node (sub-tree).- Specified by:
size
in interfaceTree<V,FlatTreeNode<V>>
- Returns:
- the number of nodes of
this
node (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, immutableFlatTreeNode
from the giventree
.- Type Parameters:
V
- the tree value types- Parameters:
tree
- the source tree- Returns:
- a new
FlatTreeNode
from the giventree
- Throws:
NullPointerException
- if the giventree
isnull
-
ofTree
public static <V> FlatTreeNode<V> ofTree(Tree<? extends V,?> tree)
Create a new, immutableFlatTreeNode
from the giventree
.- Type Parameters:
V
- the tree value types- Parameters:
tree
- the source tree- Returns:
- a new
FlatTreeNode
from the giventree
- Throws:
NullPointerException
- if the giventree
isnull
-
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 giventree
string isnull
IllegalArgumentException
- 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 isnull
IllegalArgumentException
- 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)
-
-