# Algorithms_in_C++: data_structures/trie_using_hashmap.cpp File Reference

Loading...
Searching...
No Matches
trie_using_hashmap.cpp File Reference

Implementation of Trie data structure using HashMap for different characters and method for predicting words based on prefix. More...

`#include <cassert>`
`#include <iostream>`
`#include <memory>`
`#include <stack>`
`#include <unordered_map>`
`#include <vector>`
Include dependency graph for trie_using_hashmap.cpp:

## Classes

class | data_structures::trie_using_hashmap::Trie
| 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...
struct | data_structures::trie_using_hashmap::Trie::Node
| struct representing a trie node. More...

## Namespaces

namespace | data_structures
| for IO operations
namespace | trie_using_hashmap
| Functions for Trie data structure using hashmap implementation.

## Functions

static void | test ()
| Self-test implementations. More...
int | main ()
| Main function. More...

## Detailed Description

Implementation of Trie data structure using HashMap for different characters and method for predicting words based on prefix.

Author
  Venkata Bharath

The Trie data structure is implemented using unordered map to use memory optimally, predict_words method is developed to recommend words based on a given prefix along with other methods insert, delete, search, startwith in trie.

See also
  trie_modern.cpp for difference

## Function Documentation

## main()

int main | ( | void |  | ) |

Main function.

Returns
  0 on exit

{
test(); // run self-test implementaions
return 0;
}
test
static void test()
Self-test implementations.
Definition: trie_using_hashmap.cpp:221
Here is the call graph for this function:

## test()

static void test | ( |  | ) |

static

Self-test implementations.

Returns
  void

{
data_structures::trie_using_hashmap::Trie obj;
// Inserting data into trie using the insert
// method and testing it with search method
obj.insert("app");
obj.insert("abscond");
obj.insert("about");
obj.insert("apps");
obj.insert("apen");
obj.insert("apples");
obj.insert("apple");
obj.insert("approach");
obj.insert("bus");
obj.insert("buses");
obj.insert("Apple");
obj.insert("Bounce");
assert(!obj.search("appy"));
std::cout << "appy is not a word in trie" << std::endl;
assert(!obj.search("car"));
std::cout << "car is not a word in trie" << std::endl;
assert(obj.search("app"));
assert(obj.search("apple"));
assert(obj.search("apples"));
assert(obj.search("apps"));
assert(obj.search("apen"));
assert(obj.search("approach"));
assert(obj.search("about"));
assert(obj.search("abscond"));
assert(obj.search("bus"));
assert(obj.search("buses"));
assert(obj.search("Bounce"));
assert(obj.search("Apple"));
std::cout << "All the Inserted words are present in the trie" << std::endl;
// test for startwith prefix method
assert(!obj.startwith("approachs"));
assert(obj.startwith("approach"));
assert(obj.startwith("about"));
assert(!obj.startwith("appy"));
assert(obj.startwith("abscond"));
assert(obj.startwith("bus"));
assert(obj.startwith("buses"));
assert(obj.startwith("Bounce"));
assert(obj.startwith("Apple"));
assert(obj.startwith("abs"));
assert(obj.startwith("b"));
assert(obj.startwith("bus"));
assert(obj.startwith("Bo"));
assert(obj.startwith("A"));
assert(!obj.startwith("Ca"));
assert(!obj.startwith("C"));
std::cout << "All the tests passed for startwith method" << std::endl;
// test for predict_words/recommendation of words based on a given prefix
std::vector<std::string> pred_words = obj.predict_words("a");
for (const std::string& str : obj.predict_words("a")) {
std::cout << str << std::endl;
}
assert(pred_words.size() == 8);
std::cout << "Returned all words that start with prefix a " << std::endl;
pred_words = obj.predict_words("app");
for (const std::string& str : pred_words) {
std::cout << str << std::endl;
}
assert(pred_words.size() == 5);
std::cout << "Returned all words that start with prefix app " << std::endl;
pred_words = obj.predict_words("A");
for (const std::string& str : pred_words) {
std::cout << str << std::endl;
}
assert(pred_words.size() == 1);
std::cout << "Returned all words that start with prefix A " << std::endl;
pred_words = obj.predict_words("bu");
for (const std::string& str : pred_words) {
std::cout << str << std::endl;
}
assert(pred_words.size() == 2);
std::cout << "Returned all words that start with prefix bu " << std::endl;
// tests for delete method
obj.delete_word("app");
assert(!obj.search("app"));
std::cout << "word app is deleted sucessful" << std::endl;
pred_words = obj.predict_words("app");
for (const std::string& str : pred_words) {
std::cout << str << std::endl;
}
assert(pred_words.size() == 4);
std::cout << "app is deleted sucessful" << std::endl;
// test case for Chinese language
obj.insert("苹果");
assert(obj.startwith("苹"));
pred_words = obj.predict_words("h");
assert(pred_words.size() == 0);
std::cout << "No word starts with prefix h in trie" << std::endl;
std::cout << "All tests passed" << std::endl;
}
std::cout
std::string
data_structures::trie_using_hashmap::Trie
Trie class, implementation of trie using hashmap in each trie node for all the characters of char16_t...
Definition: trie_using_hashmap.cpp:39
data_structures::trie_using_hashmap::Trie::insert
void insert(const std::string &word)
insert the string into the trie
Definition: trie_using_hashmap.cpp:62
data_structures::trie_using_hashmap::Trie::delete_word
void delete_word(std::string word)
delete a word/string from a trie
Definition: trie_using_hashmap.cpp:122
data_structures::trie_using_hashmap::Trie::search
bool search(const std::string &word)
search a word/string inside the trie
Definition: trie_using_hashmap.cpp:82
data_structures::trie_using_hashmap::Trie::predict_words
std::vector< std::string > predict_words(const std::string &prefix)
predict/recommend a word that starts with a given prefix
Definition: trie_using_hashmap.cpp:188
data_structures::trie_using_hashmap::Trie::startwith
bool startwith(const std::string &prefix)
search a word/string that starts with a given prefix
Definition: trie_using_hashmap.cpp:107
std::endl
T endl(T... args)
std::vector::size
T size(T... args)
std::vector
Here is the call graph for this function: