-
Notifications
You must be signed in to change notification settings - Fork 0
/
trie.cpp
253 lines (201 loc) · 5.49 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
#include <bits/stdc++.h>
using namespace std;
struct trienode {
bool isWordEnd;
trienode* child[26];
};
trienode* get_new_node()
{
trienode* temp = new trienode;
temp->isWordEnd = false;
for (int i = 0; i < 26; i++)
{
temp->child[i] = NULL;
}
return temp;
}
void insert(trienode* root, string key)
{
trienode* temp = root;
for (int i = 0; i < (int)key.length(); i++)
{
int ind = int(key[i]) - 'a';
if (temp->child[ind] == NULL)
{
temp->child[ind] = get_new_node();
}
temp = temp->child[ind];
}
temp->isWordEnd = true;
}
bool search(trienode* root, string key)
{
if (root == NULL) return false;
trienode* temp = root;
for (int i = 0; i < (int)key.length(); i++)
{
int ind = int(key[i]) - 'a';
if (temp->child[ind] == NULL) return false;
temp = temp->child[ind];
}
return temp->isWordEnd;
}
trienode* helper(trienode* root, string key, int ind)
{ // if tree is empty
if (root == NULL) return NULL;
/*
helper function takes 3 parameters
1) root of subtrie (or subtree)
2) processing key (key which is going to be deleted)
3) ind (index) -> which character is processing or how much depth
it returns root of this subtrie
*/
bool isempty = true;
for (int i = 0; i < 26; i++)
{
if (root->child[i] != NULL)
{
isempty = false;
break;
}
}
if (ind == key.length())
{
/* hit the end of key
possibilty -> 1) this key is prefix of other key,
int this case,set is_word of current root equals to false;
possibilty ->2) this key is not prefix of any other key
in this case delete this node;
*/
if (isempty == false)
{
// case 1
root->isWordEnd = false;
}
else
{
//case 2
delete(root);
root = NULL;
}
return root;
}
int pos = int(key[ind]) - 'a';
root->child[pos] = helper(root->child[pos], key, ind + 1);
isempty = true;
for (int i = 0; i < 26; i++)
{
if (root->child[i] != NULL)
{
isempty = false;
break;
}
}
/* here we have two case
1) if all child nodes of root is empty and this root is not marked as
of word then delete this node
2) if all child nodes are not empty "OR" this root os marked as end of
word then do nothing
*/
if (isempty && root->isWordEnd == false)
{
delete(root);
root = NULL;
}
return root;
}
trienode* delete_key(trienode* root, string key)
{ /* if trie is empty */
if (root == NULL) return root;
/* if key is empty */
if (key == "") return root;
/* delete_key function takes two parameters
1) root of trie
2) key to be deleted
delete_key function returns root of modified trie
*/
root = helper(root, key, 0);
return root;
}
void find_all_words(trienode* root, string key, vector<string>& all_words)
{
if (root == NULL) return;
trienode* temp = root;
for (int i = 0; i < (int)key.size(); i++)
{
int ind = int(key[i]) - 'a';
/* if child node at ind of current root is NULL this prefix do not exist in trie */
if (temp->child[ind] == NULL) return;
temp = temp->child[ind];
}
/* if this prefix exist in trie then do BFS from current node to find all words */
queue<pair<trienode*, string> > q;
q.push(make_pair(temp, key));
string this_word;
while (!q.empty())
{
temp = q.front().first;
this_word = q.front().second;
/* if this is word ,then add it to the all_words(vector) */
if (temp->isWordEnd) all_words.push_back(this_word);
q.pop();
for (int i = 0; i < 26; i++)
{
if (temp->child[i] != NULL)
{
q.push(make_pair(temp->child[i], this_word + char(i + int('a'))));
}
}
}
}
void auto_complete(trienode* root, string key)
{
/* this function takes two parametes
1.) root node of trie
2.) key for which suggestion need to find.
this function prints all the words for which key is prefix.
*/
/* if key is empty , no need to print */
if (key.size() == 0) return;
/* if key is not empty , then we will print all words in trie for which key is PREFIX. */
vector<string> all_words;
find_all_words(root, key, all_words);
/* print all words */
if (all_words.size() == 0)
{
cout << "There are no suggestions." << '\n';
return;
}
cout << "There are " << all_words.size() << " suggestions for this prefix " << key << '\n';
for (int i = 0; i < (int)all_words.size(); i++) {
cout << all_words[i] << ' ';
}
return;
}
int main()
{
/* creating dictionary*/
int n;
cin >> n;
vector<string> dict;
string key;
for (int i = 0; i < n; i++)
{
cout << "Enter word" << endl;
cin >> key;
dict.push_back(key);
}
/* creats root node*/
cout << "creating root node";
trienode* root = get_new_node();
cout << "Root node created";
/* inserts all words of dictionary to trie*/
for (int i = 0; i < dict.size(); i++)
{
insert(root, dict[i]);
}
cout << "Enter key to autocomplete :" << '\n';
cin >> key;
auto_complete(root, key);
return 0;
}