Package edu.princeton.cs.algs4
Class SequentialSearchST<Key,Value>
- Object
-
- edu.princeton.cs.algs4.SequentialSearchST<Key,Value>
-
public class SequentialSearchST<Key,Value> extends Object
TheSequentialSearchSTclass represents an (unordered) symbol table of generic key-value pairs. It supports the usual put, get, contains, delete, size, and is-empty methods. 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. The class also 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 relies on the
equals()method to test whether two keys are equal. It does not call either thecompareTo()orhashCode()method.This implementation uses a singly linked list and sequential search. The put and delete operations take Θ(n). The get and contains operations takes Θ(n) time in the worst case. The size, and is-empty operations take Θ(1) time. Construction takes Θ(1) time.
For additional documentation, see Section 3.1 of Algorithms, 4th Edition by Robert Sedgewick and Kevin Wayne.
- Author:
- Robert Sedgewick, Kevin Wayne
-
-
Constructor Summary
Constructors Constructor Description SequentialSearchST()Initializes an empty symbol table.
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description booleancontains(Key key)Returns true if this symbol table contains the specified key.voiddelete(Key key)Removes the specified key and its associated value from this symbol table (if the key is in this symbol table).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 the symbol table as anIterable.static voidmain(String[] args)Unit tests theSequentialSearchSTdata type.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.intsize()Returns the number of key-value pairs in this symbol table.
-
-
-
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)
Returns true if this symbol table contains the specified key.- Parameters:
key- the key- Returns:
trueif this symbol table containskey;falseotherwise- 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
-
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 its associated value from this symbol table (if the key is in this symbol table).- Parameters:
key- the key- Throws:
IllegalArgumentException- ifkeyisnull
-
keys
public Iterable<Key> keys()
Returns all keys in the 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 the symbol table
-
main
public static void main(String[] args)
Unit tests theSequentialSearchSTdata type.- Parameters:
args- the command-line arguments
-
-