-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtrie.cpp
More file actions
213 lines (189 loc) · 6.74 KB
/
trie.cpp
File metadata and controls
213 lines (189 loc) · 6.74 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
#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]];
}
}
}
void search(string word)
{
int word_i = 0;
Node* temp = root;
// if temp is NULL then don't continue
while(temp != NULL)
{
int temp_i = 0;
// loop through every character in the current node
while (temp->identifier[temp_i] != '\0')
{
if (temp->identifier[temp_i] == word[word_i])
{
temp_i++;
word_i++;
}
else
{
cout<<word<<" does not exists"<<endl;
return;
}
}
// if the word at i'th index is last character of word then the word is found
if (word[word_i] == '\0')
{
cout<<"the data in the "<<word<<" is "<<*temp->data<<endl;
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(word[word_i]) == temp->children.end())
{
// if there is no node at ith index then the word does not exists
cout<<word<<" does not exists"<<endl;
return;
}
else
//if there is a node at ith index then search for the remaining word in the node
temp = temp -> children[word[word_i]] -> second;
}
}
// 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
{
cout<<prefix<<" does not exists"<<endl;
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
cout<<prefix<<" does not exists"<<endl;
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);
}
};