Class ReversePrefixTrie
java.lang.Object
com.abstractkamen.datastructures.impl.trees.search.PrefixTrieImpl
com.abstractkamen.datastructures.impl.trees.search.ReversePrefixTrie
- All Implemented Interfaces:
PrefixTrie
,SuffixTrie
This implementation uses the same insert/delete/search algorithm as the
PrefixTrieImpl
. In order to implement the
SuffixTrie
it reverses the input string. The cost of insert/delete/search is double that of the PrefixTrieImpl
, but is
still O(m) where m is the length of the input string.-
Nested Class Summary
Nested classes/interfaces inherited from class com.abstractkamen.datastructures.impl.trees.search.PrefixTrieImpl
PrefixTrieImpl.PrefixTrieNode
-
Constructor Summary
-
Method Summary
Modifier and TypeMethodDescriptionboolean
Deletes a word from the trie.Try to find strings which end with suffix.boolean
Inserts a word into the trie.boolean
Checks if prefix is present in this trie.boolean
Checks if suffix is present in this trie.protected boolean
PrefixTrieImpl.completeWords
decrementing condition.startsWith
(String prefix, int limit) Try to find strings which start with prefix.toString()
protected void
visitWordNode
(PrefixTrieImpl.PrefixTrieNode node, StringBuilder visitor, StringBuilder currentChars) Visit a node which we know is a word.Methods inherited from class com.abstractkamen.datastructures.impl.trees.search.PrefixTrieImpl
completeWords, contains, delete, getRoot, insert, prettyString, search, size
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
Methods inherited from interface com.abstractkamen.datastructures.api.trees.search.PrefixTrie
completeWords, contains, prettyString, size
-
Constructor Details
-
ReversePrefixTrie
public ReversePrefixTrie()
-
-
Method Details
-
shouldDecrementWordOnDelete
Description copied from class:PrefixTrieImpl
PrefixTrieImpl.completeWords
decrementing condition. Can be overridden in extensions.- Overrides:
shouldDecrementWordOnDelete
in classPrefixTrieImpl
- Parameters:
n
- word node- Returns:
- true if
PrefixTrieImpl.completeWords
should be decremented
-
visitWordNode
protected void visitWordNode(PrefixTrieImpl.PrefixTrieNode node, StringBuilder visitor, StringBuilder currentChars) Description copied from class:PrefixTrieImpl
Visit a node which we know is a word.- Overrides:
visitWordNode
in classPrefixTrieImpl
- Parameters:
node
- word nodevisitor
- sb visitorcurrentChars
- word so far
-
insert
Description copied from interface:PrefixTrie
Inserts a word into the trie. Duplicate words will be ignored.- Specified by:
insert
in interfacePrefixTrie
- Overrides:
insert
in classPrefixTrieImpl
- Parameters:
string
- word to be inserted- Returns:
- true if the word was inserted false if it already existed
-
delete
Description copied from interface:PrefixTrie
Deletes a word from the trie. The number of complete words will be decremented by one and the size of the trie will shrink by the number of unique characters and their placements in the deleted word if they aren't used by a different word.- Specified by:
delete
in interfacePrefixTrie
- Overrides:
delete
in classPrefixTrieImpl
- Parameters:
string
- to be deleted- Returns:
- true if word is successfully deleted false if word was not in the trie
-
endsWith
Description copied from interface:SuffixTrie
Try to find strings which end with suffix. Matching strings are always in arbitrary order.- Specified by:
endsWith
in interfaceSuffixTrie
- Parameters:
suffix
- to look forlimit
- maximum number of found strings- Returns:
- collection of words with the same suffix
-
isSuffix
Description copied from interface:SuffixTrie
Checks if suffix is present in this trie.- Specified by:
isSuffix
in interfaceSuffixTrie
- Parameters:
suffix
- to check- Returns:
- true if suffix exists
-
isPrefix
Description copied from interface:PrefixTrie
Checks if prefix is present in this trie.- Specified by:
isPrefix
in interfacePrefixTrie
- Overrides:
isPrefix
in classPrefixTrieImpl
- Parameters:
prefix
- to check- Returns:
- true if prefix exists
-
startsWith
Description copied from interface:PrefixTrie
Try to find strings which start with prefix. Matching strings are always in lexicographical order.- Specified by:
startsWith
in interfacePrefixTrie
- Overrides:
startsWith
in classPrefixTrieImpl
- Parameters:
prefix
- to look forlimit
- maximum number of found strings- Returns:
- collection of words with the same prefix
-
toString
- Overrides:
toString
in classPrefixTrieImpl
-