-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathmain.cpp
More file actions
225 lines (202 loc) · 6.72 KB
/
Copy pathmain.cpp
File metadata and controls
225 lines (202 loc) · 6.72 KB
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
#include<bits/stdc++.h>
using namespace std;
// Converts key current character into index
#define CHAR_TO_INDEX(c) ((int)c - (int)'a')
// trie node
struct TrieNode
{
struct TrieNode *letters[26];
int isPresent;
string meaning;
};
// Returns new trie node (initialized to NULLs)
struct TrieNode *newTrieNode(void)
{
struct TrieNode *pNode = new TrieNode;
pNode->isPresent = 0;
pNode->meaning="";
for (int i = 0; i < 26; i++)
pNode->letters[i] = NULL;
return pNode;
}
// If not present, inserts key into trie.
void insert(struct TrieNode *root, string str,string meaning)
{
struct TrieNode *pCrawl = root;
for (int level = 0; level < str.length(); level++)
{
int index = CHAR_TO_INDEX(str[level]);
if (pCrawl->letters[index]==NULL)
pCrawl->letters[index] = newTrieNode();
pCrawl = pCrawl->letters[index];
}
// mark last node as leaf and add meaning
pCrawl->isPresent = 1;
pCrawl->meaning = meaning;
}
// Returns true if str presents in trie, else false
string search(struct TrieNode *root, string str)
{
int length = str.length();
struct TrieNode *pCrawl = root;
for (int level = 0; level < length; level++)
{
int index = CHAR_TO_INDEX(str[level]);
if (pCrawl->letters[index]==NULL)
return "None";
pCrawl = pCrawl->letters[index];
}
if (pCrawl != NULL && pCrawl->isPresent)
return pCrawl->meaning;
else
return "None";
}
// Returns 0 if current node has a child
// If all letters are NULL, return 1.
bool isLastNode(struct TrieNode* root)
{
for (int i = 0; i < 26; i++)
if (root->letters[i])
return 0;
return 1;
}
// Recursive function to print auto-suggestions for given node.
vector<string>suggestions;
void suggestionsRec(struct TrieNode* root, string currPrefix)
{
// found a string in Trie with the given prefix
if (root->isPresent)
{
suggestions.push_back(currPrefix);
}
// All letters struct node pointers are NULL
if (isLastNode(root))
return;
for (int i = 0; i < 26; i++)
{
if (root->letters[i])
{
currPrefix.push_back(97 + i);
suggestionsRec(root->letters[i], currPrefix);
currPrefix.pop_back();
}
}
}
// print suggestions for given query prefix.
int printAutoSuggestions(TrieNode* root, string query)
{
struct TrieNode* pCrawl = root;
int level;
int n = query.length();
for (level = 0; level < n; level++)
{
int index = CHAR_TO_INDEX(query[level]);
// no string in the Trie has this prefix
if (pCrawl->letters[index]==NULL)
return 0;
pCrawl = pCrawl->letters[index];
}
// If prefix is last node of tree
bool isLast = isLastNode(pCrawl);
// If there are nodes below last node
if (!isLast)
{
string prefix = query;
suggestionsRec(pCrawl, prefix);
vector<string>::iterator itr ;
int j=1;
for(itr=suggestions.begin();itr!=suggestions.end();itr++)
{
cout<<j++<<". "<<*itr<<endl;
}
return 1;
}
}
// Main function
int main()
{
struct TrieNode* root = newTrieNode();
insert(root, "quarantine", "a state, period, or place of isolation in which people or animals that have arrived from elsewhere or been exposed to infectious or contagious disease are placed");
insert(root, "quantum", "a discrete quantity of energy proportional in magnitude to the frequency of the radiation it represents");
insert(root, "quarrel", " a heated argument or disagreement");
insert(root, "hammer", "a tool with a heavy metal head mounted at right angles at the end of a handle");
insert(root, "hamster", "a solitary burrowing rodent with a short tail and large cheek pouches for carrying food");
insert(root, "hand", "the end part of a person's arm beyond the wrist");
insert(root, "haze", "a slight obscuration of the lower atmosphere, typically caused by fine suspended particles");
insert(root, "hair", "any of the fine threadlike strands growing from the skin of humans, mammals, and some other animals");
insert(root, "cow", "a fully grown female animal of a domesticated breed of ox, kept to produce milk or beef");
insert(root, "coward", "a person who is contemptibly lacking in the courage to do or endure dangerous or unpleasant things");
insert(root, "compound", "sympathetic pity and concern for the sufferings or misfortunes of others");
insert(root, "compassion", "meaning of helping");
insert(root, "combustion", "the process of burning something");
vector<string>history;
cout<<"------------------------------\n";
cout<<" Welcome to ABC Dictionary\n";
cout<<"------------------------------\n\n";
//Loop until user enters "no"
string ch="no";
do{
//Input query
cout<<"Enter word to search\n";
string input;
cin>>input;
string ans = search(root,input);
//If query is found
if(ans!="None")
{
history.push_back(input);
cout<<ans<<endl<<endl;
}
//Query not found
else
{
//Suggestions are being printed
int comp = printAutoSuggestions(root,input);
if (comp == 0)
cout << "No string found with this prefix\n";
else
{
cout<<endl<<input<<" not found..."<<" Above words are a few suggestions\n";
cout<<"Enter number of word to be searched or press 0 to exit\n";
int n;
cin>>n;
if(n==0)
cout<<"Sorry can't find your word\n";
else
{
string ans=search(root,suggestions[n-1]);
history.push_back(suggestions[n-1]);
cout<<ans<<endl;
}
cout<<endl;
}
}
string his;
//Ask to print history
cout<<"Do you want to check history?\n";
cin>>his;
//Print history if available
if(his=="yes")
{
if(history.size()==0)
cout<<"\nNo history available\n";
else
{
cout<<"\nSEARCH HISTORY:\n";
for(int i=0;i<history.size();i++)
{
cout<<history[i]<<endl;
}
}
}
cout<<endl;
//Clear suggestions for next query
suggestions.clear();
//Ask to search another query
cout<<"Do you want to search other word?\n";
cin>>ch;
}while(ch=="yes");
//Exit dictionary
cout<<"\nTHANK YOU FOR USING ABC DICTIONARY";
return 0;
}