|
| 1 | +#include <iostream> |
| 2 | +using namespace std; |
| 3 | + |
| 4 | +class TrieNode{ |
| 5 | + public: |
| 6 | + char data; |
| 7 | + TrieNode* children[26]; |
| 8 | + bool isTerminal; |
| 9 | + |
| 10 | + TrieNode(int data){ |
| 11 | + this -> data = data; |
| 12 | + for(int i = 0; i < 26; i++) children[i] = NULL; |
| 13 | + isTerminal = false; |
| 14 | + } |
| 15 | +}; |
| 16 | + |
| 17 | +class Trie{ |
| 18 | + public: |
| 19 | + TrieNode* root; |
| 20 | + |
| 21 | + Trie(){ |
| 22 | + root = new TrieNode('\0'); |
| 23 | + } |
| 24 | + |
| 25 | + void insertUtil(TrieNode* root, string& word, int index){ |
| 26 | + if(index == word.size()) { |
| 27 | + root -> isTerminal = true; |
| 28 | + return; |
| 29 | + } |
| 30 | + |
| 31 | + int charIndex = word[index] - 'a'; |
| 32 | + if(root -> children[charIndex] == NULL) root -> children[charIndex] = new TrieNode(word[index]); |
| 33 | + |
| 34 | + insertUtil(root->children[charIndex], word, index+1); |
| 35 | + } |
| 36 | + void insertWord(string word){ |
| 37 | + for (char& c : word) c = tolower(c); |
| 38 | + insertUtil(root, word, 0); |
| 39 | + } |
| 40 | + |
| 41 | + bool searchUtil(TrieNode* root, string word, int index){ |
| 42 | + if(index == word.size()) return root -> isTerminal; |
| 43 | + |
| 44 | + int charIndex = word[index] - 'a'; |
| 45 | + if(root -> children[charIndex] == NULL) return false; |
| 46 | + |
| 47 | + return searchUtil(root->children[charIndex], word, index+1); |
| 48 | + } |
| 49 | + |
| 50 | + bool searchWord(string word){ |
| 51 | + for (char& c : word) c = tolower(c); |
| 52 | + return searchUtil(root, word, 0); |
| 53 | + } |
| 54 | + |
| 55 | + bool removeWordUtil(TrieNode* root, string word, int index){ |
| 56 | + if(index == word.size()){ |
| 57 | + if(!root -> isTerminal) return false; |
| 58 | + |
| 59 | + root -> isTerminal = false; |
| 60 | + |
| 61 | + for(int i = 0; i < 26; i++) if(root -> children[i] != NULL) return false; |
| 62 | + return true; |
| 63 | + } |
| 64 | + |
| 65 | + int charIndex = word[index] - 'a'; |
| 66 | + TrieNode* child = root -> children[charIndex]; |
| 67 | + if(child == NULL) return false; |
| 68 | + |
| 69 | + bool shouldDeleteChild = removeWordUtil(child, word, index + 1); |
| 70 | + |
| 71 | + if(shouldDeleteChild){ |
| 72 | + delete child; |
| 73 | + root -> children[charIndex] = NULL; |
| 74 | + |
| 75 | + if(!root -> isTerminal){ |
| 76 | + for(int i = 0; i < 26; i++) if(root->children[i] != NULL) return false; |
| 77 | + return true; |
| 78 | + } |
| 79 | + } |
| 80 | + |
| 81 | + return false; |
| 82 | + } |
| 83 | + void removeWord(string word){ |
| 84 | + for (char& c : word) c = tolower(c); |
| 85 | + removeWordUtil(root, word, 0); |
| 86 | + } |
| 87 | +}; |
| 88 | + |
| 89 | +int main() { |
| 90 | + Trie* t = new Trie(); |
| 91 | + |
| 92 | + // Insert words |
| 93 | + t->insertWord("abcd"); |
| 94 | + t->insertWord("abc"); |
| 95 | + t->insertWord("trie"); |
| 96 | + |
| 97 | + |
| 98 | + // Search words |
| 99 | + cout << "------------- Search Words -------------" << endl; |
| 100 | + cout << "abc : "<<(t->searchWord("abc") ? "Found\n" : "Not Found\n"); |
| 101 | + cout << "abcd : "<<(t->searchWord("abcd") ? "Found\n" : "Not Found\n"); |
| 102 | + cout << "trie : "<<(t->searchWord("trie") ? "Found\n" : "Not Found\n"); |
| 103 | + cout << "tried : "<<(t->searchWord("tried") ? "Found\n" : "Not Found\n"); |
| 104 | + |
| 105 | + // Remove words |
| 106 | + cout << "------------- Remove Words -------------" << endl; |
| 107 | + t->removeWord("abc"); |
| 108 | + t->removeWord("abcd"); |
| 109 | + |
| 110 | + cout << "abc : "<<(t->searchWord("abc") ? "Found\n" : "Not Found\n"); |
| 111 | + cout << "abcd : "<<(t->searchWord("abcd") ? "Found\n" : "Not Found\n"); |
| 112 | + |
| 113 | + // Cleanup |
| 114 | + delete t; |
| 115 | + |
| 116 | + return 0; |
| 117 | +} |
0 commit comments