Class ImmutableTreeNode<T>

java.lang.Object
io.openml.gearbox.binarytree.ImmutableTreeNode<T>
Type Parameters:
T - The type of value stored in this tree node
All Implemented Interfaces:
TreeNode<T>, Visitable<T>

public final class ImmutableTreeNode<T> extends Object implements TreeNode<T>
ImmutableTreeNode encapsulates the value, left tree reference, and right tree reference in an immutable state; it also represents a concrete 'Element', i.e. Visitable.

This is an immutable implementation of a TreeNode. Its value and children are set at creation and cannot be changed.

  • Method Details

    • emptyNode

      public static <T> TreeNode<T> emptyNode()
      Creates a dummy node with null value, null left subtree, and null right subtree.

      This would be a useful method for implementing algorithm that makes use of some auxiliary TreeNode.

      Type Parameters:
      T - Not used
      Returns:
      a new TreeNode instance that is assignable to any instantiated generic TreeNode
    • value

      public static <T> TreeNode<T> value(T value)
      Creates a new TreeNode with the specified node value.

      The left and right subtrees of this node are default to null.

      Type Parameters:
      T - The type of value store in the node
      Parameters:
      value - The provided node value, can be null
      Returns:
      a new TreeNode instance
      Throws:
      NullPointerException - if the value is null
    • valueWithLeft

      public static <T> TreeNode<T> valueWithLeft(T value, TreeNode<T> left)
      Creates a new TreeNode with the specified node value and a left subtree.

      The right subtree of this node is default to null

      Type Parameters:
      T - The type of value store in the node
      Parameters:
      value - The provided node value
      left - The provided left subtree
      Returns:
      a new TreeNode instance
      Throws:
      NullPointerException - if the value or left is null
    • valueWithRight

      public static <T> TreeNode<T> valueWithRight(T value, TreeNode<T> right)
      Creates a new TreeNode with the specified node value and a right subtree.

      The left subtree of this node is default to null

      Type Parameters:
      T - The type of value store in the node
      Parameters:
      value - The provided node value
      right - The provided right subtree
      Returns:
      a new TreeNode instance
      Throws:
      NullPointerException - if the value or right is null
    • valueWithLeftAndRight

      public static <T> TreeNode<T> valueWithLeftAndRight(T value, TreeNode<T> left, TreeNode<T> right)
      Private all-args constructor that is to be shared across all static factor methods.
      Type Parameters:
      T - The type of value store in the node
      Parameters:
      value - The value stored in this TreeNode
      left - A reference to the root of left subtree
      right - A reference to the root of the right subtree
      Returns:
      a new TreeNode instance
    • accept

      public void accept(Visitor<T> visitor)
      Description copied from interface: Visitable
      The entry point for the visitor to perform its operations on this object.
      Specified by:
      accept in interface Visitable<T>
      Parameters:
      visitor - The visitor that will process this visitable object.
    • withLeft

      public TreeNode<T> withLeft(TreeNode<T> newLeft)
      Returns a same instance with a specified new left child.
      Specified by:
      withLeft in interface TreeNode<T>
      Parameters:
      newLeft - The provided new left child
      Returns:
      a new TreeNode instance
    • withRight

      public TreeNode<T> withRight(TreeNode<T> newRight)
      Returns a same instance with a specified new right child.
      Specified by:
      withRight in interface TreeNode<T>
      Parameters:
      newRight - The provided new right child
      Returns:
      a new TreeNode instance
    • getValue

      public T getValue()
      Description copied from interface: TreeNode
      Returns the object stored in this tree node.
      Specified by:
      getValue in interface TreeNode<T>
      Returns:
      an object stored in this tree node
    • getLeft

      public TreeNode<T> getLeft()
      Description copied from interface: TreeNode
      Returns the reference to the left child of this tree node.
      Specified by:
      getLeft in interface TreeNode<T>
      Returns:
      the left child of this tree node, or null if there is no left child
    • getRight

      public TreeNode<T> getRight()
      Description copied from interface: TreeNode
      Returns the reference to the right child of this tree node.
      Specified by:
      getRight in interface TreeNode<T>
      Returns:
      the right child of this tree node, or null if there is no right child
    • equals

      public boolean equals(Object other)
      Overrides:
      equals in class Object
    • hashCode

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

      public String toString()
      Returns a string representation of this TreeNode.

      The string is the same thing as getValue() for simplicity.

      Overrides:
      toString in class Object
      Returns:
      node value's Object.toString()