TheAlgorithms/C++ 1.0.0
All the 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::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
 

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.

Definition at line 39 of file trie_using_hashmap.cpp.

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

Definition at line 122 of file trie_using_hashmap.cpp.

122 {
123 std::shared_ptr<Node> curr = root_node;
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 // 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 }
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
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

Definition at line 160 of file trie_using_hashmap.cpp.

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...
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
wordstring to insert in the trie

Definition at line 62 of file trie_using_hashmap.cpp.

62 {
63 std::shared_ptr<Node> curr = root_node;
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

Definition at line 188 of file trie_using_hashmap.cpp.

188 {
189 std::vector<std::string> result;
190 std::shared_ptr<Node> curr = root_node;
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);
210
211 return result;
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
wordstring to search for
Returns
true if found
false if not found

Definition at line 82 of file trie_using_hashmap.cpp.

82 {
83 std::shared_ptr<Node> curr = root_node;
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

Definition at line 107 of file trie_using_hashmap.cpp.

107 {
108 std::shared_ptr<Node> curr = root_node;
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

Definition at line 51 of file trie_using_hashmap.cpp.


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