Package gaiasky.util

Class BinarySearchTree<T extends Comparable<T>>

java.lang.Object
gaiasky.util.BinarySearchTree<T>

public class BinarySearchTree<T extends Comparable<T>> extends Object
Implements an unbalanced binary search tree. Note that all "matching" is based on the compareTo method.
  • Field Summary

    Fields
    Modifier and Type
    Field
    Description
    protected gaiasky.util.BinaryNode<T>
    The tree root.
  • Constructor Summary

    Constructors
    Constructor
    Description
    Construct the tree.
  • Method Summary

    Modifier and Type
    Method
    Description
    Find an item in the tree.
    Find the start of the interval where x is in.
    Find the largest item in the tree.
    Find the smallest item in the tree.
    protected gaiasky.util.BinaryNode<T>
    findMin(gaiasky.util.BinaryNode<T> t)
    Internal method to find the smallest item in a subtree.
    void
    insert(T x)
    Insert into the tree.
    protected gaiasky.util.BinaryNode<T>
    insert(T x, gaiasky.util.BinaryNode<T> t)
    Internal method to insert into a subtree.
    boolean
    Test if the tree is logically empty.
    static void
    main(String[] args)
     
    protected gaiasky.util.BinaryNode<T>
    remove(Comparable<T> x, gaiasky.util.BinaryNode<T> t)
    Internal method to remove from a subtree.
    void
    remove(T x)
    Remove from the tree.
    protected gaiasky.util.BinaryNode<T>
    removeMin(gaiasky.util.BinaryNode<T> t)
    Internal method to remove minimum item from a subtree.

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Field Details

    • root

      protected gaiasky.util.BinaryNode<T extends Comparable<T>> root
      The tree root.
  • Constructor Details

    • BinarySearchTree

      public BinarySearchTree()
      Construct the tree.
  • Method Details

    • main

      public static void main(String[] args)
    • insert

      public void insert(T x)
      Insert into the tree.
      Parameters:
      x - the item to insert.
      Throws:
      RuntimeException - if x is already present.
    • remove

      public void remove(T x)
      Remove from the tree.
      Parameters:
      x - the item to remove.
      Throws:
      RuntimeException - if x is not found.
    • findMin

      public Comparable<T> findMin()
      Find the smallest item in the tree.
      Returns:
      smallest item or null if empty.
    • findMax

      public Comparable<T> findMax()
      Find the largest item in the tree.
      Returns:
      the largest item or null if empty.
    • find

      public Comparable<T> find(Comparable<T> x)
      Find an item in the tree.
      Parameters:
      x - the item to search for.
      Returns:
      the matching item or null if not found.
    • findIntervalStart

      public Comparable<T> findIntervalStart(Comparable<T> x)
      Find the start of the interval where x is in.
      Parameters:
      x - is the item to search the start for.
      Returns:
      node containing the start of the interval where x is in, or null if x is not contained in any interval.
    • isEmpty

      public boolean isEmpty()
      Test if the tree is logically empty.
      Returns:
      true if empty, false otherwise.
    • insert

      protected gaiasky.util.BinaryNode<T> insert(T x, gaiasky.util.BinaryNode<T> t)
      Internal method to insert into a subtree.
      Parameters:
      x - the item to insert.
      t - the node that roots the tree.
      Returns:
      the new root.
      Throws:
      RuntimeException - if x is already present.
    • remove

      protected gaiasky.util.BinaryNode<T> remove(Comparable<T> x, gaiasky.util.BinaryNode<T> t)
      Internal method to remove from a subtree.
      Parameters:
      x - the item to remove.
      t - the node that roots the tree.
      Returns:
      the new root.
      Throws:
      RuntimeException - if x is not found.
    • removeMin

      protected gaiasky.util.BinaryNode<T> removeMin(gaiasky.util.BinaryNode<T> t)
      Internal method to remove minimum item from a subtree.
      Parameters:
      t - the node that roots the tree.
      Returns:
      the new root.
      Throws:
      RuntimeException - if x is not found.
    • findMin

      protected gaiasky.util.BinaryNode<T> findMin(gaiasky.util.BinaryNode<T> t)
      Internal method to find the smallest item in a subtree.
      Parameters:
      t - the node that roots the tree.
      Returns:
      node containing the smallest item.