Algorithms_in_C++ 1.0.0
Set of algorithms implemented in C++.
Loading...
Searching...
No Matches
data_structures::trie_using_hashmap::Trie Class Reference

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...

Collaboration diagram for data_structures::trie_using_hashmap::Trie:
[legend]

Classes

struct  Node
 struct representing a trie node. More...
 

Public Member Functions

 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::stringget_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::stringpredict_words (const std::string &prefix)
 predict/recommend a word that starts with a given prefix
 

Private Attributes

std::shared_ptr< Noderoot_node
 declaring root node of trie
 

Detailed Description

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.

Member Function Documentation

◆ delete_word()

void data_structures::trie_using_hashmap::Trie::delete_word ( std::string word)
inline

delete a word/string from a trie

Parameters
wordstring to delete from trie
122 {
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 // Delete only when it's a word, and it has children after
138 // or prefix in the line
139 if (nodes.top()->word_end) {
140 nodes.top()->word_end = false;
141 }
142 // Delete only when it has no children after
143 // and also no prefix in the line
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 }
T back(T... args)
std::shared_ptr< Node > root_node
declaring root node of trie
Definition trie_using_hashmap.cpp:51
T pop_back(T... args)
T pop(T... args)
T push(T... args)
T top(T... args)
Here is the call graph for this function:

◆ 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
prefixstring to recommend the words
elementnode at the end of the given prefix
resultslist to store the all possible words
Returns
list of recommended words
162 {
163 if (element->word_end) {
164 results.push_back(prefix);
165 }
166 if (element->children.empty()) {
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 =
175 get_all_words(results, element->children[x.first], prefix);
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...
Definition trie_using_hashmap.cpp:160
T push_back(T... args)
std::unordered_map< char16_t, std::shared_ptr< Node > > children
Definition trie_using_hashmap.cpp:46
bool word_end
boolean variable to represent the node end
Definition trie_using_hashmap.cpp:48
Here is the call graph for this function:

◆ insert()

void data_structures::trie_using_hashmap::Trie::insert ( const std::string & word)
inline

insert the string into the trie

Parameters
wordstring to insert in the trie
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
prefixstring to search for
Returns
list of recommended words

< iteratively and recursively get the recommended words

188 {
191 // traversing until the end of the given prefix in trie
192
193 for (char ch : prefix) {
194 if (curr->children.find(ch) == curr->children.end()) {
195 return result;
196 }
197
198 curr = curr->children[ch];
199 }
200
201 // if the given prefix is the only word without children
202 if (curr->word_end && curr->children.empty()) {
203 result.push_back(prefix);
204 return result;
205 }
206
208 result, curr,
209 prefix); ///< iteratively and recursively get the recommended words
210
211 return result;
212 }
uint64_t result(uint64_t n)
Definition fibonacci_sum.cpp:76
Here is the call graph for this function:

◆ search()

bool data_structures::trie_using_hashmap::Trie::search ( const std::string & word)
inline

search a word/string inside the trie

Parameters
wordstring to search for
Returns
true if found
false if not found
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
prefixstring to search for
Returns
true if found
false if not found
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 }

Member Data Documentation

◆ 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


The documentation for this class was generated from the following file: