A basic implementation of trie class to store only lower-case strings. You can extend the implementation to all the ASCII characters by changing the value of @ ALPHABETS to 128.
Definition at line 16 of file trie_modern.cpp.
std::shared_ptr< TrieNode > Trie::removeWordHelper |
( |
const std::string & | word, |
|
|
std::shared_ptr< TrieNode > | curr, |
|
|
size_t | index ) |
|
inlineprivate |
A recursive helper function to remove a word from the trie. First, it recursively traverses to the location of last character of word in the trie. However, if the word is not found, the function returns a runtime error. Upon successfully reaching the last character of word in trie, if sets the isEndOfWord to false and deletes the node if and only if it has no children, else it returns the current node.
- Parameters
-
word | is the string which needs to be removed from trie. |
curr | is the current node we are at. |
index | is the index of the @word we are at. |
- Returns
- if current node has childern, it returns @ curr, else it returns nullptr.
- Exceptions
-
a | runtime error in case @ word is not found in the trie. |
Definition at line 64 of file trie_modern.cpp.
66 {
67 if (word.size() == index) {
68 if (curr->isEndOfWord) {
69 curr->isEndOfWord = false;
70 }
72 return curr;
73 }
74 return nullptr;
75 }
76
77 size_t idx = word[index] - 'a';
78
79
80
81 if (!curr->character[idx]) {
82 throw std::runtime_error(std::move(std::string("Word not found.")));
83 }
84
85 curr->character[idx] =
87
88
89
90
91
92
93
94
96 return curr;
97 }
98 return nullptr;
99 }
static bool hasChildren(std::shared_ptr< TrieNode > node)