-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathtrie.cpp
359 lines (273 loc) · 8.95 KB
/
trie.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
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
#include <iostream>
#include <string.h>
using namespace std;
#define ALPHABET_SIZE 26
#define false 0
#define true 1
#define MAX_WORD_SIZE 50
#define MAX_MEANING_SIZE 1000
int letterToInt(char letter)
{
if (letter >= 'A' && letter <= 'Z')
{
return letter - 'A';
}
else if (letter >= 'a' && letter <= 'z')
{
return letter - 'a';
}
return -1;
}
struct trieNode
{
struct trieNode* children[ALPHABET_SIZE];
bool isEndOfWord;
string value;
};
struct trieNode* getNode(void)
{
struct trieNode *pNode = new trieNode;
pNode->isEndOfWord = false;
for (int i = 0; i < ALPHABET_SIZE; i++)
pNode->children[i] = NULL;
return pNode;
}
int insert(struct trieNode *root, string word, string meaning)
{
struct trieNode *pCrawl = root;
for (int i = 0; i < word.length(); i++)
{
int index = letterToInt(word[i]);
if (!pCrawl->children[index])
pCrawl->children[index] = getNode();
pCrawl = pCrawl->children[index];
}
pCrawl->isEndOfWord = true;
pCrawl->value = meaning;
return true;
}
int readWordsFromFile(struct trieNode *root, const char* filename)
{
FILE *file = fopen(filename, "r");
if (!file)
{
printf("could not find/open file \"%s\"\n", filename);
return false;
}
char word[MAX_WORD_SIZE];
char meaning[MAX_MEANING_SIZE];
int count = 0;
while (fscanf(file, "%s %[^\n]", word, meaning) > 1)
{
if (!insert(root, word, meaning))
{
fclose(file);
return false;
}
else
{
count++;
}
}
fclose(file);
cout << "Welcome to our Dictionary. Our Dictionary right now contains " << count << " words.\n";
return true;
}
bool search(struct trieNode *root, string word)
{
struct trieNode *pCrawl = root;
for (int i = 0; i < word.length(); i++)
{
int index = letterToInt(word[i]);
if (!pCrawl->children[index])
return false;
pCrawl = pCrawl->children[index];
}
if (pCrawl != NULL && pCrawl->isEndOfWord) {
cout << "The meaning of the word is: \n";
cout << pCrawl->value;
}
return true;
}
bool deleteWord(struct trieNode *root, string word)
{
struct trieNode *pCrawl = root;
for (int i = 0; i < word.length(); i++)
{
int index = letterToInt(word[i]);
if (!pCrawl->children[index])
return false;
pCrawl = pCrawl->children[index];
}
if (pCrawl != NULL && pCrawl->isEndOfWord) {
pCrawl->isEndOfWord = false;
return true;
}
}
void alphabeticalOrder(struct trieNode* root, char allWords[], int index)
{
struct trieNode* pCrawl = root;
if(pCrawl != NULL && pCrawl->isEndOfWord == true) {
for(int j = 0; j < index; j++) {
cout << allWords[j];
}
cout << ": ";
cout << pCrawl->value << "\n";
}
for(int i = 0; i < ALPHABET_SIZE; i++) {
if(pCrawl->children[i] != NULL) {
allWords[index] = 'a' + i;
alphabeticalOrder(pCrawl->children[i], allWords, index + 1);
}
}
}
void print_prefix_search(struct trieNode* root, char allWords[], int index, string prefix)
{
struct trieNode* pCrawl = root;
if(pCrawl != NULL && pCrawl->isEndOfWord == true) {
cout << prefix;
for(int j = 0; j < index; j++) {
cout << allWords[j];
}
cout << ": ";
cout << pCrawl->value << "\n";
}
for(int i = 0; i < ALPHABET_SIZE; i++) {
if(pCrawl->children[i] != NULL) {
allWords[index] = 'a' + i;
print_prefix_search(pCrawl->children[i], allWords, index + 1, prefix);
}
}
}
void prefix_search(struct trieNode* root, string prefix)
{
struct trieNode* pCrawl = root;
for(int i = 0; i < prefix.length(); i++) {
int index = letterToInt(prefix[i]);
pCrawl = pCrawl->children[index];
}
char allWords[50];
print_prefix_search(pCrawl, allWords, 0, prefix);
}
bool searchSuggestedWord(struct trieNode* root, string word, int count)
{
struct trieNode *pCrawl = root;
for (int i = 0; i < word.length(); i++)
{
int index = letterToInt(word[i]);
if (!pCrawl->children[index])
return false;
pCrawl = pCrawl->children[index];
}
if (pCrawl != NULL && pCrawl->isEndOfWord) {
if(count == 0) {
cout << "Suggested words are: ";
}
cout << word << " ";
}
return true;
}
bool suggestedWords(struct trieNode* root, string word, int number)
{
int count = 0;
for(int i = 0; i < word.length(); i++) {
string temp = word;
for(int j = 0; j < ALPHABET_SIZE; j++) {
if(count < number && j != letterToInt(word[word.length() - i - 1])) {
temp[word.length() - i - 1] = j + 97;
if(searchSuggestedWord(root, temp, count)) {
count++;
}
}
}
}
if(count == 0) {
cout << "\nSorry we couldn't find any words related to " << word << " in our Dictionary.\n";
return false;
}
else if (count < number) {
cout << "\nWe could only find " << count << " words related to " << word << " in our Dictionary.\n";
return true;
}
else {
return true;
}
}
int main() {
struct trieNode *root = getNode();
readWordsFromFile(root, "words.txt");
string command;
do {
cout << "\nWhat would you like to do?\n";
cout << "1. Insert a word.\n";
cout << "2. Search a word.\n";
cout << "3. Delete a word.\n";
cout << "4. Print dictionary in alphabetical order.\n";
cout << "5. Prefix search.\n";
cout << "6. Show suggested words.\n";
cout << "\n";
fflush(stdin);
int call;
cin >> call;
cout << "\n";
string word;
string meaning;
switch(call) {
case 1: cout << "Enter the word you would like to insert: ";
cin >> word;
cout << "Enter its meaning: ";
cin >> meaning;
if(insert(root, word, meaning)) {
cout << word << " has been added to the Dictionary.\n";
}
break;
case 2: cout << "Enter the word you would like to search: ";
cin >> word;
if(!search(root, word)) {
cout << "Sorry, the word you searched for doesn't exist. Would you like to add it to the Dictionary.(Yes/No) ";
string add;
cin >> add;
if(add == "yes" || add == "Yes" || command == "YES") {
cout << "Enter its meaning: ";
cin >> meaning;
if(insert(root, word, meaning)) {
cout << word << " has been added to the Dictionary.\n";
}
}
}
break;
case 3: cout << "Which word would you like to delete? ";
cin >> word;
if(deleteWord(root, word)) {
cout << word << " has been successfully deleted from the Dictionary.\n";
}
else {
cout << "No such word exists in the Dictionary.\n";
}
break;
case 4: char allWords[50];
alphabeticalOrder(root, allWords, 0);
cout << "\n";
break;
case 5: cout << "Enter the word you would like to use as a prefix: ";
cin >> word;
cout << "\n";
prefix_search(root, word);
break;
case 6: cout << "Enter the word for whose suggested words you want to see: ";
cin >> word;
int number;
cout << "How many suggested words do you want to see: ";
cin >> number;
suggestedWords(root, word, number);
break;
default: cout << "Enter a valid entry.";
}
fflush(stdin);
cout << "\n\nWould you like to continue or exit?(Yes/N0) ";
cin >> command;
}
while(command == "yes" || command == "Yes" || command == "YES");
cout << "Thanks for using our Dictionary.\n";
return 0;
}