类 TST<Value>
- java.lang.Object
-
- edu.princeton.cs.algs4.TST<Value>
-
public class TST<Value> extends java.lang.Object
TheTST
class represents an 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 tonull
is 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.
-
-
构造器概要
构造器 构造器 说明 TST()
Initializes an empty string symbol table.
-
方法概要
修饰符和类型 方法 说明 boolean
contains(java.lang.String key)
Does this symbol table contain the given key?Value
get(java.lang.String key)
Returns the value associated with the given key.java.lang.Iterable<java.lang.String>
keys()
Returns all keys in the symbol table as anIterable
.java.lang.Iterable<java.lang.String>
keysThatMatch(java.lang.String pattern)
Returns all of the keys in the symbol table 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.String
longestPrefixOf(java.lang.String query)
Returns the string in the symbol table that is the longest prefix ofquery
, ornull
, if no such string.static void
main(java.lang.String[] args)
Unit tests theTST
data type.void
put(java.lang.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.
-
-
-
方法详细资料
-
size
public int size()
Returns the number of key-value pairs in this symbol table.- 返回:
- the number of key-value pairs in this symbol table
-
contains
public boolean contains(java.lang.String key)
Does this symbol table contain the given key?- 参数:
key
- the key- 返回:
true
if this symbol table containskey
andfalse
otherwise- 抛出:
java.lang.IllegalArgumentException
- ifkey
isnull
-
get
public Value get(java.lang.String key)
Returns the value associated with the given key.- 参数:
key
- the key- 返回:
- the value associated with the given key if the key is in the symbol table
and
null
if the key is not in the symbol table - 抛出:
java.lang.IllegalArgumentException
- ifkey
isnull
-
put
public void put(java.lang.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.- 参数:
key
- the keyval
- the value- 抛出:
java.lang.IllegalArgumentException
- ifkey
isnull
-
longestPrefixOf
public java.lang.String longestPrefixOf(java.lang.String query)
Returns the string in the symbol table that is the longest prefix ofquery
, ornull
, if no such string.- 参数:
query
- the query string- 返回:
- the string in the symbol table that is the longest prefix of
query
, ornull
if no such string - 抛出:
java.lang.IllegalArgumentException
- ifquery
isnull
-
keys
public java.lang.Iterable<java.lang.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())
.- 返回:
- all keys in the symbol table as an
Iterable
-
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 - 抛出:
java.lang.IllegalArgumentException
- ifprefix
isnull
-
keysThatMatch
public java.lang.Iterable<java.lang.String> keysThatMatch(java.lang.String pattern)
Returns all of the keys in the symbol table that matchpattern
, where . symbol is treated as a wildcard character.- 参数:
pattern
- the pattern- 返回:
- 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(java.lang.String[] args)
Unit tests theTST
data type.- 参数:
args
- the command-line arguments
-
-