Package edu.princeton.cs.algs4
Class TrieSET
- Object
-
- edu.princeton.cs.algs4.TrieSET
-
public class TrieSET extends Object implements Iterable<String>
TheTrieSETclass represents an ordered set of strings over the extended ASCII alphabet. It supports the usual add, contains, and delete methods. It also provides character-based methods for finding the string in the set that is the longest prefix of a given prefix, finding all strings in the set that start with a given prefix, and finding all strings in the set that match a given pattern.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.
- Author:
- Robert Sedgewick, Kevin Wayne
-
-
Constructor Summary
Constructors Constructor Description TrieSET()Initializes an empty set of strings.
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description voidadd(String key)Adds the key to the set if it is not already present.booleancontains(String key)Does the set contain the given key?voiddelete(String key)Removes the key from the set if the key is present.booleanisEmpty()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 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 set that is the longest prefix ofquery, ornull, if no such string.static voidmain(String[] args)Unit tests theTrieSETdata type.intsize()Returns the number of strings in the set.-
Methods inherited from class java.lang.Object
clone, equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
-
Methods inherited from interface java.lang.Iterable
forEach, spliterator
-
-
-
-
Method Detail
-
contains
public boolean contains(String key)
Does the set contain the given key?- Parameters:
key- the key- Returns:
trueif the set containskeyandfalseotherwise- Throws:
IllegalArgumentException- ifkeyisnull
-
add
public void add(String key)
Adds the key to the set if it is not already present.- Parameters:
key- the key to add- Throws:
IllegalArgumentException- ifkeyisnull
-
size
public int size()
Returns the number of strings in the set.- Returns:
- the number of strings in the set
-
isEmpty
public boolean isEmpty()
Is the set empty?- Returns:
trueif the set is empty, andfalseotherwise
-
iterator
public Iterator<String> iterator()
Returns all of the keys in the set, as an iterator. To iterate over all of the keys in a set namedset, use the foreach notation:for (Key key : set).
-
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
-
keysThatMatch
public Iterable<String> keysThatMatch(String pattern)
Returns all of the keys in the set that matchpattern, where the character '.' is interpreted as a wildcard character.- Parameters:
pattern- the pattern- Returns:
- all of the keys in the set that match
pattern, as an iterable, where . is treated as a wildcard character.
-
longestPrefixOf
public String longestPrefixOf(String query)
Returns the string in the set that is the longest prefix ofquery, ornull, if no such string.- Parameters:
query- the query string- Returns:
- the string in the set that is the longest prefix of
query, ornullif no such string - Throws:
IllegalArgumentException- ifqueryisnull
-
delete
public void delete(String key)
Removes the key from the set if the key is present.- Parameters:
key- the key- Throws:
IllegalArgumentException- ifkeyisnull
-
main
public static void main(String[] args)
Unit tests theTrieSETdata type.- Parameters:
args- the command-line arguments
-
-