类 TrieSET
- java.lang.Object
-
- edu.princeton.cs.algs4.TrieSET
-
- 所有已实现的接口:
java.lang.Iterable<java.lang.String>
public class TrieSET extends java.lang.Object implements java.lang.Iterable<java.lang.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.
-
-
构造器概要
构造器 构造器 说明 TrieSET()Initializes an empty set of strings.
-
方法概要
修饰符和类型 方法 说明 voidadd(java.lang.String key)Adds the key to the set if it is not already present.booleancontains(java.lang.String key)Does the set contain the given key?voiddelete(java.lang.String key)Removes the key from the set if the key is present.booleanisEmpty()Is the set empty?java.util.Iterator<java.lang.String>iterator()Returns all of the keys in the set, as an iterator.java.lang.Iterable<java.lang.String>keysThatMatch(java.lang.String pattern)Returns all of the keys in the set that matchpattern, where . symbol is treated as a wildcard character.java.lang.Iterable<java.lang.String>keysWithPrefix(java.lang.String prefix)Returns all of the keys in the set that start withprefix.java.lang.StringlongestPrefixOf(java.lang.String query)Returns the string in the set that is the longest prefix ofquery, ornull, if no such string.static voidmain(java.lang.String[] args)Unit tests theTrieSETdata type.intsize()Returns the number of strings in the set.
-
-
-
方法详细资料
-
contains
public boolean contains(java.lang.String key)
Does the set contain the given key?- 参数:
key- the key- 返回:
trueif the set containskeyandfalseotherwise- 抛出:
java.lang.IllegalArgumentException- ifkeyisnull
-
add
public void add(java.lang.String key)
Adds the key to the set if it is not already present.- 参数:
key- the key to add- 抛出:
java.lang.IllegalArgumentException- ifkeyisnull
-
size
public int size()
Returns the number of strings in the set.- 返回:
- the number of strings in the set
-
isEmpty
public boolean isEmpty()
Is the set empty?- 返回:
trueif the set is empty, andfalseotherwise
-
iterator
public java.util.Iterator<java.lang.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).- 指定者:
iterator在接口中java.lang.Iterable<java.lang.String>- 返回:
- an iterator to all of the keys in the set
-
keysWithPrefix
public java.lang.Iterable<java.lang.String> keysWithPrefix(java.lang.String prefix)
Returns all of the keys in the set that start withprefix.- 参数:
prefix- the prefix- 返回:
- all of the keys in the set that start with
prefix, as an iterable
-
keysThatMatch
public java.lang.Iterable<java.lang.String> keysThatMatch(java.lang.String pattern)
Returns all of the keys in the set that matchpattern, where . symbol is treated as a wildcard character.- 参数:
pattern- the pattern- 返回:
- all of the keys in the set that match
pattern, as an iterable, where . is treated as a wildcard character.
-
longestPrefixOf
public java.lang.String longestPrefixOf(java.lang.String query)
Returns the string in the set that is the longest prefix ofquery, ornull, if no such string.- 参数:
query- the query string- 返回:
- the string in the set that is the longest prefix of
query, ornullif no such string - 抛出:
java.lang.IllegalArgumentException- ifqueryisnull
-
delete
public void delete(java.lang.String key)
Removes the key from the set if the key is present.- 参数:
key- the key- 抛出:
java.lang.IllegalArgumentException- ifkeyisnull
-
main
public static void main(java.lang.String[] args)
Unit tests theTrieSETdata type.- 参数:
args- the command-line arguments
-
-