std::unordered_map<char16_t, std::shared_ptr<Node>>
52std::make_shared<Node>();
62 void insert(
conststd::string& word) {
64 for(
charch : word) {
65 if(curr->children.find(ch) == curr->children.end()) {
66curr->children[ch] = std::make_shared<Node>();
68curr = curr->children[ch];
71 if(!curr->word_end && curr !=
root_node) {
72curr->word_end =
true;
82 bool search(
conststd::string& word) {
84 for(
charch : word) {
85 if(curr->children.find(ch) == curr->children.end()) {
88curr = curr->children[ch];
109 for(
charch : prefix) {
110 if(curr->children.find(ch) == curr->children.end()) {
113curr = curr->children[ch];
124std::stack<std::shared_ptr<Node>> nodes;
126 for(
charch : word) {
127 if(curr->children.find(ch) == curr->children.end()) {
130 if(curr->word_end) {
134nodes.push(curr->children[ch]);
135curr = curr->children[ch];
139 if(nodes.top()->word_end) {
140nodes.top()->word_end =
false;
144 while(!(nodes.top()->word_end) && nodes.top()->children.empty()) {
146nodes.top()->children.erase(word.back());
161 conststd::shared_ptr<Node>& element,
162std::string prefix) {
163 if(element->word_end) {
164results.push_back(prefix);
166 if(element->children.empty()) {
169 for(
auto const& x : element->children) {
170std::string key =
"";
189std::vector<std::string> result;
193 for(
charch : prefix) {
194 if(curr->children.find(ch) == curr->children.end()) {
198curr = curr->children[ch];
202 if(curr->word_end && curr->children.empty()) {
203result.push_back(prefix);
226obj.
insert(
"abscond");
232obj.
insert(
"approach");
238assert(!obj.
search(
"appy"));
239std::cout <<
"appy is not a word in trie"<< std::endl;
241assert(!obj.
search(
"car"));
242std::cout <<
"car is not a word in trie"<< std::endl;
243assert(obj.
search(
"app"));
244assert(obj.
search(
"apple"));
245assert(obj.
search(
"apples"));
246assert(obj.
search(
"apps"));
247assert(obj.
search(
"apen"));
248assert(obj.
search(
"approach"));
249assert(obj.
search(
"about"));
250assert(obj.
search(
"abscond"));
251assert(obj.
search(
"bus"));
252assert(obj.
search(
"buses"));
253assert(obj.
search(
"Bounce"));
254assert(obj.
search(
"Apple"));
256std::cout <<
"All the Inserted words are present in the trie"<< std::endl;
277std::cout <<
"All the tests passed for startwith method"<< std::endl;
281std::vector<std::string> pred_words = obj.
predict_words(
"a");
284std::cout << str << std::endl;
286assert(pred_words.size() == 8);
287std::cout <<
"Returned all words that start with prefix a "<< std::endl;
290 for(
conststd::string& str : pred_words) {
291std::cout << str << std::endl;
294assert(pred_words.size() == 5);
295std::cout <<
"Returned all words that start with prefix app "<< std::endl;
298 for(
conststd::string& str : pred_words) {
299std::cout << str << std::endl;
302assert(pred_words.size() == 1);
303std::cout <<
"Returned all words that start with prefix A "<< std::endl;
306 for(
conststd::string& str : pred_words) {
307std::cout << str << std::endl;
310assert(pred_words.size() == 2);
311std::cout <<
"Returned all words that start with prefix bu "<< std::endl;
316assert(!obj.
search(
"app"));
317std::cout <<
"word app is deleted sucessful"<< std::endl;
320 for(
conststd::string& str : pred_words) {
321std::cout << str << std::endl;
323assert(pred_words.size() == 4);
324std::cout <<
"app is deleted sucessful"<< std::endl;
332assert(pred_words.size() == 0);
333std::cout <<
"No word starts with prefix h in trie"<< std::endl;
335std::cout <<
"All tests passed"<< std::endl;
Trie class, implementation of trie using hashmap in each trie node for all the characters of char16_t...
Trie()=default
< Constructor
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::shared_ptr< Node > root_node
declaring root node of trie
void insert(const std::string &word)
insert the string into the trie
void delete_word(std::string word)
delete a word/string from a trie
bool search(const std::string &word)
search a word/string inside the trie
std::vector< std::string > predict_words(const std::string &prefix)
predict/recommend a word that starts with a given prefix
bool startwith(const std::string &prefix)
search a word/string that starts with a given prefix
Functions for Trie data structure using hashmap implementation.
struct representing a trie node.
std::unordered_map< char16_t, std::shared_ptr< Node > > children
bool word_end
boolean variable to represent the node end
static void test()
Self-test implementations.
int main()
Main function.
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4