1+ #include < iostream>
2+ #include < map>
3+ #include < vector>
4+ #include < algorithm>
5+ using namespace std ;
6+
7+ class TrieNode
8+ {
9+ public:
10+ map<char , TrieNode *> children;
11+ bool isEnd;
12+
13+ TrieNode () : isEnd(false ) {}
14+ };
15+
16+ class Trie
17+ {
18+ private:
19+ TrieNode *root;
20+
21+ void getAllWordsFromNode (TrieNode *node, string prefix, vector<string> &result) const
22+ {
23+ if (!node)
24+ return ;
25+ if (node->isEnd )
26+ result.push_back (prefix);
27+ for (auto &p : node->children )
28+ getAllWordsFromNode (p.second , prefix + p.first , result);
29+ }
30+
31+ bool deleteHelper (TrieNode *node, const string &word, int depth)
32+ {
33+ if (!node)
34+ return false ;
35+
36+ if (depth == word.size ())
37+ {
38+ if (!node->isEnd )
39+ return false ;
40+ node->isEnd = false ;
41+ return node->children .empty ();
42+ }
43+
44+ char c = word[depth];
45+
46+ if (node->children .find (c) == node->children .end ())
47+ return false ;
48+
49+ if (deleteHelper (node->children [c], word, depth + 1 ))
50+ {
51+ delete node->children [c];
52+ node->children .erase (c);
53+ return !node->isEnd && node->children .empty ();
54+ }
55+ return false ;
56+ }
57+
58+ void freeMemory (TrieNode *node)
59+ {
60+ for (auto &p : node->children )
61+ freeMemory (p.second );
62+ delete node;
63+ }
64+
65+ public:
66+ Trie () { root = new TrieNode (); }
67+
68+ ~Trie () { freeMemory (root); }
69+
70+ void insert (const string &word)
71+ {
72+ TrieNode *current = root;
73+ for (char c : word)
74+ {
75+ if (current->children .find (c) == current->children .end ())
76+ current->children [c] = new TrieNode ();
77+ current = current->children [c];
78+ }
79+ current->isEnd = true ;
80+ }
81+
82+ bool search (const string &word) const
83+ {
84+ TrieNode *current = root;
85+ for (char c : word)
86+ {
87+ if (current->children .find (c) == current->children .end ())
88+ return false ;
89+ current = current->children [c];
90+ }
91+ return current->isEnd ;
92+ }
93+
94+ vector<string> autoSuggest (const string &prefix) const
95+ {
96+ vector<string> result;
97+ TrieNode *current = root;
98+
99+ for (char c : prefix)
100+ {
101+ if (current->children .find (c) == current->children .end ())
102+ return result;
103+ current = current->children [c];
104+ }
105+
106+ getAllWordsFromNode (current, prefix, result);
107+ return result;
108+ }
109+
110+ void displayAll () const
111+ {
112+ vector<string> words;
113+ getAllWordsFromNode (root, " " , words);
114+ for (auto &w : words)
115+ cout << w << " \n " ;
116+ }
117+
118+ void deleteWord (const string &word)
119+ {
120+ deleteHelper (root, word, 0 );
121+ }
122+ };
123+
124+ int main ()
125+ {
126+ Trie dictionary;
127+ int choice;
128+ string word, prefix;
129+
130+ while (true )
131+ {
132+ cout << " \n ===== Dictionary Application =====\n " ;
133+ cout << " 1. Insert Word\n " ;
134+ cout << " 2. Auto Suggest\n " ;
135+ cout << " 3. Delete Word\n " ;
136+ cout << " 4. Display All Words\n " ;
137+ cout << " 5. Exit\n " ;
138+ cout << " Enter your choice: " ;
139+
140+ cin >> choice;
141+ if (cin.fail ())
142+ {
143+ cin.clear ();
144+ cin.ignore (1000 , ' \n ' );
145+ cout << " Invalid input. Please enter a number.\n " ;
146+ continue ;
147+ }
148+
149+ if (choice == 5 )
150+ {
151+ cout << " Exiting program..." << endl;
152+ break ;
153+ }
154+
155+ switch (choice)
156+ {
157+ case 1 :
158+ cout << " Enter word to insert: " ;
159+ cin >> word;
160+ dictionary.insert (word);
161+ cout << " Inserted successfully!\n " ;
162+ break ;
163+
164+ case 2 :
165+ cout << " Enter prefix: " ;
166+ cin >> prefix;
167+ {
168+ vector<string> suggestions = dictionary.autoSuggest (prefix);
169+ if (suggestions.empty ())
170+ cout << " No suggestions found.\n " ;
171+ else
172+ for (auto &s : suggestions)
173+ cout << s << " \n " ;
174+ }
175+ break ;
176+
177+ case 3 :
178+ cout << " Enter word to delete: " ;
179+ cin >> word;
180+ dictionary.deleteWord (word);
181+ cout << " Deleted (if existed).\n " ;
182+ break ;
183+
184+ case 4 :
185+ cout << " All words stored:\n " ;
186+ dictionary.displayAll ();
187+ break ;
188+
189+ default :
190+ cout << " Invalid choice. Try again.\n " ;
191+ }
192+ }
193+
194+ return 0 ;
195+ }
0 commit comments