Algorithms_in_C++  1.0.0
Set of algorithms implemented in C++.
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

 data_structures
 Data Structures algorithms.
 
 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
342  {
343  test(); // run self-test implementaions
344  return 0;
345 }
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
221  {
223  // Inserting data into trie using the insert
224  // method and testing it with search method
225  obj.insert("app");
226  obj.insert("abscond");
227  obj.insert("about");
228  obj.insert("apps");
229  obj.insert("apen");
230  obj.insert("apples");
231  obj.insert("apple");
232  obj.insert("approach");
233  obj.insert("bus");
234  obj.insert("buses");
235  obj.insert("Apple");
236  obj.insert("Bounce");
237 
238  assert(!obj.search("appy"));
239  std::cout << "appy is not a word in trie" << std::endl;
240 
241  assert(!obj.search("car"));
242  std::cout << "car is not a word in trie" << std::endl;
243  assert(obj.search("app"));
244  assert(obj.search("apple"));
245  assert(obj.search("apples"));
246  assert(obj.search("apps"));
247  assert(obj.search("apen"));
248  assert(obj.search("approach"));
249  assert(obj.search("about"));
250  assert(obj.search("abscond"));
251  assert(obj.search("bus"));
252  assert(obj.search("buses"));
253  assert(obj.search("Bounce"));
254  assert(obj.search("Apple"));
255 
256  std::cout << "All the Inserted words are present in the trie" << std::endl;
257 
258  // test for startwith prefix method
259  assert(!obj.startwith("approachs"));
260  assert(obj.startwith("approach"));
261  assert(obj.startwith("about"));
262  assert(!obj.startwith("appy"));
263  assert(obj.startwith("abscond"));
264  assert(obj.startwith("bus"));
265  assert(obj.startwith("buses"));
266  assert(obj.startwith("Bounce"));
267  assert(obj.startwith("Apple"));
268  assert(obj.startwith("abs"));
269  assert(obj.startwith("b"));
270  assert(obj.startwith("bus"));
271  assert(obj.startwith("Bo"));
272  assert(obj.startwith("A"));
273  assert(!obj.startwith("Ca"));
274 
275  assert(!obj.startwith("C"));
276 
277  std::cout << "All the tests passed for startwith method" << std::endl;
278 
279  // test for predict_words/recommendation of words based on a given prefix
280 
281  std::vector<std::string> pred_words = obj.predict_words("a");
282 
283  for (const std::string& str : obj.predict_words("a")) {
284  std::cout << str << std::endl;
285  }
286  assert(pred_words.size() == 8);
287  std::cout << "Returned all words that start with prefix a " << std::endl;
288  pred_words = obj.predict_words("app");
289 
290  for (const std::string& str : pred_words) {
291  std::cout << str << std::endl;
292  }
293 
294  assert(pred_words.size() == 5);
295  std::cout << "Returned all words that start with prefix app " << std::endl;
296  pred_words = obj.predict_words("A");
297 
298  for (const std::string& str : pred_words) {
299  std::cout << str << std::endl;
300  }
301 
302  assert(pred_words.size() == 1);
303  std::cout << "Returned all words that start with prefix A " << std::endl;
304  pred_words = obj.predict_words("bu");
305 
306  for (const std::string& str : pred_words) {
307  std::cout << str << std::endl;
308  }
309 
310  assert(pred_words.size() == 2);
311  std::cout << "Returned all words that start with prefix bu " << std::endl;
312 
313  // tests for delete method
314 
315  obj.delete_word("app");
316  assert(!obj.search("app"));
317  std::cout << "word app is deleted sucessful" << std::endl;
318 
319  pred_words = obj.predict_words("app");
320  for (const std::string& str : pred_words) {
321  std::cout << str << std::endl;
322  }
323  assert(pred_words.size() == 4);
324  std::cout << "app is deleted sucessful" << std::endl;
325 
326  // test case for Chinese language
327 
328  obj.insert("苹果");
329  assert(obj.startwith("苹"));
330  pred_words = obj.predict_words("h");
331 
332  assert(pred_words.size() == 0);
333  std::cout << "No word starts with prefix h in trie" << std::endl;
334 
335  std::cout << "All tests passed" << std::endl;
336 }
Trie class, implementation of trie using hashmap in each trie node for all the characters of char16_t...
Definition: trie_using_hashmap.cpp:39
void insert(const std::string &word)
insert the string into the trie
Definition: trie_using_hashmap.cpp:62
void delete_word(std::string word)
delete a word/string from a trie
Definition: trie_using_hashmap.cpp:122
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
bool search(const std::string &word)
search a word/string inside the trie
Definition: trie_using_hashmap.cpp:82
bool startwith(const std::string &prefix)
search a word/string that starts with a given prefix
Definition: trie_using_hashmap.cpp:107
T endl(T... args)
T size(T... args)
Here is the call graph for this function: