Package org.passay.dictionary
Class TernaryTree
java.lang.Object
org.passay.dictionary.TernaryTree
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.
-
Field Summary
FieldsModifier and TypeFieldDescriptionprotected static final Comparator<String>Case insensitive comparator.protected static final Comparator<String>Case sensitive comparator.protected final Comparator<String>Character comparator.private static final String[]An empty results array.private static final StringFile system line separator.private TernaryNoderoot node of the ternary tree. -
Constructor Summary
ConstructorsConstructorDescriptionCreates an empty case-sensitive ternary tree.TernaryTree(boolean caseSensitive) Creates an empty ternary tree with the given case sensitivity. -
Method Summary
Modifier and TypeMethodDescriptionReturns a histogram of how many words end at each depth.getNodeStats(TernaryNode node, int depth, Map<Integer, Integer> histogram) Returns a histogram of how many words end at each depth.getWords()Returns a list of all the words in this ternary tree.voidinsert(CharSequence word) Inserts the supplied word into this tree.voidinsert(CharSequence[] words) Inserts the supplied array of words into this tree.private TernaryNodeinsertNode(TernaryNode node, CharSequence word, int index) Recursively inserts a word into the ternary tree one node at a time beginning at the supplied node.nearSearch(CharSequence word, int distance) Return an array of strings which are near to the supplied word by the supplied distance.private List<CharSequence>nearSearchNode(TernaryNode node, int distance, List<CharSequence> matches, CharSequence match, CharSequence word, int index) Recursively searches for a near match word in the ternary tree one node at a time beginning at the supplied node.partialSearch(CharSequence word) Returns 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.private List<CharSequence>partialSearchNode(TernaryNode node, List<CharSequence> matches, CharSequence match, CharSequence word, int index) Recursively searches for a partial word in the ternary tree one node at a time beginning at the supplied node.voidPrints an ASCII representation of this ternary tree to the supplied writer.voidPrints an ASCII representation of this ternary tree to the supplied writer.private voidprintNode(TernaryNode node, String s, int depth, boolean fullPath, StringBuilder buffer) Recursively traverses every node in the ternary tree rooted at the supplied node.booleansearch(CharSequence word) Returns whether the supplied word has been inserted into this ternary tree.private booleansearchNode(TernaryNode node, int[] word, int index) Recursively searches for a word in the ternary tree one node at a time beginning at the supplied node.private List<CharSequence>traverseNode(TernaryNode node, String s, List<CharSequence> words) Recursively traverses every node in the ternary tree one node at a time beginning at the supplied node.
-
Field Details
-
CASE_SENSITIVE_COMPARATOR
Case sensitive comparator. -
CASE_INSENSITIVE_COMPARATOR
Case insensitive comparator. -
LINE_SEPARATOR
File system line separator. -
EMPTY_ARRAY
An empty results array. -
comparator
Character comparator. -
root
root node of the ternary tree.
-
-
Constructor Details
-
TernaryTree
public TernaryTree()Creates an empty case-sensitive ternary tree. -
TernaryTree
public TernaryTree(boolean caseSensitive) Creates an empty ternary tree with the given case sensitivity.- Parameters:
caseSensitive- whether this ternary tree should be case-sensitive.
-
-
Method Details
-
insert
Inserts the supplied word into this tree.- Parameters:
word- to insert
-
insert
Inserts the supplied array of words into this tree.- Parameters:
words- to insert
-
search
Returns whether the supplied word has been inserted into this ternary tree.- Parameters:
word- to search for- Returns:
- whether the word was found
-
partialSearch
Returns 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 performed.NOTE This method is not supported for case-insensitive ternary trees. Since the tree is built without regard to case any words returned from the tree may or may not match the case of the supplied word.
- Parameters:
word- to search for- Returns:
- array of matching words
- Throws:
UnsupportedOperationException- if this is a case-insensitive ternary tree
-
nearSearch
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 performed.NOTE This method is not supported for case-insensitive ternary trees. Since the tree is built without regard to case any words returned from the tree may or may not match the case of the supplied word.
- Parameters:
word- to search fordistance- for valid match- Returns:
- array of matching words
- Throws:
UnsupportedOperationException- if this is a case-insensitive ternary tree
-
getWords
Returns a list of all the words in this ternary tree. This is a very expensive operation, every node in the tree is traversed. The returned list cannot be modified.- Returns:
- unmodifiable list of words
-
print
Prints an ASCII representation of this ternary tree to the supplied writer. This is a very expensive operation, every node in the tree is traversed. The output produced is hard to read, but it should give an indication of whether your tree is balanced.- Parameters:
out- to print tofullPath- specifies whether each line should show the full path from root or only the suffix- Throws:
IOException- if an error occurs
-
print
Prints an ASCII representation of this ternary tree to the supplied writer. This is a very expensive operation, every node in the tree is traversed. The output produced is hard to read, but it should give an indication of whether your tree is balanced.- Parameters:
out- to print to- Throws:
IOException- if an error occurs
-
insertNode
Recursively inserts a word into the ternary tree one node at a time beginning at the supplied node.- Parameters:
node- to put character inword- to be insertedindex- of character in word- Returns:
- ternary node to insert
-
searchNode
Recursively searches for a word in the ternary tree one node at a time beginning at the supplied node.- Parameters:
node- to search inword- to search forindex- of character in word- Returns:
- whether the word was found
-
partialSearchNode
private List<CharSequence> partialSearchNode(TernaryNode node, List<CharSequence> matches, CharSequence match, CharSequence word, int index) Recursively searches for a partial word in the ternary tree one node at a time beginning at the supplied node.- Parameters:
node- to search inmatches- of partial matchesmatch- the current word being examinedword- to search forindex- of character in word- Returns:
- list of matches
-
nearSearchNode
private List<CharSequence> nearSearchNode(TernaryNode node, int distance, List<CharSequence> matches, CharSequence match, CharSequence word, int index) Recursively searches for a near match word in the ternary tree one node at a time beginning at the supplied node.- Parameters:
node- to search indistance- of a valid match, must be > 0matches- list of near matchesmatch- the current word being examinedword- to search forindex- of character in word- Returns:
- list of matches
-
traverseNode
Recursively traverses every node in the ternary tree 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.- Parameters:
node- to begin traversings- string of words found at the supplied nodewords- which will be returned (recursive function)- Returns:
- list of strings containing all words from the supplied node
-
printNode
private void printNode(TernaryNode node, String s, int depth, boolean fullPath, StringBuilder buffer) Recursively traverses every node in the ternary tree rooted at the supplied node. The result is an ASCII string representation of the tree rooted at the supplied node.- Parameters:
node- to begin traversings- the string representation of the current chain of equal kid nodesdepth- depth of the current chain of nodesfullPath- specifies whether each line should show the full path from root or only the suffixbuffer- the buffer to which the output is printed
-
getNodeStats
private Map<Integer,Integer> getNodeStats(TernaryNode node, int depth, Map<Integer, Integer> histogram) Returns a histogram of how many words end at each depth.- Parameters:
node- the node at the root of the tree to countdepth- the depth of the node from roothistogram- the depth count histogram to update- Returns:
- a histogram of how many words end at each depth
-
getNodeStats
Returns a histogram of how many words end at each depth.- Returns:
- a histogram of how many words end at each depth
-