Package dsa
Class TrieST<Value>
- java.lang.Object
-
- dsa.TrieST<Value>
-
public class TrieST<Value> extends Object
A data type to represent the trie data structure, which is a symbol table with with string keys.
-
-
Constructor Summary
Constructors Constructor Description TrieST()
Constructs an empty symbol table.
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description boolean
contains(String key)
Returnstrue
if this symbol table containskey
, andfalse
otherwise.void
delete(String key)
Deleteskey
and the associated value from this symbol table.Value
get(String key)
Returns the value associated withkey
in this symbol table, ornull
.boolean
isEmpty()
Returnstrue
if this symbol table is empty, andfalse
otherwise.Iterable<String>
keys()
Returns all the keys in this symbol table.Iterable<String>
keysThatMatch(String pattern)
Returns all the keys in this symbol table that matchpattern
, where the.
symbol is treated as a wildcard character.Iterable<String>
keysWithPrefix(String prefix)
Returns all the keys in this symbol table that start withprefix
.String
longestPrefixOf(String query)
Returns the string in this symbol table that is the longest prefix ofquery
, ornull
.static void
main(String[] args)
Unit tests the data type.void
put(String key, Value value)
Inserts thekey
andvalue
pair into this symbol table.int
size()
Returns the number of key-value pairs in this symbol table.String
toString()
Returns a string representation of this symbol table.
-
-
-
Method Detail
-
isEmpty
public boolean isEmpty()
Returnstrue
if this symbol table is empty, andfalse
otherwise.- Returns:
true
if this symbol table is empty, andfalse
otherwise.
-
size
public int size()
Returns the number of key-value pairs in this symbol table.- Returns:
- the number of key-value pairs in this symbol table.
-
put
public void put(String key, Value value)
Inserts thekey
andvalue
pair into this symbol table.- Parameters:
key
- the key.value
- the value.
-
get
public Value get(String key)
Returns the value associated withkey
in this symbol table, ornull
.- Parameters:
key
- the key.- Returns:
- the value associated with
key
in this symbol table, ornull
.
-
contains
public boolean contains(String key)
Returnstrue
if this symbol table containskey
, andfalse
otherwise.- Parameters:
key
- the key.- Returns:
true
if this symbol table contains key, andfalse
otherwise.
-
delete
public void delete(String key)
Deleteskey
and the associated value from this symbol table.- Parameters:
key
- the key.
-
keys
public Iterable<String> keys()
Returns all the keys in this symbol table.- Returns:
- all the keys in this symbol table.
-
keysWithPrefix
public Iterable<String> keysWithPrefix(String prefix)
Returns all the keys in this symbol table that start withprefix
.- Parameters:
prefix
- the prefix.- Returns:
- all the keys in this symbol table that start with
prefix
.
-
keysThatMatch
public Iterable<String> keysThatMatch(String pattern)
Returns all the keys in this symbol table that matchpattern
, where the.
symbol is treated as a wildcard character.- Parameters:
pattern
- the pattern.- Returns:
- all the keys in this symbol table that match
pattern
, where the.
symbol is treated as a wildcard character.
-
longestPrefixOf
public String longestPrefixOf(String query)
Returns the string in this symbol table that is the longest prefix ofquery
, ornull
.- Parameters:
query
- the string.- Returns:
- the string in this symbol table that is the longest prefix of
query
, ornull
.
-
toString
public String toString()
Returns a string representation of this symbol table.
-
main
public static void main(String[] args)
Unit tests the data type.- Parameters:
args
- the command-line arguments.
-
-