Class BinarySearchST<Key extends Comparable<Key>,Value>
- Object
-
- edu.princeton.cs.algs4.BinarySearchST<Key,Value>
-
public class BinarySearchST<Key extends Comparable<Key>,Value> extends Object
TheBSTclass represents an ordered symbol table of generic key-value pairs. It supports the usual put, get, contains, delete, size, and is-empty methods. It also provides ordered methods for finding the minimum, maximum, floor, select, and ceiling. It also provides a keys method for iterating over all of the keys. A symbol table implements the associative array abstraction: when associating a value with a key that is already in the symbol table, the convention is to replace the old value with the new value. UnlikeMap, this class uses the convention that values cannot benull—setting the value associated with a key tonullis equivalent to deleting the key from the symbol table.It requires that the key type implements the
Comparableinterface and calls thecompareTo()and method to compare two keys. It does not call eitherequals()orhashCode().This implementation uses a sorted array. The put and remove operations take Θ(n) time in the worst case. The contains, ceiling, floor, and rank operations take Θ(log n) time in the worst case. The size, is-empty, minimum, maximum, and select operations take Θ(1) time. Construction takes Θ(1) time.
For alternative implementations of the symbol table API, see
ST,BST,SequentialSearchST,RedBlackBST,SeparateChainingHashST, andLinearProbingHashST, For additional documentation, see Section 3.1 of Algorithms, 4th Edition by Robert Sedgewick and Kevin Wayne.
-
-
Constructor Summary
Constructors Constructor Description BinarySearchST()Initializes an empty symbol table.BinarySearchST(int capacity)Initializes an empty symbol table with the specified initial capacity.
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description Keyceiling(Key key)Returns the smallest key in this symbol table greater than or equal tokey.booleancontains(Key key)Does this symbol table contain the given key?voiddelete(Key key)Removes the specified key and associated value from this symbol table (if the key is in the symbol table).voiddeleteMax()Removes the largest key and associated value from this symbol table.voiddeleteMin()Removes the smallest key and associated value from this symbol table.Keyfloor(Key key)Returns the largest key in this symbol table less than or equal tokey.Valueget(Key key)Returns the value associated with the given key in this symbol table.booleanisEmpty()Returns true if this symbol table is empty.Iterable<Key>keys()Returns all keys in this symbol table as anIterable.Iterable<Key>keys(Key lo, Key hi)Returns all keys in this symbol table in the given range, as anIterable.static voidmain(String[] args)Unit tests theBinarySearchSTdata type.Keymax()Returns the largest key in this symbol table.Keymin()Returns the smallest key in this symbol table.voidput(Key key, Value val)Inserts the specified key-value pair into the symbol table, overwriting the old value with the new value if the symbol table already contains the specified key.intrank(Key key)Returns the number of keys in this symbol table strictly less thankey.Keyselect(int k)Return the kth smallest key in this symbol table.intsize()Returns the number of key-value pairs in this symbol table.intsize(Key lo, Key hi)Returns the number of keys in this symbol table in the specified range.
-
-
-
Method Detail
-
size
public int size()
Returns the number of key-value pairs in this symbol table.- Returns:
- the number of key-value pairs in this symbol table
-
isEmpty
public boolean isEmpty()
Returns true if this symbol table is empty.- Returns:
trueif this symbol table is empty;falseotherwise
-
contains
public boolean contains(Key key)
Does this symbol table contain the given key?- Parameters:
key- the key- Returns:
trueif this symbol table containskeyandfalseotherwise- Throws:
IllegalArgumentException- ifkeyisnull
-
get
public Value get(Key key)
Returns the value associated with the given key in this symbol table.- Parameters:
key- the key- Returns:
- the value associated with the given key if the key is in the symbol table
and
nullif the key is not in the symbol table - Throws:
IllegalArgumentException- ifkeyisnull
-
rank
public int rank(Key key)
Returns the number of keys in this symbol table strictly less thankey.- Parameters:
key- the key- Returns:
- the number of keys in the symbol table strictly less than
key - Throws:
IllegalArgumentException- ifkeyisnull
-
put
public void put(Key key, Value val)
Inserts the specified key-value pair into the symbol table, overwriting the old value with the new value if the symbol table already contains the specified key. Deletes the specified key (and its associated value) from this symbol table if the specified value isnull.- Parameters:
key- the keyval- the value- Throws:
IllegalArgumentException- ifkeyisnull
-
delete
public void delete(Key key)
Removes the specified key and associated value from this symbol table (if the key is in the symbol table).- Parameters:
key- the key- Throws:
IllegalArgumentException- ifkeyisnull
-
deleteMin
public void deleteMin()
Removes the smallest key and associated value from this symbol table.- Throws:
NoSuchElementException- if the symbol table is empty
-
deleteMax
public void deleteMax()
Removes the largest key and associated value from this symbol table.- Throws:
NoSuchElementException- if the symbol table is empty
-
min
public Key min()
Returns the smallest key in this symbol table.- Returns:
- the smallest key in this symbol table
- Throws:
NoSuchElementException- if this symbol table is empty
-
max
public Key max()
Returns the largest key in this symbol table.- Returns:
- the largest key in this symbol table
- Throws:
NoSuchElementException- if this symbol table is empty
-
select
public Key select(int k)
Return the kth smallest key in this symbol table.- Parameters:
k- the order statistic- Returns:
- the
kth smallest key in this symbol table - Throws:
IllegalArgumentException- unlesskis between 0 and n–1
-
floor
public Key floor(Key key)
Returns the largest key in this symbol table less than or equal tokey.- Parameters:
key- the key- Returns:
- the largest key in this symbol table less than or equal to
key - Throws:
NoSuchElementException- if there is no such keyIllegalArgumentException- ifkeyisnull
-
ceiling
public Key ceiling(Key key)
Returns the smallest key in this symbol table greater than or equal tokey.- Parameters:
key- the key- Returns:
- the smallest key in this symbol table greater than or equal to
key - Throws:
NoSuchElementException- if there is no such keyIllegalArgumentException- ifkeyisnull
-
size
public int size(Key lo, Key hi)
Returns the number of keys in this symbol table in the specified range.- Parameters:
lo- minimum endpointhi- maximum endpoint- Returns:
- the number of keys in this symbol table between
lo(inclusive) andhi(inclusive) - Throws:
IllegalArgumentException- if eitherloorhiisnull
-
keys
public Iterable<Key> keys()
Returns all keys in this symbol table as anIterable. To iterate over all of the keys in the symbol table namedst, use the foreach notation:for (Key key : st.keys()).- Returns:
- all keys in this symbol table
-
keys
public Iterable<Key> keys(Key lo, Key hi)
Returns all keys in this symbol table in the given range, as anIterable.- Parameters:
lo- minimum endpointhi- maximum endpoint- Returns:
- all keys in this symbol table between
lo(inclusive) andhi(inclusive) - Throws:
IllegalArgumentException- if eitherloorhiisnull
-
main
public static void main(String[] args)
Unit tests theBinarySearchSTdata type.- Parameters:
args- the command-line arguments
-
-