Class TreeUtils


  • public final class TreeUtils
    extends Object
    General utility methods for operating on tree, i.e. graphs consisting of TreeNodes.
    • Method Detail

      • getRoot

        public static <T extends TreeNode<T>> T getRoot​(T node)
        Returns the root of the tree the given node is part of.
        Parameters:
        node - the node to get the root of
        Returns:
        the root or null if the given node is null
      • addChild

        public static <T extends MutableTreeNode<T>> void addChild​(T parent,
                                                                   T child)
        Adds a new child node to a given MutableTreeNode parent.
        Parameters:
        parent - the parent node
        child - the child node to add
      • removeChild

        public static <T extends MutableTreeNode<T>> void removeChild​(T parent,
                                                                      T child)
        Removes the given child from the given parent node.
        Parameters:
        parent - the parent node
        child - the child node
      • toLeftAssociativity

        public static <N extends MutableBinaryTreeNode<N>> N toLeftAssociativity​(N node)
        Performs the following transformation on the given MutableBinaryTreeNode:
                o1                    o2
               / \                   / \
              A   o2     ====>     o1   C
                 / \              / \
                B   C            A   B
         
        Parameters:
        node - the node to transform
        Returns:
        the new root after the transformation, which is either the right sub node of the original root or the original root, if the right sub node is null