Class PrefixTrieImpl
java.lang.Object
com.abstractkamen.datastructures.impl.trees.search.PrefixTrieImpl
- All Implemented Interfaces:
PrefixTrie
- Direct Known Subclasses:
ReversePrefixTrie
Basic implementation of a prefix 'trie'. Each character of an inserted string is stored in a node in lexicographical order and each
node has children and a parent. All nodes have one common ancestor which is the
root
. A node is marked if the path from the
root
to it forms a word (a string inserted by insert(String)
).-
Nested Class Summary
Modifier and TypeClassDescriptionprotected static class
Node structure for this tree implementation. -
Constructor Summary
-
Method Summary
Modifier and TypeMethodDescriptionint
The number of complete words in this trie.boolean
Checks if a word exists in this trie.protected boolean
delete
(PrefixTrieImpl.PrefixTrieNode n, String word, int i) Internal protected delete for this tree implementation.boolean
Deletes a word from the trie.protected PrefixTrieImpl.PrefixTrieNode
getRoot()
Get the root of this tree.boolean
Inserts a word into the trie.protected PrefixTrieImpl.PrefixTrieNode
insert
(String word, PrefixTrieImpl.PrefixTrieNode root, int i, BiFunction<Integer, PrefixTrieImpl.PrefixTrieNode, PrefixTrieImpl.PrefixTrieNode> factory) Inserts a word character by character where first a character is lookup in the root and if it's missing a new node is created and appended to the root after which the algorithm is repeated recursively until we reach the end of the word.boolean
Checks if prefix is present in this trie.A detailed string representation of the structure of this trie.protected boolean
search
(PrefixTrieImpl.PrefixTrieNode n, String needle, int i, Collection<String> result, int resultLimit, Predicate<PrefixTrieImpl.PrefixTrieNode> resultTest, StringBuilder currentChars) Searching method for this trie.protected boolean
completeWords
decrementing condition.int
size()
The number of nodes in this trie.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.
-
Constructor Details
-
PrefixTrieImpl
public PrefixTrieImpl()Constructor
-
-
Method Details
-
getRoot
Get the root of this tree.- Returns:
- root of this tree which can never be null
-
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
- Parameters:
prefix
- to look forlimit
- maximum number of found strings- Returns:
- collection of words with the same prefix
-
isPrefix
Description copied from interface:PrefixTrie
Checks if prefix is present in this trie.- Specified by:
isPrefix
in interfacePrefixTrie
- Parameters:
substring
- to check- Returns:
- true if prefix exists
-
insert
Description copied from interface:PrefixTrie
Inserts a word into the trie. Duplicate words will be ignored.- Specified by:
insert
in interfacePrefixTrie
- 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
- Parameters:
string
- to be deleted- Returns:
- true if word is successfully deleted false if word was not in the trie
-
size
public int size()Description copied from interface:PrefixTrie
The number of nodes in this trie.- Specified by:
size
in interfacePrefixTrie
- Returns:
- number of nodes
-
completeWords
public int completeWords()Description copied from interface:PrefixTrie
The number of complete words in this trie.- Specified by:
completeWords
in interfacePrefixTrie
- Returns:
- number of complete words
-
toString
-
contains
Description copied from interface:PrefixTrie
Checks if a word exists in this trie.- Specified by:
contains
in interfacePrefixTrie
- Parameters:
string
- to check- Returns:
- true if word is in this trie
-
prettyString
Description copied from interface:PrefixTrie
A detailed string representation of the structure of this trie.- Specified by:
prettyString
in interfacePrefixTrie
- Returns:
- detailed string
-
visitWordNode
protected void visitWordNode(PrefixTrieImpl.PrefixTrieNode node, StringBuilder visitor, StringBuilder currentChars) Visit a node which we know is a word.- Parameters:
node
- word nodevisitor
- sb visitorcurrentChars
- word so far
-
insert
protected PrefixTrieImpl.PrefixTrieNode insert(String word, PrefixTrieImpl.PrefixTrieNode root, int i, BiFunction<Integer, PrefixTrieImpl.PrefixTrieNode, PrefixTrieImpl.PrefixTrieNode> factory) Inserts a word character by character where first a character is lookup in the root and if it's missing a new node is created and appended to the root after which the algorithm is repeated recursively until we reach the end of the word. The final result of the recursion will be the leaf node.- Parameters:
word
- input stingroot
- root nodei
- current character indexfactory
- node factory - useful in extensions- Returns:
- the 'inserted' node
-
search
protected boolean search(PrefixTrieImpl.PrefixTrieNode n, String needle, int i, Collection<String> result, int resultLimit, Predicate<PrefixTrieImpl.PrefixTrieNode> resultTest, StringBuilder currentChars) Searching method for this trie. Will walk through all characters in needle and find a possible path of nodes for them. If the path exists return true.- Parameters:
n
- current nodeneedle
- stringi
- offsetresult
- string storageresultLimit
- storage limitresultTest
- add to result collection if truecurrentChars
- word so far- Returns:
- true if a needle path exists
-
delete
Internal protected delete for this tree implementation.- Parameters:
n
- current nodeword
- word to deletei
- current index- Returns:
- true if word was deleted
-
shouldDecrementWordOnDelete
completeWords
decrementing condition. Can be overridden in extensions.- Parameters:
n
- word node- Returns:
- true if
completeWords
should be decremented
-