public class TrieST<Value> extends Object
Map, this class uses the convention that
values cannot be null—setting the
value associated with a key to null is equivalent to deleting the key
from the symbol table.
This implementation uses a 256-way trie. The put, contains, delete, and longest prefix operations take time proportional to the length of the key (in the worst case). Construction takes constant time. The size, and is-empty operations take constant time. Construction takes constant time.
For additional documentation, see Section 5.2 of Algorithms, 4th Edition by Robert Sedgewick and Kevin Wayne.
| Constructor and Description |
|---|
TrieST() |
| Modifier and Type | Method and Description |
|---|---|
boolean |
contains(String key)
Does this symbol table contain the given key?
|
void |
delete(String key)
Removes the key from the set if the key is present.
|
Value |
get(String key)
Returns the value associated with the given key.
|
boolean |
isEmpty()
Is this symbol table empty?
|
Iterable<String> |
keys()
Returns all keys in the symbol table as an Iterable.
|
Iterable<String> |
keysThatMatch(String pattern)
Returns all of the keys in the symbol table that match pattern,
where .
|
Iterable<String> |
keysWithPrefix(String prefix)
Returns all of the keys in the set that start with prefix.
|
String |
longestPrefixOf(String query)
Returns the string in the symbol table that is the longest prefix of query,
or null, if no such string.
|
static void |
main(String[] args)
Unit tests the TrieSET data type.
|
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.
|
int |
size()
Returns the number of key-value pairs in this symbol table.
|
public Value get(String key)
key - the keyNullPointerException - if key is nullpublic boolean contains(String key)
key - the keyNullPointerException - if key is nullpublic void put(String key, Value val)
key - the keyval - the valueNullPointerException - if key is nullpublic int size()
public boolean isEmpty()
public Iterable<String> keys()
public Iterable<String> keysWithPrefix(String prefix)
prefix - the prefixpublic Iterable<String> keysThatMatch(String pattern)
pattern - the patternpublic String longestPrefixOf(String query)
query - the query stringNullPointerException - if query is nullpublic void delete(String key)
key - the keyNullPointerException - if key is nullpublic static void main(String[] args)
Copyright © 2014. All Rights Reserved.