-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathmain.cpp
304 lines (249 loc) · 8.16 KB
/
main.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
#include <iostream>
#include <string>
#include <map>
#include <set>
using namespace std;
class dictionary {
private:
class trie {
private:
class node {
private:
char letter;
string definition;
map<char, node *> *children;
bool terminal;
public:
node(char letter, string definition, bool terminal) {
this->letter = letter;
this->definition = definition;
this->terminal = terminal;
this->children = new map<char, node *>();
};
char getLetter() { return this->letter; };
void setLetter(char letter) { this->letter = letter; };
string getDefinition() { return this->definition; };
void setDefinition(string definition) { this->definition = definition; };
void setTerminal(bool terminal) { this->terminal = terminal; };
bool getTerminal() { return this->terminal; };
set<node *> getChildren() {
set<node *> children;
map<char, node *>::iterator itr;
for (itr = this->children->begin(); itr != this->children->end(); itr++) {
children.insert(itr->second);
}
return children;
};
void addChild(char letter, node *child) { this->children->insert(pair<char, node *>(letter, child)); };
node *getChild(char letter) {
node *child;
try {
child = this->children->at(letter);
} catch (...) {
return NULL;
}
return child;
};
void removeChild(char letter) { this->children->erase(letter); };
bool ifNoChild() { return this->children->empty(); };
int getTotalChildren() { return this->children->size(); };
};
int totalWords;
node *root;
void addWord(node *parent, string word, string definition) {
if (word.length() == 0) {
if (!parent->getTerminal()) {
parent->setTerminal(true);
parent->setDefinition(definition);
this->totalWords++;
}
return;
}
char ch = word[0];
string remainingWord = word.substr(1);
node *child = parent->getChild(ch);
if (child == NULL) {
child = new node(ch, "\0", false);
parent->addChild(ch, child);
}
this->addWord(child, remainingWord, definition);
};
bool find(node *parent, string word) {
if (word.length() == 0) {
return parent->getTerminal();
}
char ch = word[0];
string remainingWord = word.substr(1);
node *child = parent->getChild(ch);
if (child == NULL) {
return false;
}
return this->find(child, remainingWord);
};
string getWordDefinition(node *parent, string word) {
if (word.length() == 0) {
if (parent->getTerminal())
return parent->getDefinition();
else
return "";
}
char ch = word[0];
string remainingWord = word.substr(1);
node *child = parent->getChild(ch);
if (child == NULL) {
return "";
}
return this->getWordDefinition(child, remainingWord);
};
void removeWord(node *parent, string word) {
if (word.length() == 0) {
if (parent->getTerminal()) {
parent->setTerminal(false);
this->totalWords--;
}
return;
}
char ch = word[0];
string remainingWord = word.substr(1);
node *child = parent->getChild(ch);
if (child == NULL) {
return;
}
this->removeWord(child, remainingWord);
if (!child->getTerminal() && child->ifNoChild()) {
parent->removeChild(ch);
}
};
node *getPrefixEnd(node *parent, string prefix) {
if (prefix.length() == 0) {
return parent;
}
char ch = prefix[0];
string remainingPrefix = prefix.substr(1);
node *child = parent->getChild(ch);
if (child == NULL) {
return NULL;
}
return this->getPrefixEnd(child, remainingPrefix);
};
set<string> wordsFrom(node *treeNode, string word, set<string> words) {
if (treeNode == NULL) {
return words;
}
set<node *> children = treeNode->getChildren();
for (auto child : children) {
char ch = child->getLetter();
string currentWord = word + ch;
if (child->getTerminal()) {
words.insert(words.end(), currentWord);
}
words = this->wordsFrom(child, currentWord, words);
}
return words;
};
public:
trie() {
this->root = new node('\0', "\0", false);
this->totalWords = 0;
}
int getTotalWords() { return this->totalWords; };
bool isEmpty() { return getTotalWords() == 0; };
void addWord(string word, string definition) { this->addWord(this->root, word, definition); };
bool find(string word) { return this->find(this->root, word); };
string getWordDefinition(string word) { return this->getWordDefinition(this->root, word); };
void removeWord(string word) { this->removeWord(this->root, word); };
set<string> wordsFrom(string prefix) {
node *prefixEnd = this->getPrefixEnd(this->root, prefix);
if (prefixEnd == NULL)
return set<string>();
return this->wordsFrom(prefixEnd, prefix, set<string>());
};
};
trie *tree;
public:
dictionary() {
this->tree = new trie();
}
void add() {
string word, definition;
cout << "Word: ";
cin >> word;
cout << "Definition: ";
cin >> definition;
this->tree->addWord(word, definition);
cout << "Word Added\n";
};
void update() {
string word, definition;
cout << "Word: ";
cin >> word;
cout << "Difination: ";
cin >> definition;
this->tree->addWord(word, definition);
cout << "Word Definition Updated\n";
};
void remove() {
string word;
cout << "Word: ";
cin >> word;
this->tree->removeWord(word);
cout << "Word Removed\n";
};
void search() {
string word;
cout << "Word: ";
cin >> word;
bool found = this->tree->find(word);
if (!found) {
cout << "Not Found!\n";
return;
}
cout << "Definition: ";
cout << this->tree->getWordDefinition(word);
};
void prefixSearch() {
string prefix;
cout << "Prefix: ";
cin >> prefix;
set<string> results = this->tree->wordsFrom(prefix);
for (auto word : results) {
cout << word << endl;
}
};
};
int main() {
dictionary myDictionary;
menu:
cout << "1) Add Word\n";
cout << "2) Update Word\n";
cout << "3) Search Word\n";
cout << "4) Remove Word\n";
cout << "5) Prefix Search\n";
cout << "6) Exit\n";
int opt;
option:
cin >> opt;
switch (opt) {
case 1:
myDictionary.add();
break;
case 2:
myDictionary.update();
break;
case 3:
myDictionary.search();
break;
case 4:
myDictionary.remove();
break;
case 5:
myDictionary.prefixSearch();
break;
case 6:
exit(0);
default:
goto option;
}
cout << endl;
goto menu;
}