Class ST<Key extends Comparable<Key>,Value>

Object
  extended by ST<Key,Value>
All Implemented Interfaces:
Iterable<Key>

public class ST<Key extends Comparable<Key>,Value>
extends Object
implements Iterable<Key>

This class represents an ordered symbol table. It assumes that the elements are Comparable. It supports the usual put, get, contains, and delete methods. It also provides ordered methods for finding the minimum, maximum, floor, and ceiling.

The class uses the convention that values cannot be null. Setting the value associated with a key to null is equivalent to removing the key.

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

For additional documentation, see Section 4.4 of Introduction to Programming in Java: An Interdisciplinary Approach by Robert Sedgewick and Kevin Wayne.


Constructor Summary
ST()
          Create an empty symbol table.
 
Method Summary
 Key ceil(Key k)
          Return the smallest key in the table >= k.
 boolean contains(Key key)
          Is the key in the table?
 Value delete(Key key)
          Delete the key (and paired value) from table.
 Key floor(Key k)
          Return the largest key in the table <= k.
 Value get(Key key)
          Return the value paired with given key; null if key is not in table.
 java.util.Iterator<Key> iterator()
          Return an Iterator for the keys in the table.
 Iterable<Key> keys()
          Return an Iterable for the keys in the table.
static void main(String[] args)
          Test routine.
 Key max()
          Return the largest key in the table.
 Key min()
          Return the smallest key in the table.
 void put(Key key, Value val)
          Put key-value pair into the symbol table.
 int size()
          How many keys are in the table?
 
Methods inherited from class Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

ST

public ST()
Create an empty symbol table.

Method Detail

put

public void put(Key key,
                Value val)
Put key-value pair into the symbol table. Remove key from table if value is null.


get

public Value get(Key key)
Return the value paired with given key; null if key is not in table.


delete

public Value delete(Key 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(Key key)
Is the key in the table?


size

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


iterator

public java.util.Iterator<Key> 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 (Key key : st).

Specified by:
iterator in interface Iterable<Key extends Comparable<Key>>

keys

public Iterable<Key> 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 (Key key : st.keys()).


min

public Key min()
Return the smallest key in the table.


max

public Key max()
Return the largest key in the table.


ceil

public Key ceil(Key k)
Return the smallest key in the table >= k.


floor

public Key floor(Key k)
Return the largest key in the table <= k.


main

public static void main(String[] args)
Test routine.