Class TreeNode<T>

    • Method Detail

      • 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<TchildAt​(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
      • insert

        public TreeNode<Tinsert​(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
      • remove

        public TreeNode<Tremove​(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 at 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 argument 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 at 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 argument is null
        Since:
        4.4
      • detach

        public TreeNode<Tdetach()
        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<Tattach​(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<Tattach​(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<Tattach​(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
      • 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
      • 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<Stringparse​(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 whitespace between the parentheses and the commas. If you want to remove this formatting whitespaces, 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:
        Tree.toParenthesesString(Function), Tree.toParenthesesString(), parse(String, Function)
      • 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:
        Tree.toParenthesesString(Function), Tree.toParenthesesString(), parse(String)