public class TernaryTrie extends java.lang.Object implements TaggedStrings
TernaryTrie
is an implementation of a ternary tree.
Methods are provided for inserting strings and searching for strings.
The algorithms in this class are all recursive, and have not been
optimized for any particular purpose.
Data which is inserted is not sorted before insertion, however data
can be inserted beginning with the median of the supplied data.
Modifier and Type | Field and Description |
---|---|
protected int |
maxKeyLength
Maximum key length.
|
protected int |
nodeCount
Count of nodes in tree.
|
protected TernaryTrieNode |
root
root node of the ternary tree
|
Constructor and Description |
---|
TernaryTrie()
Construct empty ternary trie.
|
TernaryTrie(java.util.List<java.lang.String> stringsList)
Construct a ternary trie from values in a list.
|
TernaryTrie(java.util.Map<java.lang.String,java.lang.String> stringsMap,
boolean addValuesAsKeys)
Construct a ternary trie from keys and values in a map.
|
TernaryTrie(java.util.Set<java.lang.String> stringsSet)
Construct a ternary trie from values in a set.
|
TernaryTrie(TaggedStrings stringsList,
boolean addValuesAsKeys)
Construct a ternary trie from values in a tagged strings list.
|
Modifier and Type | Method and Description |
---|---|
void |
addCollection(java.util.Collection<java.lang.String> collection)
Add words from a collection.
|
boolean |
containsKey(java.lang.String word)
Check if trie contains a specified key.
|
boolean |
containsString(java.lang.String string)
See if specified string exists.
|
protected TernaryTrieNode |
findNode(TernaryTrieNode node,
java.lang.String word,
int index) |
java.lang.Object |
get(java.lang.String word)
Get associated value for word.
|
java.util.Set<java.lang.String> |
getAllStrings()
Get set of all unique string values.
|
java.util.Set<java.lang.String> |
getAllTags()
Get set of all unique tag values as strings.
|
int |
getStringCount()
Get number of strings.
|
java.lang.String |
getTag(java.lang.String string)
Get the tag value associated with a string.
|
java.util.List<java.lang.String> |
getWords()
This will return an array of all the words in this
TernaryTrie . |
protected TernaryTrieNode |
insertNode(TernaryTrieNode node,
java.lang.String word,
int index,
java.lang.Object value)
This will recursively insert a word into the
TernaryTrie
one node at a time beginning at the supplied node. |
java.util.List<java.lang.String> |
nearSearch(java.lang.String word,
int distance)
This will return an array of strings which are near to the supplied
word by the supplied distance.
|
protected java.util.List<java.lang.String> |
nearSearchNode(TernaryTrieNode node,
int distance,
java.util.List<java.lang.String> matches,
java.lang.String match,
java.lang.String word,
int index)
This will recursively search for a near match word in the
TernaryTrie
one node at a time beginning at the supplied node. |
java.util.List<java.lang.String> |
partialSearch(java.lang.String word)
This will return an array of strings which partially match the supplied
word.
|
protected java.util.List<java.lang.String> |
partialSearchNode(TernaryTrieNode node,
java.util.List<java.lang.String> matches,
java.lang.String match,
java.lang.String word,
int index)
This will recursively search for a partial word in the
TernaryTrie
one node at a time beginning at the supplied node. |
java.util.List<java.lang.String> |
prefixSearch(java.lang.String prefix) |
protected java.util.List<java.lang.String> |
prefixSearchNode(TernaryTrieNode node,
java.util.List<java.lang.String> matches,
java.lang.String match,
java.lang.String prefix,
java.lang.String prefixPattern,
int index) |
java.lang.Object |
put(java.lang.String word,
java.lang.Object value)
Add word and associated value to trie.
|
void |
putTag(java.lang.String string,
java.lang.String tag)
Set the tag value associated with a string.
|
protected boolean |
searchNode(TernaryTrieNode node,
java.lang.String word,
int index)
This will recursively search for a word in the
TernaryTrie
one node at a time beginning at the supplied node. |
int |
size()
Return size of trie (# of terminal nodes)
|
protected java.util.List<java.lang.String> |
traverseNode(TernaryTrieNode node,
java.lang.String s,
java.util.List<java.lang.String> words)
This will recursively traverse every node in the
TernaryTrie
one node at a time beginning at the supplied node. |
protected TernaryTrieNode root
protected int nodeCount
protected int maxKeyLength
public TernaryTrie()
public TernaryTrie(java.util.Map<java.lang.String,java.lang.String> stringsMap, boolean addValuesAsKeys)
stringsMap
- The map whose keys and values are to be
added to the trie.addValuesAsKeys
- True to add each value as a key
as well.public TernaryTrie(java.util.Set<java.lang.String> stringsSet)
stringsSet
- The set whose values are to be added
to the trie.public TernaryTrie(java.util.List<java.lang.String> stringsList)
stringsList
- The list whose values are to be added
to the trie.public TernaryTrie(TaggedStrings stringsList, boolean addValuesAsKeys)
stringsList
- The tagged strings list whose values
are to be added to the trie.addValuesAsKeys
- True to add each value as a key
as well.public void addCollection(java.util.Collection<java.lang.String> collection)
collection
- Collection whose values
are to be inserted into
trie.public java.lang.Object put(java.lang.String word, java.lang.Object value)
word
- String to insert.value
- Object value.public java.lang.Object get(java.lang.String word)
word
- String whose associated value we want.public boolean containsKey(java.lang.String word)
word
- The key to look up.public java.util.List<java.lang.String> partialSearch(java.lang.String word)
This will return an array of strings which partially match the supplied word. word should be of the format '.e.e.e' Where the '.' character represents any valid character. Possible results from this query include: Helene, delete, or severe Note that no substring matching occurs, results only include strings of the same length. If the supplied word does not contain the '.' character, then a regular search is preformed.
word
- String
to search forString[]
- of matching wordspublic java.util.List<java.lang.String> prefixSearch(java.lang.String prefix)
protected java.util.List<java.lang.String> prefixSearchNode(TernaryTrieNode node, java.util.List<java.lang.String> matches, java.lang.String match, java.lang.String prefix, java.lang.String prefixPattern, int index)
public java.util.List<java.lang.String> nearSearch(java.lang.String word, int distance)
This will return an array of strings which are near to the supplied word by the supplied distance. For the query nearSearch("fisher", 2): Possible results include: cipher, either, fishery, kosher, sister. If the supplied distance is not > 0, then a regular search is preformed.
word
- String
to search fordistance
- int
for valid matchString[]
- of matching wordspublic java.util.List<java.lang.String> getWords()
This will return an array of all the words in this
TernaryTrie
.
This is a very expensive operation, every node in the tree is traversed.
String[]
- of wordsprotected TernaryTrieNode insertNode(TernaryTrieNode node, java.lang.String word, int index, java.lang.Object value)
This will recursively insert a word into the TernaryTrie
one node at a time beginning at the supplied node.
node
- TernaryTrieNode
to put character inword
- String
to be insertedindex
- int
of character in wordTernaryTrieNode
- to insertprotected boolean searchNode(TernaryTrieNode node, java.lang.String word, int index)
This will recursively search for a word in the TernaryTrie
one node at a time beginning at the supplied node.
node
- TernaryTrieNode
to search inword
- String
to search forindex
- int
of character in wordboolean
- whether or not word was foundprotected TernaryTrieNode findNode(TernaryTrieNode node, java.lang.String word, int index)
protected java.util.List<java.lang.String> partialSearchNode(TernaryTrieNode node, java.util.List<java.lang.String> matches, java.lang.String match, java.lang.String word, int index)
This will recursively search for a partial word in the
TernaryTrie
one node at a time beginning at the supplied node.
node
- TernaryTrieNode
to search inmatches
- ArrayList
of partial matchesmatch
- String
the current word being examinedword
- String
to search forindex
- int
of character in wordArrayList
- of matchesprotected java.util.List<java.lang.String> nearSearchNode(TernaryTrieNode node, int distance, java.util.List<java.lang.String> matches, java.lang.String match, java.lang.String word, int index)
This will recursively search for a near match word in the
TernaryTrie
one node at a time beginning at the supplied node.
node
- TernaryTrieNode
to search indistance
- int
of a valid match, must be > 0matches
- ArrayList
of near matchesmatch
- String
the current word being examinedword
- String
to search forindex
- int
of character in wordArrayList
- of matchesprotected java.util.List<java.lang.String> traverseNode(TernaryTrieNode node, java.lang.String s, java.util.List<java.lang.String> words)
This will recursively traverse every node in the
TernaryTrie
one node at a time beginning at the supplied node.
The result is a string representing every word, which is delimited by
the LINE_SEPARATOR character.
node
- TernaryTrieNode
to begin traversings
- String
of words found at the supplied nodewords
- ArrayList
which will be returned
(recursive function)String
- containing all words from the supplied nodepublic int size()
public boolean containsString(java.lang.String string)
containsString
in interface TaggedStrings
string
- The string.public java.lang.String getTag(java.lang.String string)
getTag
in interface TaggedStrings
string
- The string.public void putTag(java.lang.String string, java.lang.String tag)
putTag
in interface TaggedStrings
string
- The string.tag
- The tag.public int getStringCount()
getStringCount
in interface TaggedStrings
public java.util.Set<java.lang.String> getAllStrings()
getAllStrings
in interface TaggedStrings
public java.util.Set<java.lang.String> getAllTags()
getAllTags
in interface TaggedStrings