Package edu.princeton.cs.algs4
Class TST<Value>
- Object
 - 
- edu.princeton.cs.algs4.TST<Value>
 
 
- 
public class TST<Value> extends Object
TheTSTclass represents a symbol table of key-value pairs, with string keys and generic values. It supports the usual put, get, contains, delete, size, and is-empty methods. It also provides character-based methods for finding the string in the symbol table that is the longest prefix of a given prefix, finding all strings in the symbol table that start with a given prefix, and finding all strings in the symbol table that match a given pattern. 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.This implementation uses a ternary search trie.
For additional documentation, see Section 5.2 of Algorithms, 4th Edition by Robert Sedgewick and Kevin Wayne.
 
- 
- 
Constructor Summary
Constructors Constructor Description TST()Initializes an empty string symbol table. 
- 
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description booleancontains(String key)Does this symbol table contain the given key?Valueget(String key)Returns the value associated with the given key.Iterable<String>keys()Returns all keys in the symbol table as anIterable.Iterable<String>keysThatMatch(String pattern)Returns all of the keys in the symbol table that matchpattern, where the character '.' is interpreted as a wildcard character.Iterable<String>keysWithPrefix(String prefix)Returns all of the keys in the set that start withprefix.StringlongestPrefixOf(String query)Returns the string in the symbol table that is the longest prefix ofquery, ornull, if no such string.static voidmain(String[] args)Unit tests theTSTdata type.voidput(String key, Value val)Inserts the key-value pair into the symbol table, overwriting the old value with the new value if the key is already in the symbol table.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
 
 
- 
contains
public boolean contains(String 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(String key)
Returns the value associated with the given key.- 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(String key, Value val)
Inserts the key-value pair into the symbol table, overwriting the old value with the new value if the key is already in the symbol table. If the value isnull, this effectively deletes the key from the symbol table.- Parameters:
 key- the keyval- the value- Throws:
 IllegalArgumentException- ifkeyisnull
 
- 
longestPrefixOf
public String longestPrefixOf(String query)
Returns the string in the symbol table that is the longest prefix ofquery, ornull, if no such string.- Parameters:
 query- the query string- Returns:
 - the string in the symbol table that is the longest prefix of 
query, ornullif no such string - Throws:
 IllegalArgumentException- ifqueryisnull
 
- 
keys
public Iterable<String> 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 as an 
Iterable 
 
- 
keysWithPrefix
public Iterable<String> keysWithPrefix(String prefix)
Returns all of the keys in the set that start withprefix.- Parameters:
 prefix- the prefix- Returns:
 - all of the keys in the set that start with 
prefix, as an iterable - Throws:
 IllegalArgumentException- ifprefixisnull
 
- 
keysThatMatch
public Iterable<String> keysThatMatch(String pattern)
Returns all of the keys in the symbol table that matchpattern, where the character '.' is interpreted as a wildcard character.- Parameters:
 pattern- the pattern- Returns:
 - all of the keys in the symbol table that match 
pattern, as an iterable, where . is treated as a wildcard character. 
 
- 
main
public static void main(String[] args)
Unit tests theTSTdata type.- Parameters:
 args- the command-line arguments
 
 - 
 
 -