Class FlatTreeNode<V>

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.
  • Method Details

    • 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 has a runtime complexity of O(1).
      Specified by:
      root in interface Tree<V,FlatTreeNode<V>>
      Returns:
      the root of the tree that contains this node
    • isRoot

      public boolean isRoot()
      Description copied from interface: Tree
      Returns true if this node is the root of the tree.
      Specified by:
      isRoot in interface Tree<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 current Tree node. The value may be null.
      Specified by:
      value in interface Tree<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 interface Tree<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 interface Tree<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 interface Tree<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 if this node is a leaf.
      Specified by:
      childOffset in interface FlatTree<V,FlatTreeNode<V>>
      Returns:
      Return the index of the first child node in the underlying node array, or -1 if this node is a leaf
    • flattenedNodes

      Description copied from interface: FlatTree
      Return the whole flattened tree values in breadth-first order. This is equivalent to
      final ISeq<T> seq = getRoot().breadthFirstStream() .collect(ISeq.toISeq());
      Specified by:
      flattenedNodes in interface FlatTree<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 at this node in breadth-first order. The first node returned by the iterator is this node.

      Modifying the tree by inserting, removing, or moving a node invalidates any iterator created before the modification.

      Specified by:
      breadthFirstIterator in interface Tree<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 at this node in breadth-first order. The first node returned by the stream is this node.
      Specified by:
      breadthFirstStream in interface Tree<V,FlatTreeNode<V>>
      Returns:
      a stream for traversing the tree in breadth-first order
      See Also:
    • 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 for
      final 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 whether this node is the same as the other 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 interface Tree<V,FlatTreeNode<V>>
      Parameters:
      other - the other node
      Returns:
      true if the other node is the same as this node.
    • reduce

      public <U> U reduce(U[] neutral, BiFunction<? super V,? super U[],? extends U> reducer)
      Description copied from interface: Tree
      Performs a reduction on the elements of this 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 interface Tree<V,FlatTreeNode<V>>
      Type Parameters:
      U - the result type
      Parameters:
      neutral - the neutral element of the reduction. In most cases this will be new U[0].
      reducer - the reduce function
      Returns:
      the result of the reduction, or null if this tree is empty (isEmpty() == true)
    • hashCode

      public int hashCode()
      Overrides:
      hashCode in class Object
    • equals

      public boolean equals(Object obj)
      Overrides:
      equals in class Object
    • toString

      public String toString()
      Overrides:
      toString in class Object
    • size

      public int size()
      Description copied from interface: Tree
      Return the number of nodes of this node (subtree).
      Specified by:
      size in interface Tree<V,FlatTreeNode<V>>
      Returns:
      the number of nodes of this node (subtree)
    • ofTree

      public static <V> FlatTreeNode<V> ofTree(Tree<? extends V,?> tree)
      Create a new, immutable FlatTreeNode from the given tree.
      Type Parameters:
      V - the tree value types
      Parameters:
      tree - the source tree
      Returns:
      a FlatTreeNode from the given tree
      Throws:
      NullPointerException - if the given tree is null
    • parse

      public static FlatTreeNode<String> parse(String tree)
      Parses a (parentheses) tree string, created with Tree.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 given tree string is null
      IllegalArgumentException - if the given tree string could not be parsed
      Since:
      5.0
      See Also:
    • parse

      public static <B> FlatTreeNode<B> parse(String tree, Function<? super String,? extends B> mapper)
      Parses a (parentheses) tree string, created with Tree.toParenthesesString(). The tree string might look like this
        0(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 string
      mapper - the mapper which converts the serialized string value to the desired type
      Returns:
      the parsed tree object
      Throws:
      NullPointerException - if one of the arguments is null
      IllegalArgumentException - if the given parentheses tree string doesn't represent a valid tree
      Since:
      5.0
      See Also: