Class TreeNode<T>

java.lang.Object
io.jenetics.ext.util.TreeNode<T>
Type Parameters:
T - the value type of the tree node
All Implemented Interfaces:
Tree<T,TreeNode<T>>, Copyable<TreeNode<T>>, Self<TreeNode<T>>, Serializable, Iterable<TreeNode<T>>

public final class TreeNode<T> extends Object implements Tree<T,TreeNode<T>>, Iterable<TreeNode<T>>, Copyable<TreeNode<T>>, Serializable
A general purpose node in a tree data-structure. The TreeNode is a mutable implementation of the Tree interface.
Since:
3.9
Version:
5.2
See Also:
  • Method Details

    • value

      public void value(T value)
      Sets the user object for this node.
      Parameters:
      value - the node value
    • value

      public T value()
      Return the node value
      Specified by:
      value in interface Tree<T,TreeNode<T>>
      Returns:
      the node value
    • parent

      public Optional<TreeNode<T>> parent()
      Returns this node's parent if available.
      Specified by:
      parent in interface Tree<T,TreeNode<T>>
      Returns:
      the tree-node, or an empty value if this node has no parent
    • childAt

      public TreeNode<T> childAt(int index)
      Returns the child at the specified index in this node's child array.
      Specified by:
      childAt in interface Tree<T,TreeNode<T>>
      Parameters:
      index - an index into this node's child array
      Returns:
      the tree-node in this node's child array at the specified index
      Throws:
      ArrayIndexOutOfBoundsException - if the index is out of bounds
    • 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<T,TreeNode<T>>
      Returns:
      the number of children this tree node consists of
    • childIterator

      Description copied from interface: Tree
      Return an iterator of the children of this Tree node.
      Specified by:
      childIterator in interface Tree<T,TreeNode<T>>
      Returns:
      an iterator of the children of this Tree node.
    • childStream

      Description copied from interface: Tree
      Return a forward-order stream of this node's children.
      Specified by:
      childStream in interface Tree<T,TreeNode<T>>
      Returns:
      a stream of children of this node
    • insert

      public TreeNode<T> insert(int index, TreeNode<T> child)
      Removes the child from its present parent (if it has one), sets the child's parent to this node, and then adds the child to this node's child array at index index. The new child must not be null and must not be an ancestor of this node.
      Parameters:
      index - the index in the child array where this node is to be inserted
      child - the sub-node to be inserted
      Returns:
      this tree-node, for method chaining
      Throws:
      ArrayIndexOutOfBoundsException - if index is out of bounds
      IllegalArgumentException - if child is an ancestor of this node
      NullPointerException - if the given child is null
    • replace

      public TreeNode<T> replace(int index, TreeNode<T> child)
      Replaces the child at the give index with the given child
      Parameters:
      index - the index of the child which will be replaced
      child - the new child
      Returns:
      this tree-node, for method chaining
      Throws:
      ArrayIndexOutOfBoundsException - if the index is out of bounds
      IllegalArgumentException - if child is an ancestor of this node
      NullPointerException - if the given child is null
    • remove

      public TreeNode<T> remove(int index)
      Removes the child at the specified index from this node's children and sets that node's parent to null.
      Parameters:
      index - the index in this node's child array of the child to remove
      Returns:
      this tree-node, for method chaining
      Throws:
      ArrayIndexOutOfBoundsException - if the index is out of bounds
    • removeAtPath

      public boolean removeAtPath(Tree.Path path)
      Removes the child at the given path. If no child exists on the given path, nothing is removed.
      Parameters:
      path - the path of the child to replace
      Returns:
      true if a child at the given path existed and has been removed
      Throws:
      NullPointerException - if one of the given arguments is null
      Since:
      4.4
    • replaceAtPath

      public boolean replaceAtPath(Tree.Path path, TreeNode<T> child)
      Replaces the child at the given path with the given new child. If no child exists on the given path, nothing is replaced.
      Parameters:
      path - the path of the child to replace
      child - the new child
      Returns:
      true if a child at the given path existed and has been replaced
      Throws:
      NullPointerException - if one of the given arguments is null
      Since:
      4.4
    • detach

      public TreeNode<T> detach()
      Detaches the subtree rooted at this node from the tree, giving this node a null parent. Does nothing if this node is the root of its tree.
      Returns:
      this
    • remove

      public void remove(Tree<?,?> child)
      Remove the child from this node's child array, giving it a null parent.
      Parameters:
      child - the child of this node to remove
      Throws:
      NullPointerException - if the given child is null
      IllegalArgumentException - if the given child is not a child of this node
    • removeAllChildren

      public void removeAllChildren()
      Removes all children fo this node and setting their parents to null. If this node has no children, this method does nothing.
    • attach

      public TreeNode<T> attach(TreeNode<T> child)
      Remove the given child from its parent and makes it a child of this node by adding it to the end of this node's child array.
      Parameters:
      child - the new child added to this node
      Returns:
      this tree-node, for method chaining
      Throws:
      NullPointerException - if the given child is null
    • attach

      @SafeVarargs public final TreeNode<T> attach(T... children)
      Attaches the given children to this node.
      Parameters:
      children - the children to attach to this node
      Returns:
      this tree-node, for method chaining
      Throws:
      NullPointerException - if the given children array is null
    • attach

      public TreeNode<T> attach(T child)
      Attaches the given child to this node.
      Parameters:
      child - the child to attach to this node
      Returns:
      this tree-node, for method chaining
    • copy

      public TreeNode<T> copy()
      Specified by:
      copy in interface Copyable<T>
    • map

      public <B> TreeNode<B> map(Function<? super T,? extends B> mapper)
      Returns a new TreeNode consisting of all nodes of this tree, but with a different value type, created by applying the given function to the node values of this tree.
      Type Parameters:
      B - the new node type
      Parameters:
      mapper - the node value mapper
      Returns:
      a new tree consisting of all nodes of this tree
      Throws:
      NullPointerException - if the given mapper function is null
    • 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
    • of

      public static <T> TreeNode<T> of()
      Return a new TreeNode with a null tree value.
      Type Parameters:
      T - the tree-node type
      Returns:
      a new tree-node
    • of

      public static <T> TreeNode<T> of(T value)
      Return a new TreeNode with the given node value.
      Type Parameters:
      T - the tree-node type
      Parameters:
      value - the node value
      Returns:
      a new tree-node
    • ofTree

      public static <T, B> TreeNode<B> ofTree(Tree<? extends T,?> tree, Function<? super T,? extends B> mapper)
      Return a new TreeNode from the given source tree. The whole tree is copied.
      Type Parameters:
      T - the current tree value type
      B - the mapped tree value type
      Parameters:
      tree - the source tree the new tree-node is created from
      mapper - the tree value mapper function
      Returns:
      a new TreeNode from the given source tree
      Throws:
      NullPointerException - if one of the arguments is null
    • ofTree

      public static <T> TreeNode<T> ofTree(Tree<? extends T,?> tree)
      Return a new TreeNode from the given source tree. The whole tree is copied.
      Type Parameters:
      T - the current tree value type
      Parameters:
      tree - the source tree the new tree-node is created from
      Returns:
      a new TreeNode from the given source tree
      Throws:
      NullPointerException - if the source tree is null
    • parse

      public static TreeNode<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)))
       
      The parse method doesn't strip the space between the parentheses and the commas. If you want to remove this formatting space, you should do the parsing with an addition mapper function.
      final TreeNode<String> tree = TreeNode.parse(
          "mul(  div(cos( 1.0) , cos(π )), sin(mul(1.0, z) ) )",
          String::trim
      );
      
      The code above will trim all tree nodes during the parsing process.
      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:
      4.3
      See Also:
    • parse

      public static <B> TreeNode<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 = TreeNode.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:
      4.3
      See Also: