Package project5

Class BST<E extends Comparable<E>>

Object
BST<E>
Type Parameters:
E - the type of elements maintained by this set
All Implemented Interfaces:
Iterable<E>

public class BST<E extends Comparable<E>> extends Object implements Iterable<E>
An implementation of a binary search tree. The elements are ordered using their natural ordering.

This implementation provides guaranteed O(H) (H is the height of this tree which could be as low as logN for balanced trees, but could be as large as N for unbalanced trees) time cost for the basic operations (add, remove and contains).

This class implements many of the methods provided by the Java framework's TreeSet class.

  • Constructor Summary

    Constructors
    Constructor
    Description
    BST()
    Constructs a new, empty tree, sorted according to the natural ordering of its elements.
    BST(E[] collection)
    Constructs a new tree containing the elements in the specified collection array, sorted according to the natural ordering of its elements.
  • Method Summary

    Modifier and Type
    Method
    Description
    boolean
    add(E e)
    Adds the specified element to this set if it is not already present.
    void
    Removes all of the elements from this set.
    boolean
    Returns true if this set contains the specified element.
    boolean
    Compares the specified object with this tree for equality.
    Returns the first (smallest) element currently in this tree.
    get(int index)
    Returns the element at the specified position in this tree.
    int
    Returns the height of this tree.
    boolean
    Returns true if this set contains no elements.
    Returns an iterator over the elements in this tree in ascending order.
    Returns the last (largest) element currently in this tree.
    Returns an iterator over the elements in this tree in order of the postorder traversal.
    Returns an iterator over the elements in this tree in order of the preorder traversal.
    boolean
    Removes the specified element from this tree if it is present.
    int
    Returns the number of elements in this tree.
    Returns a string representation of this tree.
    Produces tree like string representation of this tree.

    Methods inherited from class Object

    clone, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait

    Methods inherited from interface Iterable

    forEach, spliterator
  • Constructor Details

    • BST

      public BST()
      Constructs a new, empty tree, sorted according to the natural ordering of its elements. All elements inserted into the tree must implement the Comparable interface.
      This operation should be O(1).
    • BST

      public BST(E[] collection)
      Constructs a new tree containing the elements in the specified collection array, sorted according to the natural ordering of its elements. All elements inserted into the tree must implement the Comparable interface.
      This operation should be O(N logN) where N is the number of elements in the collection. This implies, that the tree that is constructed has to have the high that is approximately logN, not N.
      Parameters:
      collection - collection whose elements will comprise the new tree
      Throws:
      NullPointerException - if the specified collection is null
  • Method Details

    • add

      public boolean add(E e)
      Adds the specified element to this set if it is not already present. More formally, adds the specified element e to this tree if the set contains no element e2 such that Objects.equals(e, e2). If this set already contains the element, the call leaves the set unchanged and returns false.
      This operation should be O(H).
      Parameters:
      e - element to be added to this set
      Returns:
      true if this set did not already contain the specified element
      Throws:
      NullPointerException - if the specified element is null and this set uses natural ordering, or its comparator does not permit null elements
    • remove

      public boolean remove(Object o)
      Removes the specified element from this tree if it is present. More formally, removes an element e such that Objects.equals(o, e), if this tree contains such an element. Returns true if this tree contained the element (or equivalently, if this tree changed as a result of the call). (This tree will not contain the element once the call returns.)
      This operation should be O(H).
      Parameters:
      o - object to be removed from this set, if present
      Returns:
      true if this set contained the specified element
      Throws:
      ClassCastException - if the specified object cannot be compared with the elements currently in this tree
      NullPointerException - if the specified element is null
    • clear

      public void clear()
      Removes all of the elements from this set. The set will be empty after this call returns.
      This operation should be O(1).
    • contains

      public boolean contains(Object o)
      Returns true if this set contains the specified element. More formally, returns true if and only if this set contains an element e such that Objects.equals(o, e).
      This operation should be O(H).
      Parameters:
      o - object to be checked for containment in this set
      Returns:
      true if this set contains the specified element
      Throws:
      ClassCastException - if the specified object cannot be compared with the elements currently in the set
      NullPointerException - if the specified element is null and this set uses natural ordering, or its comparator does not permit null elements
    • size

      public int size()
      Returns the number of elements in this tree.
      This operation should be O(1).
      Returns:
      the number of elements in this tree
    • isEmpty

      public boolean isEmpty()
      Returns true if this set contains no elements.
      This operation should be O(1).
      Returns:
      true if this set contains no elements
    • height

      public int height()
      Returns the height of this tree. The height of a leaf is 1. The height of the tree is the height of its root node.
      This operation should be O(1).
      Returns:
      the height of this tree or zero if the tree is empty
    • iterator

      public Iterator<E> iterator()
      Returns an iterator over the elements in this tree in ascending order.
      This operation should be O(N).
      Specified by:
      iterator in interface Iterable<E extends Comparable<E>>
      Returns:
      an iterator over the elements in this set in ascending order
    • preorderIterator

      public Iterator<E> preorderIterator()
      Returns an iterator over the elements in this tree in order of the preorder traversal.
      This operation should be O(N).
      Returns:
      an iterator over the elements in this tree in order of the preorder traversal
    • postorderIterator

      public Iterator<E> postorderIterator()
      Returns an iterator over the elements in this tree in order of the postorder traversal.
      This operation should be O(N).
      Returns:
      an iterator over the elements in this tree in order of the postorder traversal
    • get

      public E get(int index)
      Returns the element at the specified position in this tree. The order of the indexed elements is the same as provided by this tree's iterator. The indexing is zero based (i.e., the smallest element in this tree is at index 0 and the largest one is at index size()-1).
      This operation should be O(H).
      Parameters:
      index - index of the element to return
      Returns:
      the element at the specified position in this tree
      Throws:
      IndexOutOfBoundsException - if the index is out of range (index < 0 || index >= size())
    • first

      public E first()
      Returns the first (smallest) element currently in this tree.
      This operation should be O(H).
      Returns:
      the first (smallest) element currently in this tree
      Throws:
      NoSuchElementException - if this set is empty
    • last

      public E last()
      Returns the last (largest) element currently in this tree.
      This operation should be O(H).
      Returns:
      the last (largest) element currently in this tree
      Throws:
      NoSuchElementException - if this set is empty
    • equals

      public boolean equals(Object obj)
      Compares the specified object with this tree for equality. Returns true if the given object is also a tree, the two trees have the same size, and every member of the given tree is contained in this tree.
      This operation should be O(N).
      Overrides:
      equals in class Object
      Parameters:
      obj - object to be compared for equality with this tree
      Returns:
      true if the specified object is equal to this tree
    • toString

      public String toString()
      Returns a string representation of this tree. The string representation consists of a list of the tree's elements in the order they are returned by its iterator (inorder traversal), enclosed in square brackets ("[]"). Adjacent elements are separated by the characters ", " (comma and space). Elements are converted to strings as by String.valueOf(Object).
      This operation should be O(N).
      Overrides:
      toString in class Object
      Returns:
      a string representation of this collection
    • toStringTreeFormat

      public String toStringTreeFormat()
      Produces tree like string representation of this tree. Returns a string representation of this tree in a tree-like format. The string representation consists of a tree-like representation of this tree. Each node is shown in its own line with the indentation showing the depth of the node in this tree. The root is printed on the first line, followed by its left subtree, followed by its right subtree. For example
       K 
       |--D
          |--B
             |--A
                |--null
                |--null
             |--null
          |--J
             |--null
             |--null
       |--P
          |--M
             |--L
                |--null
                |--null
             |--O
                |--N
                         |--null
                   |--null
                |--null
          |--null
       
      is a string representation of this tree:
        
       
                  K 
               /     \
             D        P
           /   \     /   
          B     J   M
         /        /   \
        A        L     O
                     /
                    N
       
       

      This operation should be O(N).
      Returns:
      string containing tree-like representation of this tree.