TheAlgorithms/C++ 1.0.0
All the algorithms implemented in C++
Loading...
Searching...
No Matches
trie_multiple_search.cpp File Reference

Trie datastructure with search variants More...

#include <algorithm>
#include <cassert>
#include <cctype>
#include <cstdint>
#include <cstring>
#include <iostream>
#include <queue>
Include dependency graph for trie_multiple_search.cpp:

Go to the source code of this file.

Classes

class  operations_on_datastructures::trie_operations::Tnode
 Class defining the structure of trie node and containing the methods to perform operations on them. More...

Namespaces

namespace  operations_on_datastructures
 for std::vector
namespace  trie_operations
 Functions for Trie datastructure implementation.

Functions

static void test ()
 Function to test a simple search before and after deleting an entry. And to test out the multiple variants of search.
int main ()
 Main function.

Detailed Description

Trie datastructure with search variants

This provides multiple variants of search functions on a trie structure utilizing STL. The trie is valid for only English alphabets.

Author
Ghanashyam

Definition in file trie_multiple_search.cpp.

Function Documentation

◆ main()

int main ( void )

Main function.

Returns
0 on exit

Definition at line 464 of file trie_multiple_search.cpp.

464 {
465 test(); // run self-test implementations
466 return 0;
467}
static void test()
Function to test a simple search before and after deleting an entry. And to test out the multiple var...

◆ test()

void test ( )
static

Function to test a simple search before and after deleting an entry. And to test out the multiple variants of search.

Definition at line 425 of file trie_multiple_search.cpp.

425 {
427 std::vector<std::string> inputs = {
428 "abcde", "sss", "ssss", "ssst", "sssu", "sssv",
429 "sst", "ssts", "sstt", "sstu", "tutu", "tutuv",
430 "tutuu", "tutuvs", "tutus", "tvst", "tvsu", "vvvv"};
431
432 for (auto &i : inputs) {
433 root->Insert(i);
434 }
435 // Search an existing entry
436 assert(root->SearchPresence("vvvv"));
437 std::cout << root->SearchPresence("vvvv") << std::endl;
438 // Delete it
439 root->Delete("vvvv");
440 // Search for the entry again
441 assert(!root->SearchPresence("vvvv"));
442 std::cout << root->SearchPresence("vvvv") << std::endl;
443
444 std::cout << root->SearchPresence("tutu") << std::endl;
445 root->SearchSuggestions("tutu");
446 std::cout << root->SearchPresence("tutu") << std::endl;
447
448 root->SearchSuggestions("tutuv");
449 std::cout << root->SearchPresence("tutuv") << std::endl;
450
451 root->SearchSuggestions("tutuvs");
452
453 root->SearchFreqSuggestions(
454 "tu"); // The top 3 frequent entries with prefix tu are tutu, tutuv &
455 // tutuvs respectively
456 root->SearchSuggestions(
457 ""); // Empty search to list all the entries in the trie
458}
Class defining the structure of trie node and containing the methods to perform operations on them.