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
Nested ClassesModifier and TypeClassDescriptionprotected static classNode structure for this tree implementation. -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionintThe number of complete words in this trie.booleanChecks if a word exists in this trie.protected booleandelete(PrefixTrieImpl.PrefixTrieNode n, String word, int i) Internal protected delete for this tree implementation.booleanDeletes a word from the trie.protected PrefixTrieImpl.PrefixTrieNodegetRoot()Get the root of this tree.booleanInserts a word into the trie.protected PrefixTrieImpl.PrefixTrieNodeinsert(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.booleanChecks if prefix is present in this trie.A detailed string representation of the structure of this trie.protected booleansearch(PrefixTrieImpl.PrefixTrieNode n, String needle, int i, Collection<String> result, int resultLimit, Predicate<PrefixTrieImpl.PrefixTrieNode> resultTest, StringBuilder currentChars) Searching method for this trie.protected booleancompleteWordsdecrementing condition.intsize()The number of nodes in this trie.startsWith(String prefix, int limit) Try to find strings which start with prefix.toString()protected voidvisitWordNode(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:PrefixTrieTry to find strings which start with prefix. Matching strings are always in lexicographical order.- Specified by:
startsWithin interfacePrefixTrie- Parameters:
prefix- to look forlimit- maximum number of found strings- Returns:
- collection of words with the same prefix
-
isPrefix
Description copied from interface:PrefixTrieChecks if prefix is present in this trie.- Specified by:
isPrefixin interfacePrefixTrie- Parameters:
substring- to check- Returns:
- true if prefix exists
-
insert
Description copied from interface:PrefixTrieInserts a word into the trie. Duplicate words will be ignored.- Specified by:
insertin interfacePrefixTrie- Parameters:
string- word to be inserted- Returns:
- true if the word was inserted false if it already existed
-
delete
Description copied from interface:PrefixTrieDeletes 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:
deletein 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:PrefixTrieThe number of nodes in this trie.- Specified by:
sizein interfacePrefixTrie- Returns:
- number of nodes
-
completeWords
public int completeWords()Description copied from interface:PrefixTrieThe number of complete words in this trie.- Specified by:
completeWordsin interfacePrefixTrie- Returns:
- number of complete words
-
toString
-
contains
Description copied from interface:PrefixTrieChecks if a word exists in this trie.- Specified by:
containsin interfacePrefixTrie- Parameters:
string- to check- Returns:
- true if word is in this trie
-
prettyString
Description copied from interface:PrefixTrieA detailed string representation of the structure of this trie.- Specified by:
prettyStringin 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
completeWordsdecrementing condition. Can be overridden in extensions.- Parameters:
n- word node- Returns:
- true if
completeWordsshould be decremented
-