-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtrie.cpp
More file actions
315 lines (277 loc) · 9.86 KB
/
Copy pathtrie.cpp
File metadata and controls
315 lines (277 loc) · 9.86 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
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
#include <iostream>
#include <list>
#include <map>
using namespace std;
template <class T>
class OptimizedTrie
{
class Node
{
public:
string identifier;
T* data;
map<char, Node*> children;
};
Node* root = NULL;
public:
OptimizedTrie()
{
// initialize the root node
root = new Node();
root->data = NULL;
root->identifier = "";
}
void insert(string word, T data)
{
T* dataptr = new T(data);
int i =0;
// find common substring split, if found
// else if identifier length < word then go to next child
Node* prev = NULL;
Node* temp = root;
while (i<word.length())
{
int temp_i = 0;
while (temp->identifier[temp_i] != '\0')
{
if (temp->identifier[temp_i] == word[i])
{
temp_i++;
i++;
}
else
{
// non common letter exists at ith index on the word
// in this case create a new node and add two childs one to store the previous chain
// and one to store the new word
// node where common identifier exists exists
// a new common node to insert before
Node* common_node = new Node();
common_node->identifier = temp->identifier.substr(0,temp_i);
//changing the identifier of prev node
temp->identifier = temp->identifier.substr( temp_i );
common_node->children[temp->identifier[0]] = temp;
// if the word has some non matching characters
if (word[i] != '\0')
{
// creating separate node to store the new data
Node* node2 = new Node();
node2->identifier = word.substr(i);
node2->data = dataptr;
// building the link between node with data and common node
common_node->children[word[i]] = node2;
// setting the parent of common node
prev->children[common_node->identifier[0]] = common_node;
}
// if the word only consists of matching characters
else
{
common_node->data = dataptr;
}
return;
}
}
if (temp->children.find(word[i]) == temp->children.end())
{
Node* newNode = new Node();
newNode->identifier = word.substr(i, word.length());
newNode->data = dataptr;
temp->children[word[i]] = newNode;
break;
}
else
{
prev = temp;
temp = temp->children[word[i]];
}
}
}
Node* getNode(string word)
{
// returns the node with the identifier
int word_i = 0;
Node* temp = root;
// temp variable to traverse the nodes in trie
while(temp != NULL)
{
int temp_i = 0;
// compare corresponding characters of word and identifier
while (temp->identifier[temp_i] != '\0')
{
// if the current character is same then increment variable to compare next character
if (temp->identifier[temp_i] == word[word_i])
{
temp_i++;
word_i++;
}
// if at any point the corresponding characters do not match then the word is not present in the trie
else
{
return NULL;
}
}
// if the word at i'th index is last character of word then the word is found
if (word[word_i] == '\0')
{
if (temp->data != NULL)
return temp;
else
return NULL;
}
// if the word at i'th index is not the last character of word
// then find the node that begins with the character in the children of the current node
if (temp->children.find(word[word_i]) == temp->children.end())
{
// if there is no node at ith index then the word does not exists
return NULL;
}
else
//if there is a node at ith index then search for the remaining word in the child node
temp = temp -> children[word[word_i]];
}
return NULL;
}
void search(string word)
{
Node* node = getNode(word);
if (node != NULL)
{
// if the node exists but does not contain any data then
cout<<"The data stored in node with key "<<word<<" is "<<*node->data<<endl;
}
}
void Delete(string word)
{
// returns the node with the identifier
int word_i = 0;
Node* prev = NULL;
Node* temp = root;
// temp variable to traverse the nodes in trie
while(temp != NULL)
{
int temp_i = 0;
// compare corresponding characters of word and identifier
while (temp->identifier[temp_i] != '\0')
{
// if the current character is same then increment variable to compare next character
if (temp->identifier[temp_i] == word[word_i])
{
temp_i++;
word_i++;
}
// if at any point the corresponding characters do not match then the word is not present in the trie
else
{
return;
}
}
// if the word at i'th index is last character of word then the word is found
if (word[word_i] == '\0')
{
if (temp->data != NULL)
break;
else
return;
}
// if the word at i'th index is not the last character of word
// then find the node that begins with the character in the children of the current node
if (temp->children.find(word[word_i]) == temp->children.end())
{
// if there is no node at ith index then the word does not exists
return;
}
else
{
prev = temp;
//if there is a node at that begins with character at word_i then search for the remaining word in that child node
temp = temp -> children[word[word_i]];
}
}
cout<<"deleting the node with key "<<word<<endl;
// if the node has children then delete its data
if (temp->children.size() > 0)
{
temp->data = NULL;
}
// if the node has no child then delete the node
else
{
prev->children.erase(temp->identifier[0]);
}
}
// a function to print values of all nodes starting with a given string in the trie
void printChildren(string prefix)
{
// temp is the node that contains the prefix
Node* temp = root;
int word_i =0;
// find the node where prefix exists
while(temp != NULL)
{
int temp_i = 0;
// loop through every character in the current node
while (temp->identifier[temp_i] != '\0'&&prefix[word_i] != '\0')
{
if (temp->identifier[temp_i] == prefix[word_i])
{
temp_i++;
word_i++;
}
else
{
return;
}
}
// if the word at i'th index is last character of word then the word is found
if (prefix[word_i] == '\0')
{
// the node with the prefix has been found now
// now we have to print all children of that node
printChildNodes(temp);
return;
}
// if the word at i'th index is not the last character of word
// then check if the node has a child at ith index
if (temp->children.find(prefix[word_i]) == temp->children.end())
{
// if there is no node at ith index then the word does not exists
return;
}
else
//if there is a node at ith index then search for the remaining word in the node
temp = temp -> children[prefix[word_i]];
}
}
void printChildNodes(Node* node)
{
// if the node is NULL return
if (node == NULL)
{
cout<<"The given node is NULL"<<endl;
return;
}
if (node->data != NULL)
cout<<*node->data<<endl;
// iterating over all value of umap
for (auto itr : node->children)
printChildNodes(itr.second);
}
};
int main ()
{
OptimizedTrie<string> trie;
trie.insert("helo", "helo");
trie.insert("heloooo", "heloo");
trie.insert("hi", "hi");
// trie.insert("hel", "hel");
trie.insert("hello", "hello");
trie.insert("hey", "hey");
// trie.search("he"); // prints nothing
// trie.search("hey"); // prints that the node has 5 stored in it
cout<<"\nbefore deleting: "<<endl;
trie.printChildren("h");
trie.Delete("hello");
cout<<"\nafter deleting: "<<endl;
trie.printChildren("h");
return 0;
}