Algorithms_in_C++ 1.0.0
Set of algorithms implemented in C++.
Loading...
Searching...
No Matches
operations_on_datastructures::trie_operations::Tnode Class Reference

Class defining the structure of trie node and containing the methods to perform operations on them. More...

Collaboration diagram for operations_on_datastructures::trie_operations::Tnode:
[legend]

Public Member Functions

 Tnode (const Tnode &node)
 
Tnodeoperator= (const Tnode &node)=default
 
 Tnode (Tnode &&)=default
 
Tnodeoperator= (Tnode &&)=default
 
uint8_t numberOfChildren (Tnode *node)
 Function to count the number of children a node in the trie has.
 
void Insert (const std::string &entry)
 Function to insert a word in the trie.
 
void Delete (std::string entry)
 Function to verify presence and hence delete an entry from the trie.
 
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 deleted. It traverses till the end of word in a recursive fashion, from there it deletes characters one by one till it reaches back to the initial call.
 
bool SearchPresence (const std::string &key)
 Function to check a word's presence in the trie (Basic)
 
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. For example - if trie contains "abc", "abcde", "abcdefg", "abcddef" and if the search key is "abcdezz", then the longest common prefix is "abcde" and hence search results will be "abcde", "abcdefg".
 
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 prefix.
 
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 in the trie. For example - if trie contains "abc", "abcde", "abcdefg", "abcddef" and they have been previously searched for 3, 1, 2, 4 times respectively, if the search key is "ab", then the longest common prefix is "ab" and only the top 3 frequencies among the matches would be displayed viz. "abcddef", "abc", "abcdefg".
 
void SelectionTop_3 (std::priority_queue< std::pair< int, std::string > > *suggestions)
 Function to display the 3 suggestions with highest frequency of search hits.
 

Private Attributes

std::vector< Tnode * > english
 
bool endOfWord
 
uint32_t frequency
 

Static Private Attributes

static constexpr uint8_t ENGLISH_ALPHABET_SIZE = 26
 

Detailed Description

Class defining the structure of trie node and containing the methods to perform operations on them.

Constructor & Destructor Documentation

◆ Tnode() [1/2]

operations_on_datastructures::trie_operations::Tnode::Tnode ( )
inline
47 {
48 english.resize(ENGLISH_ALPHABET_SIZE, nullptr);
49 endOfWord = false;
50 frequency = 0;
51 }

◆ Tnode() [2/2]

operations_on_datastructures::trie_operations::Tnode::Tnode ( const Tnode & node)
inline
53 {
54 english = node.english;
55 endOfWord = node.endOfWord;
56 frequency = node.frequency;
57 }
Definition binary_search_tree.cpp:11

◆ ~Tnode()

operations_on_datastructures::trie_operations::Tnode::~Tnode ( )
inline
90 {
91 int i = 0;
92 for (i = 0; i < ENGLISH_ALPHABET_SIZE; i++) {
93 if (english[i]) {
94 delete english[i];
95 }
96 }
97 }

Member Function Documentation

◆ Delete()

void operations_on_datastructures::trie_operations::Tnode::Delete ( std::string entry)

Function to verify presence and hence delete an entry from the trie.

Parameters
entrystring entry to be deleted from the trie
150 {
151 Tnode *cur_pos = this,
152 *delete_from = this; // Current pointer pointing to root
153 int letter_index = 0, delete_from_index = 0, i = 0, n = entry.size();
154
155 for (i = 0; i < n; i++) {
156 // To ignore case
157 letter_index = tolower(entry[i]) - 97;
158
159 // Display error message when given entry is not present in the tree
160 if (cur_pos->english[letter_index] == nullptr) {
161 std::cout << "Entry not Found" << std::endl;
162 return;
163 }
164 // If the current node is end of word for the current prefix or if it
165 // has 2 or more branches It cannot be deleted while deleting the
166 // required entry.
167 if (numberOfChildren(cur_pos) > 1 || cur_pos->endOfWord) {
168 delete_from = cur_pos; // denotes the beginning of the shortest
169 // suffix that is allowed to be deleted
170 delete_from_index = i - 1; // Beginning index of the suffix
171 // corresponding to the 'entry'
172 }
173
174 // Traversing through the entry
175 cur_pos = cur_pos->english[letter_index];
176 }
177
178 // cur_pos now points to the last char of entry. Display message if that
179 // entry does not exist
180 if (!cur_pos->endOfWord) {
181 std::cout << "Entry not Found" << std::endl;
182 return;
183 }
184
185 // If cur_pos is not a leaf node, unmark end of word and assign 0 to it's
186 // frequency for deletion
187 if (numberOfChildren(cur_pos)) {
188 cur_pos->endOfWord = false;
189 cur_pos->frequency = 0;
190 return;
191 }
192
193 // The first character of the suffix to be deleted
194 letter_index = tolower(entry[delete_from_index + 1]) - 97;
195 // Point cur_pos to the next node
196 cur_pos = delete_from->english[letter_index];
197 // Sever the connection from the main trie
198 delete_from->english[letter_index] = nullptr;
199
200 // If number of characters in the suffix are more than 1, recursively delete
201 // each character starting from cur_pos using the helper function
202 if (n > delete_from_index + 2) {
203 DeleteFrom(cur_pos, entry, delete_from_index + 2);
204 }
205 // If the suffix is only 1 char in length
206 else {
207 delete cur_pos;
208 }
209}
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 ...
Definition trie_multiple_search.cpp:134
uint8_t numberOfChildren(Tnode *node)
Function to count the number of children a node in the trie has.
Definition trie_multiple_search.cpp:69
T endl(T... args)
T size(T... args)
T tolower(T... args)
Here is the call graph for this function:

◆ DeleteFrom()

void operations_on_datastructures::trie_operations::Tnode::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 deleted. It traverses till the end of word in a recursive fashion, from there it deletes characters one by one till it reaches back to the initial call.

Parameters
delete_fromthe acting root to the required suffix to be deleted
delete_stringthe string to be deleted from the trie
remove_indexindex denoting the beginning of the substring to be deleted
135 {
136 if (delete_string.size() == remove_index) {
137 int letter_index = tolower(delete_string[remove_index]) - 97;
138
139 DeleteFrom(delete_from->english[letter_index], delete_string,
140 remove_index + 1);
141
142 delete delete_from;
143 }
144}
Here is the call graph for this function:

◆ Insert()

void operations_on_datastructures::trie_operations::Tnode::Insert ( const std::string & entry)

Function to insert a word in the trie.

Parameters
entrystring entry to be inserted in the trie
104 {
105 Tnode *cur_pos = this;
106 int letter_index = 0;
107
108 for (auto &i : entry) {
109 // To ignore case
110 letter_index = tolower(i) - 97;
111
112 // Allocate a node for each character of entry if not present in the
113 // trie
114 if (cur_pos->english[letter_index] == nullptr) {
115 cur_pos->english[letter_index] = new Tnode();
116 }
117
118 cur_pos = cur_pos->english[letter_index];
119 }
120 // cur_pos points to the last char, mark it as end of word
121 cur_pos->endOfWord = true;
122}

◆ numberOfChildren()

uint8_t operations_on_datastructures::trie_operations::Tnode::numberOfChildren ( Tnode * node)
inline

Function to count the number of children a node in the trie has.

Parameters
nodea trie node whose children need to be counted
Returns
count of the number of children of the given node (max 26)
69 {
70 return ENGLISH_ALPHABET_SIZE -
71 std::count(node->english.begin(), node->english.end(), nullptr);
72 }
T count(T... args)
Here is the call graph for this function:

◆ SearchFreqSuggestions()

void operations_on_datastructures::trie_operations::Tnode::SearchFreqSuggestions ( const std::string & key)

Lists out the most frequent words in trie with the longest prefix of the search key that is present in the trie. For example - if trie contains "abc", "abcde", "abcdefg", "abcddef" and they have been previously searched for 3, 1, 2, 4 times respectively, if the search key is "ab", then the longest common prefix is "ab" and only the top 3 frequencies among the matches would be displayed viz. "abcddef", "abc", "abcdefg".

Parameters
keythe string key to be searched for suggestions
365 {
366 Tnode *cur_pos = nullptr, *prev_pos = nullptr;
367 cur_pos = prev_pos = this; // maintaining 2 pointers, initialized to root
368 int letter_index = 0;
369 std::string prefix =
370 ""; // variable storing the updated value of longest common prefix
372 suggestions; // max heap to store (frequency, word) in descending order
373 // of freq
374
376 &suggestions;
377
378 for (auto &i : key) {
379 letter_index = tolower(i) - 97;
380 prev_pos = cur_pos; // Previous pointer updated to point to the last
381 // char of the longest common prefix
382
383 // When the node for the character does not exist, longest prefix has
384 // been determined and SuggestFreqAutocomplete is called
385 if (cur_pos->english[letter_index] == nullptr) {
386 SuggestFreqAutocomplete(prev_pos, prefix, Suggestions);
387 // To display the top 3 results
388 SelectionTop_3(Suggestions);
389 std::cout << "- - - - - - - - - - - - - - - - - - - - - - - - - - "
390 << std::endl;
391 return;
392 }
393 // Updating the longest common prefix
394 prefix += char(tolower(i));
395 cur_pos = cur_pos->english[letter_index];
396 }
397 // If the key is a valid entry of trie, display it @ top of the suggestions
398 if (cur_pos->endOfWord) {
399 (cur_pos->frequency)++;
400 std::cout << key << std::endl;
401 }
402
403 (void)prev_pos; // Idiom to ignore previous pointer
404
405 // Call for Suggestions when the search key is present as an entry/a prefix
406 // in the trie
407 SuggestFreqAutocomplete(cur_pos, prefix, Suggestions);
408 // Display the top 3 results
409 SelectionTop_3(Suggestions);
410
411 std::cout << "- - - - - - - - - - - - - - - - - - - - - - - - - - "
412 << std::endl;
413 return;
414}
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...
Definition trie_multiple_search.cpp:337
void SelectionTop_3(std::priority_queue< std::pair< int, std::string > > *suggestions)
Function to display the 3 suggestions with highest frequency of search hits.
Definition trie_multiple_search.cpp:317
Here is the call graph for this function:

◆ SearchPresence()

bool operations_on_datastructures::trie_operations::Tnode::SearchPresence ( const std::string & key)

Function to check a word's presence in the trie (Basic)

Parameters
keythe string key to be searched in the trie
Returns
true if the key is found
false if the key is not found
217 {
218 Tnode *cur_pos = this;
219 int letter_index = 0;
220
221 for (auto &i : key) {
222 letter_index = tolower(i) - 97;
223 // If any character in the order of the key is absent, word not found!
224 if (cur_pos->english[letter_index] == nullptr) {
225 return false;
226 }
227 cur_pos = cur_pos->english[letter_index];
228 }
229 // Word is only present in the trie if the key is a valid complete entry and
230 // not just a prefix.
231 if (cur_pos->endOfWord) {
232 (cur_pos->frequency)++;
233 return true;
234 } else {
235 return false;
236 }
237}

◆ SearchSuggestions()

void operations_on_datastructures::trie_operations::Tnode::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. For example - if trie contains "abc", "abcde", "abcdefg", "abcddef" and if the search key is "abcdezz", then the longest common prefix is "abcde" and hence search results will be "abcde", "abcdefg".

Parameters
keythe string key to be searched for suggestions
271 {
272 Tnode *cur_pos = nullptr, *prev_pos = nullptr;
273 cur_pos = prev_pos = this; // maintaining 2 pointers, initialized to root
274 int letter_index = 0;
275 std::string prefix =
276 ""; // variable storing the updated value of longest common prefix
277
278 for (auto &i : key) {
279 letter_index = tolower(i) - 97;
280 prev_pos = cur_pos; // Previous pointer updated to point to the last
281 // char of the longest common prefix
282
283 // When the node for the character does not exist, longest prefix has
284 // been determined and SuggestAutocomplete is called
285 if (cur_pos->english[letter_index] == nullptr) {
286 SuggestAutocomplete(prev_pos, prefix);
287 std::cout << "- - - - - - - - - - - - - - - - - - - - - - - - - - "
288 << std::endl;
289 return;
290 }
291 // Updating the longest common prefix
292 prefix += char(tolower(i));
293 cur_pos = cur_pos->english[letter_index];
294 }
295 // If the key is a valid entry of trie, display it @ top of the suggestions
296 if (cur_pos->endOfWord) {
297 std::cout << key << std::endl;
298 (cur_pos->frequency)++;
299 }
300
301 (void)prev_pos; // Idiom to ignore previous pointer
302
303 // Call for suggestions when the search key is present as an entry/a prefix
304 // in the trie
305 SuggestAutocomplete(cur_pos, prefix);
306 std::cout << "- - - - - - - - - - - - - - - - - - - - - - - - - - "
307 << std::endl;
308 return;
309}
void SuggestAutocomplete(Tnode *new_root, const std::string &prefix)
Recursive function to suggest all the entries of trie which have a given common prefix.
Definition trie_multiple_search.cpp:246
Here is the call graph for this function:

◆ SelectionTop_3()

void operations_on_datastructures::trie_operations::Tnode::SelectionTop_3 ( std::priority_queue< std::pair< int, std::string > > * suggestions)

Function to display the 3 suggestions with highest frequency of search hits.

Parameters
suggestionsa max heap that contains pairs of (frequency, word) heapified based on frequency
318 {
319 // Display Either top 3 or total number of suggestions, whichever is smaller
320 int n = suggestions->size(), Top = 0;
321 Top = n < 3 ? n : 3;
322 while (Top--) {
323 std::cout << suggestions->top().second << std::endl;
324 suggestions->pop();
325 }
326}
T pop(T... args)
T top(T... args)
Here is the call graph for this function:

◆ SuggestAutocomplete()

void operations_on_datastructures::trie_operations::Tnode::SuggestAutocomplete ( Tnode * new_root,
const std::string & prefix )

Recursive function to suggest all the entries of trie which have a given common prefix.

Parameters
new_rootpointer pointing to the node corresponding to the last char of prefix
prefixthe common prefix that all the suggestions must have
246 {
247 // Iterate through all 26 nodes as we have to print all strings with the
248 // given prefix
249 int i = 0;
250 for (i = 0; i < ENGLISH_ALPHABET_SIZE; i++) {
251 if (new_root->english[i] != nullptr) {
252 // Print the sugestion only if it's a valid complete entry and not
253 // just a prefix
254 if (new_root->english[i]->endOfWord) {
255 std::cout << prefix + char(i + 97) << std::endl;
256 }
257
258 SuggestAutocomplete(new_root->english[i], prefix + char(i + 97));
259 }
260 }
261}
Here is the call graph for this function:

◆ SuggestFreqAutocomplete()

void operations_on_datastructures::trie_operations::Tnode::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 prefix.

Parameters
new_rootpointer pointing to the node corresponding to the last char of prefix
prefixthe common prefix that all the suggestions must have
suggestionsa max heap that contains pairs of (frequency, word) heapified based on frequency
339 {
340 int i = 0;
341 for (i = 0; i < ENGLISH_ALPHABET_SIZE; i++) {
342 if (new_root->english[i] != nullptr) {
343 // Add to sugestions only if it's a valid complete entry and not
344 // just a prefix
345 if (new_root->english[i]->endOfWord) {
346 suggestions->push(std::make_pair(
347 new_root->english[i]->frequency, prefix + char(i + 97)));
348 }
349
350 SuggestFreqAutocomplete(new_root->english[i], prefix + char(i + 97),
351 suggestions);
352 }
353 }
354}
T make_pair(T... args)
T push(T... args)
Here is the call graph for this function:

The documentation for this class was generated from the following file: