Package algs35

Class ST<K extends Comparable<? super K>,V>

java.lang.Object
algs35.ST<K,V>
All Implemented Interfaces:
Iterable<K>

public class ST<K extends Comparable<? super K>,V> extends Object implements Iterable<K>
This class represents an ordered symbol table. It assumes that the keys are Comparable. It supports the usual put, get, contains, and remove methods. It also provides ordered methods for finding the minimum, maximum, floor, and ceiling.

The class implements the associative array abstraction: when associating a value with a key that is already in the table, the convention is to replace the old value with the new value. The class also uses the convention that values cannot be null. Setting the value associated with a key to null is equivalent to removing the key.

This class implements the Iterable interface for compatiblity with the version from Introduction to Programming in Java: An Interdisciplinary Approach.

This implementation uses a balanced binary search tree. The put, contains, remove, minimum, maximum, ceiling, and floor methods take logarithmic time.

For additional documentation, see Section 4.5 of Algorithms, 4th Edition by Robert Sedgewick and Kevin Wayne.

  • Constructor Summary

    Constructors
    Constructor
    Description
    ST()
    Create an empty symbol table.
  • Method Summary

    Modifier and Type
    Method
    Description
    ceil(K k)
    Return the smallest key in the table >= k.
    boolean
    contains(K key)
    Is the key in the table?
    delete(K key)
    Delete the key (and paired value) from table.
    floor(K k)
    Return the largest key in the table <= k.
    get(K key)
    Return the value paired with given key; null if key is not in table.
    Return an Iterator for the keys in the table.
    Return an Iterable for the keys in the table.
    static void
    main(String[] args)
     
    max()
    Return the largest key in the table.
    min()
    Return the smallest key in the table.
    void
    put(K key, V val)
    Put key-value pair into the symbol table.
    int
    How many keys are in the table?

    Methods inherited from class java.lang.Object

    equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait

    Methods inherited from interface java.lang.Iterable

    forEach, spliterator
  • Constructor Details

    • ST

      public ST()
      Create an empty symbol table.
  • Method Details

    • put

      public void put(K key, V val)
      Put key-value pair into the symbol table. Remove key from table if value is null.
    • get

      public V get(K key)
      Return the value paired with given key; null if key is not in table.
    • delete

      public V delete(K key)
      Delete the key (and paired value) from table. Return the value paired with given key; null if key is not in table.
    • contains

      public boolean contains(K key)
      Is the key in the table?
    • size

      public int size()
      How many keys are in the table?
    • keys

      public Iterable<K> keys()
      Return an Iterable for the keys in the table. To iterate over all of the keys in the symbol table st, use the foreach notation: for (K key : st.keys()).
    • iterator

      public Iterator<K> iterator()
      Return an Iterator for the keys in the table. To iterate over all of the keys in the symbol table st, use the foreach notation: for (K key : st). This method is for backward compatibility with the version from Introduction to Programming in Java: An Interdisciplinary Approach.
      Specified by:
      iterator in interface Iterable<K extends Comparable<? super K>>
    • min

      public K min()
      Return the smallest key in the table.
    • max

      public K max()
      Return the largest key in the table.
    • ceil

      public K ceil(K k)
      Return the smallest key in the table >= k.
    • floor

      public K floor(K k)
      Return the largest key in the table <= k.
    • main

      public static void main(String[] args)