/*************************************************************************
* Compilation: javac ST.java
* Execution: java ST
*
* Sorted symbol table implementation using a java.util.TreeMap.
* Does not allow duplicates.
*
* % java ST
*
*************************************************************************/
package chap3;
import java.util.Iterator;
import java.util.SortedMap;
import java.util.TreeMap;
/**
* This class represents an ordered symbol table. It assumes that
* the elements 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 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 add, 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.
*/
public class ST, Value> implements Iterable {
private TreeMap st;
/**
* Create an empty symbol table.
*/
public ST() {
st = new TreeMap();
}
/**
* Put key-value pair into the symbol table. Remove key from table if
* value is null.
*/
public void put(Key key, Value val) {
if (val == null) st.remove(key);
else st.put(key, val);
}
/**
* Return the value paired with given key; null if key is not in table.
*/
public Value get(Key key) {
return st.get(key);
}
/**
* Delete the key (and paired value) from table.
* Return the value paired with given key; null if key is not in table.
*/
public Value delete(Key key) {
return st.remove(key);
}
/**
* Is the key in the table?
*/
public boolean contains(Key key) {
return st.containsKey(key);
}
/**
* How many keys are in the table?
*/
public int size() {
return st.size();
}
/**
* 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()).
*/
public Iterable keys() {
return st.keySet();
}
/**
* 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).
* This method is for backward compatibility with the version from Introduction
* to Programming in Java: An Interdisciplinary Approach.
*/
public Iterator iterator() {
return st.keySet().iterator();
}
/**
* Return the smallest key in the table.
*/
public Key min() {
return st.firstKey();
}
/**
* Return the largest key in the table.
*/
public Key max() {
return st.lastKey();
}
/**
* Return the smallest key in the table >= k.
*/
public Key ceil(Key k) {
SortedMap tail = st.tailMap(k);
if (tail.isEmpty()) return null;
else return tail.firstKey();
}
/**
* Return the largest key in the table <= k.
*/
public Key floor(Key k) {
if (st.containsKey(k)) return k;
// does not include key if present (!)
SortedMap head = st.headMap(k);
if (head.isEmpty()) return null;
else return head.lastKey();
}
}