Trie class, implementation of trie using hashmap in each trie node for all the characters of char16_t(UTF-16)type with methods to insert, delete, search, start with and to recommend words based on a given prefix.
More...
|
| Trie ()=default |
| < Constructor
|
|
void | insert (const std::string &word) |
| insert the string into the trie
|
|
bool | search (const std::string &word) |
| search a word/string inside the trie
|
|
bool | startwith (const std::string &prefix) |
| search a word/string that starts with a given prefix
|
|
void | delete_word (std::string word) |
| delete a word/string from a trie
|
|
std::vector< std::string > | get_all_words (std::vector< std::string > results, const std::shared_ptr< Node > &element, std::string prefix) |
| helper function to predict/recommend words that starts with a given prefix from the end of prefix's node iterate through all the child nodes by recursively appending all the possible words below the trie
|
|
std::vector< std::string > | predict_words (const std::string &prefix) |
| predict/recommend a word that starts with a given prefix
|
|
Trie class, implementation of trie using hashmap in each trie node for all the characters of char16_t(UTF-16)type with methods to insert, delete, search, start with and to recommend words based on a given prefix.
Definition at line 39 of file trie_using_hashmap.cpp.
◆ delete_word()
void data_structures::trie_using_hashmap::Trie::delete_word |
( |
std::string | word | ) |
|
|
inline |
delete a word/string from a trie
- Parameters
-
word | string to delete from trie |
Definition at line 122 of file trie_using_hashmap.cpp.
122 {
124 std::stack<std::shared_ptr<Node>> nodes;
125 int cnt = 0;
126 for (char ch : word) {
127 if (curr->children.find(ch) == curr->children.end()) {
128 return;
129 }
130 if (curr->word_end) {
131 cnt++;
132 }
133
134 nodes.push(curr->children[ch]);
135 curr = curr->children[ch];
136 }
137
138
139 if (nodes.top()->word_end) {
140 nodes.top()->word_end = false;
141 }
142
143
144 while (!(nodes.top()->word_end) && nodes.top()->children.empty()) {
145 nodes.pop();
146 nodes.top()->children.erase(word.back());
147 word.pop_back();
148 }
149 }
std::shared_ptr< Node > root_node
declaring root node of trie
◆ get_all_words()
std::vector< std::string > data_structures::trie_using_hashmap::Trie::get_all_words |
( |
std::vector< std::string > | results, |
|
|
const std::shared_ptr< Node > & | element, |
|
|
std::string | prefix ) |
|
inline |
helper function to predict/recommend words that starts with a given prefix from the end of prefix's node iterate through all the child nodes by recursively appending all the possible words below the trie
- Parameters
-
prefix | string to recommend the words |
element | node at the end of the given prefix |
results | list to store the all possible words |
- Returns
- list of recommended words
Definition at line 160 of file trie_using_hashmap.cpp.
162 {
164 results.push_back(prefix);
165 }
167 return results;
168 }
169 for (
auto const& x : element->
children) {
170 std::string key = "";
171 key = x.first;
172 prefix += key;
173
174 results =
176
177 prefix.pop_back();
178 }
179
180 return results;
181 }
std::vector< std::string > get_all_words(std::vector< std::string > results, const std::shared_ptr< Node > &element, std::string prefix)
helper function to predict/recommend words that starts with a given prefix from the end of prefix's n...
std::unordered_map< char16_t, std::shared_ptr< Node > > children
bool word_end
boolean variable to represent the node end
◆ insert()
void data_structures::trie_using_hashmap::Trie::insert |
( |
const std::string & | word | ) |
|
|
inline |
insert the string into the trie
- Parameters
-
word | string to insert in the trie |
Definition at line 62 of file trie_using_hashmap.cpp.
62 {
64 for (char ch : word) {
65 if (curr->children.find(ch) == curr->children.end()) {
66 curr->children[ch] = std::make_shared<Node>();
67 }
68 curr = curr->children[ch];
69 }
70
71 if (!curr->word_end && curr !=
root_node) {
72 curr->word_end = true;
73 }
74 }
◆ predict_words()
std::vector< std::string > data_structures::trie_using_hashmap::Trie::predict_words |
( |
const std::string & | prefix | ) |
|
|
inline |
predict/recommend a word that starts with a given prefix
- Parameters
-
prefix | string to search for |
- Returns
- list of recommended words
< iteratively and recursively get the recommended words
Definition at line 188 of file trie_using_hashmap.cpp.
188 {
189 std::vector<std::string>
result;
191
192
193 for (char ch : prefix) {
194 if (curr->children.find(ch) == curr->children.end()) {
196 }
197
198 curr = curr->children[ch];
199 }
200
201
202 if (curr->word_end && curr->children.empty()) {
205 }
206
208 result, curr,
209 prefix);
210
212 }
uint64_t result(uint64_t n)
◆ search()
bool data_structures::trie_using_hashmap::Trie::search |
( |
const std::string & | word | ) |
|
|
inline |
search a word/string inside the trie
- Parameters
-
- Returns
true
if found
-
false
if not found
Definition at line 82 of file trie_using_hashmap.cpp.
82 {
84 for (char ch : word) {
85 if (curr->children.find(ch) == curr->children.end()) {
86 return false;
87 }
88 curr = curr->children[ch];
89 if (!curr) {
90 return false;
91 }
92 }
93
94 if (curr->word_end) {
95 return true;
96 } else {
97 return false;
98 }
99 }
◆ startwith()
bool data_structures::trie_using_hashmap::Trie::startwith |
( |
const std::string & | prefix | ) |
|
|
inline |
search a word/string that starts with a given prefix
- Parameters
-
prefix | string to search for |
- Returns
true
if found
-
false
if not found
Definition at line 107 of file trie_using_hashmap.cpp.
107 {
109 for (char ch : prefix) {
110 if (curr->children.find(ch) == curr->children.end()) {
111 return false;
112 }
113 curr = curr->children[ch];
114 }
115 return true;
116 }
◆ root_node
std::shared_ptr<Node> data_structures::trie_using_hashmap::Trie::root_node |
|
private |
Initial value:=
std::make_shared<Node>()
declaring root node of trie
Definition at line 51 of file trie_using_hashmap.cpp.
The documentation for this class was generated from the following file: