39 static constexpr uint8_t ENGLISH_ALPHABET_SIZE = 26;
41 std::vector<Tnode *> english;
51 english.resize(ENGLISH_ALPHABET_SIZE,
nullptr);
57 english =
node.english;
58 endOfWord =
node.endOfWord;
59 frequency =
node.frequency;
73 return ENGLISH_ALPHABET_SIZE -
74 std::count(
node->english.begin(),
node->english.end(),
nullptr);
78 void Insert(
const std::string &entry);
79 void Delete(std::string entry);
86 Tnode *new_root,
const std::string &prefix,
87 std::priority_queue<std::pair<int, std::string> > *suggestions);
90 std::priority_queue<std::pair<int, std::string> > *suggestions);
95 for (i = 0; i < ENGLISH_ALPHABET_SIZE; i++) {
108 Tnode *cur_pos =
this;
109 int letter_index = 0;
111 for (
auto &i : entry) {
113 letter_index = tolower(i) - 97;
117 if (cur_pos->english[letter_index] ==
nullptr) {
118 cur_pos->english[letter_index] =
new Tnode();
121 cur_pos = cur_pos->english[letter_index];
124 cur_pos->endOfWord =
true;
139 if (delete_string.size() == remove_index) {
140 int letter_index = tolower(delete_string[remove_index]) - 97;
142 DeleteFrom(delete_from->english[letter_index], delete_string,
154 Tnode *cur_pos =
this,
156 int letter_index = 0, delete_from_index = 0, i = 0, n = entry.size();
158 for (i = 0; i < n; i++) {
160 letter_index = tolower(entry[i]) - 97;
163 if (cur_pos->english[letter_index] ==
nullptr) {
164 std::cout <<
"Entry not Found" << std::endl;
171 delete_from = cur_pos;
173 delete_from_index = i - 1;
178 cur_pos = cur_pos->english[letter_index];
183 if (!cur_pos->endOfWord) {
184 std::cout <<
"Entry not Found" << std::endl;
191 cur_pos->endOfWord =
false;
192 cur_pos->frequency = 0;
197 letter_index = tolower(entry[delete_from_index + 1]) - 97;
199 cur_pos = delete_from->english[letter_index];
201 delete_from->english[letter_index] =
nullptr;
205 if (n > delete_from_index + 2) {
206 DeleteFrom(cur_pos, entry, delete_from_index + 2);
221 Tnode *cur_pos =
this;
222 int letter_index = 0;
224 for (
auto &i : key) {
225 letter_index = tolower(i) - 97;
227 if (cur_pos->english[letter_index] ==
nullptr) {
230 cur_pos = cur_pos->english[letter_index];
234 if (cur_pos->endOfWord) {
235 (cur_pos->frequency)++;
253 for (i = 0; i < ENGLISH_ALPHABET_SIZE; i++) {
254 if (new_root->english[i] !=
nullptr) {
257 if (new_root->english[i]->endOfWord) {
258 std::cout << prefix + char(i + 97) << std::endl;
275 Tnode *cur_pos =
nullptr, *prev_pos =
nullptr;
276 cur_pos = prev_pos =
this;
277 int letter_index = 0;
281 for (
auto &i : key) {
282 letter_index = tolower(i) - 97;
288 if (cur_pos->english[letter_index] ==
nullptr) {
290 std::cout <<
"- - - - - - - - - - - - - - - - - - - - - - - - - - "
295 prefix += char(tolower(i));
296 cur_pos = cur_pos->english[letter_index];
299 if (cur_pos->endOfWord) {
300 std::cout << key << std::endl;
301 (cur_pos->frequency)++;
309 std::cout <<
"- - - - - - - - - - - - - - - - - - - - - - - - - - "
321 std::priority_queue<std::pair<int, std::string> > *suggestions) {
323 int n = suggestions->size(), Top = 0;
326 std::cout << suggestions->top().second << std::endl;
341 Tnode *new_root,
const std::string &prefix,
342 std::priority_queue<std::pair<int, std::string> > *suggestions) {
344 for (i = 0; i < ENGLISH_ALPHABET_SIZE; i++) {
345 if (new_root->english[i] !=
nullptr) {
348 if (new_root->english[i]->endOfWord) {
349 suggestions->push(std::make_pair(
350 new_root->english[i]->frequency, prefix +
char(i + 97)));
369 Tnode *cur_pos =
nullptr, *prev_pos =
nullptr;
370 cur_pos = prev_pos =
this;
371 int letter_index = 0;
374 std::priority_queue<std::pair<int, std::string> >
378 std::priority_queue<std::pair<int, std::string> > *Suggestions =
381 for (
auto &i : key) {
382 letter_index = tolower(i) - 97;
388 if (cur_pos->english[letter_index] ==
nullptr) {
392 std::cout <<
"- - - - - - - - - - - - - - - - - - - - - - - - - - "
397 prefix += char(tolower(i));
398 cur_pos = cur_pos->english[letter_index];
401 if (cur_pos->endOfWord) {
402 (cur_pos->frequency)++;
403 std::cout << key << std::endl;
414 std::cout <<
"- - - - - - - - - - - - - - - - - - - - - - - - - - "
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"};
432 for (
auto &i : inputs) {
436 assert(root->SearchPresence(
"vvvv"));
437 std::cout << root->SearchPresence(
"vvvv") << std::endl;
439 root->Delete(
"vvvv");
441 assert(!root->SearchPresence(
"vvvv"));
442 std::cout << root->SearchPresence(
"vvvv") << std::endl;
444 std::cout << root->SearchPresence(
"tutu") << std::endl;
445 root->SearchSuggestions(
"tutu");
446 std::cout << root->SearchPresence(
"tutu") << std::endl;
448 root->SearchSuggestions(
"tutuv");
449 std::cout << root->SearchPresence(
"tutuv") << std::endl;
451 root->SearchSuggestions(
"tutuvs");
453 root->SearchFreqSuggestions(
456 root->SearchSuggestions(
466int main(
int argc,
char const *argv[]) {
Class defining the structure of trie node and containing the methods to perform operations on them.
void SuggestAutocomplete(Tnode *new_root, const std::string &prefix)
Recursive function to suggest all the entries of trie which have a given common prefix.
void SearchSuggestions(const std::string &key)
Lists out all the words in trie with the longest prefix of the search key that is present in the trie...
bool SearchPresence(const std::string &key)
Function to check a word's presence in the trie (Basic)
void SearchFreqSuggestions(const std::string &key)
Lists out the most frequent words in trie with the longest prefix of the search key that is present i...
void Insert(const std::string &entry)
Function to insert a word in the trie.
void SuggestFreqAutocomplete(Tnode *new_root, const std::string &prefix, std::priority_queue< std::pair< int, std::string > > *suggestions)
Recursive function to suggest most frequently searched entries of trie which have a given common pref...
void SelectionTop_3(std::priority_queue< std::pair< int, std::string > > *suggestions)
Function to display the 3 suggestions with highest frequency of search hits.
void DeleteFrom(Tnode *delete_from, std::string delete_string, int remove_index)
Function recursively deletes the substring character by character iterating through the string to be ...
void Delete(std::string entry)
Function to verify presence and hence delete an entry from the trie.
uint8_t numberOfChildren(Tnode *node)
Function to count the number of children a node in the trie has.
Functions for Trie datastructure implementation.
static void test()
Function to test a simple search before and after deleting an entry. And to test out the multiple var...