public class TrieSET extends Object implements Iterable<String>
This implementation uses a 256-way trie. The add, contains, delete, and longest prefix methods take time proportional to the length of the key (in the worst case). Construction takes constant time.
For additional documentation, see Section 5.2 of Algorithms in Java, 4th Edition by Robert Sedgewick and Kevin Wayne.
| Constructor and Description |
|---|
TrieSET()
Initializes an empty set of strings.
|
| Modifier and Type | Method and Description |
|---|---|
void |
add(String key)
Adds the key to the set if it is not already present.
|
boolean |
contains(String key)
Does the set contain the given key?
|
void |
delete(String key)
Removes the key from the set if the key is present.
|
boolean |
isEmpty()
Is the set empty?
|
Iterator<String> |
iterator()
Returns all of the keys in the set, as an iterator.
|
Iterable<String> |
keysThatMatch(String pattern)
Returns all of the keys in the set 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 set that is the longest prefix of query,
or null, if no such string.
|
static void |
main(String[] args)
Unit tests the TrieSET data type.
|
int |
size()
Returns the number of strings in the set.
|
public boolean contains(String key)
key - the keyNullPointerException - if key is nullpublic void add(String key)
key - the key to addNullPointerException - if key is nullpublic int size()
public boolean isEmpty()
public Iterator<String> iterator()
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.