-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPatriciaTrie.cpp
317 lines (313 loc) · 11.3 KB
/
PatriciaTrie.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
#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
#include <stack>
#include <random>
#include <chrono>
using namespace std;
class PatriciaTrie {
private:
int m_size = 0;
string* lastLowerBound = nullptr;
string* lastUpperBound = nullptr;
struct Node {
Node(const string& label) {
isWord = false;
this->label = label;
}
vector<Node*> next;
string label;
bool isWord;
};
Node* m_root = new Node("$");
int matchConsecutive(const string& s1, const string& s2) {
int N = min(s1.size(), s2.size());
int count = 0;
for (size_t i = 0; i < N; i++) {
if (s1[i] == s2[i]) count++;
else break;
}
return count;
}
Node* put(Node* n, const string& key) {
int matchN = matchConsecutive(n->label, key);
if (matchN == key.size() && matchN == n->label.size()) {
if (!n->isWord) {
m_size++;
n->isWord = true;
}
} else if (matchN == key.size() && n->label.size() > key.size()) {
string leftPart = n->label.substr(0, matchN);
string rightPart = n->label.substr(matchN, n->label.size() - matchN);
Node* newNode = new Node(leftPart); // new Node(key)
m_size++;
newNode->isWord = true;
n->label = rightPart;
newNode->next.emplace_back(n);
n = newNode;
} else if (matchN == n->label.size() && key.size() > n->label.size()) {
string rightKeyPart = key.substr(matchN, key.size() - matchN);
bool isPresent = false;
for (Node*& x : n->next) {
if (x->label[0] == rightKeyPart[0]) {
isPresent = true;
x = put(x, rightKeyPart);
}
}
if (!isPresent) {
Node* newNode = new Node(rightKeyPart);
m_size++;
newNode->isWord = true;
n->next.emplace_back(newNode);
}
} else {
string commonPrefix = key.substr(0, matchN);
Node* newNode = new Node(commonPrefix);
string restOfLabel = n->label.substr(matchN, n->label.size() - matchN);
string restOfKey = key.substr(matchN, key.size() - matchN);
n->label = restOfLabel;
newNode->next.emplace_back(n);
Node* keyNode = new Node(restOfKey);
m_size++;
keyNode->isWord = true;
newNode->next.emplace_back(keyNode);
n = newNode;
}
return n;
}
void collect(Node* n, string soFar, vector<string>& words) {
for (Node* x : n->next) {
if (x->isWord) words.emplace_back(soFar + x->label);
collect(x, soFar + x->label, words);
}
}
Node* remove(Node* n, const string& key) {
int matchN = matchConsecutive(n->label, key);
if (matchN == n->label.size() && matchN == key.size()) {
m_size--;
if (n->next.size() == 0) {
delete n;
n = nullptr;
} else {
n->isWord = false;
}
} else if (matchN == n->label.size() && n->label.size() < key.size()) {
string restOfKey = key.substr(matchN, key.size() - matchN);
bool isPresent = false;
int i = 0;
for (; i < n->next.size(); ++i) {
if (n->next[i]->label[0] == restOfKey[0]) {
isPresent = true;
n->next[i] = remove(n->next[i], restOfKey);
break;
}
}
if (isPresent && n->next[i] == nullptr) {
n->next.erase(n->next.begin() + i);
}
}
return n;
}
bool contains(Node* n, const string& keyPart) {
if (keyPart.size() < n->label.size()) return false;
int matchN = matchConsecutive(n->label, keyPart);
if (matchN == n->label.size() && matchN == keyPart.size()) {
return n->isWord;
} else if (matchN >= n->label.size() && keyPart.size() > n->label.size()) {
string restOfKey = keyPart.substr(matchN, keyPart.size() - matchN);
bool isPresent = false;
for (Node* x : n->next) {
if (x->label[0] == restOfKey[0]) {
isPresent = true;
return contains(x, restOfKey);
}
}
if (!isPresent) {
return false;
}
}
return false;
}
string getMax(Node* n) {
if (n == nullptr) return "";
string result = n->label;
Node* crawler = n;
while (crawler->next.size() > 0) {
Node* maxLabelNode = nullptr;
for (Node* x : crawler->next) if (maxLabelNode == nullptr || maxLabelNode->label[0] < x->label[0]) maxLabelNode = x;
result += maxLabelNode->label;
crawler = maxLabelNode;
}
return result;
}
string getMinWrong(Node* n) { // wrong
if (n == nullptr) return "";
string result = n->label;
Node* crawler = n;
while (crawler->next.size() > 0) {
Node* minLabelNode = nullptr;
for (Node* x : crawler->next) if (minLabelNode == nullptr || minLabelNode->label[0] > x->label[0]) minLabelNode = x;
result += minLabelNode->label;
crawler = minLabelNode;
}
return result;
}
string getMin(Node* n) {
if (n == nullptr) return "";
string result = n->label;
Node* crawler = n;
while (!crawler->isWord) {
Node* minLabelNode = nullptr;
for (Node* x : crawler->next) if (minLabelNode == nullptr || minLabelNode->label[0] > x->label[0]) minLabelNode = x;
crawler = minLabelNode;
result += crawler->label;
}
return result;
}
string* lowerBound(Node* n, Node* prevNodeWithASmallerChar, string soFarBeforePrevNode, string prevKey, const string& keyPart, string soFar) {
string tempPrevKey = prevKey;
int matchN = matchConsecutive(n->label, keyPart);
if (n->isWord) prevKey = soFar + n->label;
if (matchN == keyPart.size() && matchN == n->label.size()) {
if (n->isWord) return new string(prevKey.substr(1, prevKey.size() - 1));
else if (prevKey.size() > soFarBeforePrevNode.size()) return new string(prevKey.substr(1, prevKey.size() - 1));
else if (prevNodeWithASmallerChar != nullptr) return new string(soFarBeforePrevNode.substr(1, soFarBeforePrevNode.size() - 1) + getMax(prevNodeWithASmallerChar));
} else if (matchN == n->label.size() && keyPart.size() > n->label.size()) {
string restOfKey = keyPart.substr(matchN, keyPart.size() - matchN);
Node* lowerBoundNode = nullptr;
for (Node* x : n->next) if (x->label[0] < restOfKey[0] && (lowerBoundNode == nullptr || lowerBoundNode->label[0] < x->label[0])) lowerBoundNode = x;
if (lowerBoundNode != nullptr) {
prevNodeWithASmallerChar = lowerBoundNode;
soFarBeforePrevNode = soFar + n->label;
}
for (Node* x : n->next) if (x->label[0] == restOfKey[0]) return lowerBound(x, prevNodeWithASmallerChar, soFarBeforePrevNode, prevKey, restOfKey, soFar + n->label);
if (prevKey.size() > soFarBeforePrevNode.size()) return new string(prevKey.substr(1, prevKey.size() - 1)); // might be incorrect
else if (prevNodeWithASmallerChar != nullptr) return new string(soFarBeforePrevNode.substr(1, soFarBeforePrevNode.size() - 1) + getMax(prevNodeWithASmallerChar));
} else if (matchN == keyPart.size() && n->label.size() > keyPart.size()) {
if (tempPrevKey.size() > soFarBeforePrevNode.size()) return new string(tempPrevKey.substr(1, tempPrevKey.size() - 1));
else if (prevNodeWithASmallerChar != nullptr) return new string(soFarBeforePrevNode.substr(1, soFarBeforePrevNode.size() - 1) + getMax(prevNodeWithASmallerChar));
} else {
char fromLabel = n->label[matchN];
char fromKeyPart = keyPart[matchN];
if (fromLabel < fromKeyPart) {
Node* nodeWithMaxLabel = nullptr;
for (Node* x : n->next) if (nodeWithMaxLabel == nullptr || nodeWithMaxLabel->label[0] < x->label[0]) nodeWithMaxLabel = x;
string temp = soFar + n->label;
return new string(temp.substr(1, temp.size() - 1) + getMax(nodeWithMaxLabel));
} else if (prevNodeWithASmallerChar != nullptr) return new string(soFarBeforePrevNode.substr(1, soFarBeforePrevNode.size() - 1) + getMax(prevNodeWithASmallerChar));
}
return nullptr;
}
string* upperBound(Node* n, Node* prevNodeWithALargerChar, string soFarBeforePrevNode, const string& keyPart, string soFar) {
int matchN = matchConsecutive(n->label, keyPart);
if (matchN == n->label.size() && matchN == keyPart.size()) {
if (n->isWord) {
string temp = soFar+n->label;
return new string(temp.substr(1, temp.size() - 1));
} else if (n->next.size() > 0) {
string temp = soFar + getMin(n);
return new string(temp.substr(1, temp.size() - 1));
} else if (prevNodeWithALargerChar != nullptr) return new string(soFarBeforePrevNode.substr(1, soFarBeforePrevNode.size() - 1) + getMin(prevNodeWithALargerChar));
} else if (matchN == n->label.size() && keyPart.size() > n->label.size()) {
string restOfKey = keyPart.substr(matchN, keyPart.size() - matchN);
Node* upperBoundNode = nullptr;
for (Node* x : n->next) if (x->label[0] > restOfKey[0] && (upperBoundNode == nullptr || upperBoundNode->label[0] > x->label[0])) upperBoundNode = x;
if (upperBoundNode != nullptr) {
prevNodeWithALargerChar = upperBoundNode;
soFarBeforePrevNode = soFar + n->label;
}
for (Node* x : n->next) if (x->label[0] == restOfKey[0]) return upperBound(x, prevNodeWithALargerChar, soFarBeforePrevNode, restOfKey, soFar + n->label); // must be larger!
if (prevNodeWithALargerChar != nullptr) {
if (n->label == "$") soFarBeforePrevNode = "$";
return new string(soFarBeforePrevNode.substr(1, soFarBeforePrevNode.size() - 1) + getMin(prevNodeWithALargerChar));
}
} else if (matchN == keyPart.size() && n->label.size() > keyPart.size()) return new string(soFar.substr(1, soFar.size() - 1) + getMin(n));
char fromLabel = n->label[matchN];
char fromKeyPart = keyPart[matchN];
if (fromLabel > fromKeyPart) return new string(soFar.substr(1, soFar.size() - 1) + getMin(n));
else if (prevNodeWithALargerChar != nullptr) return new string(soFarBeforePrevNode.substr(1, soFarBeforePrevNode.size() - 1) + getMin(prevNodeWithALargerChar));
return nullptr;
}
public:
void put(const string& key) {
m_root = put(m_root, "$" + key);
}
void remove(const string& key) {
m_root = remove(m_root, "$" + key);
if (m_root == nullptr) m_root = new Node("$");
}
string* lowerBound(const string& key) {
delete lastLowerBound;
lastLowerBound = lowerBound(m_root, nullptr, "", "", "$" + key, "");
return lastLowerBound;
}
string* upperBound(const string& key) {
delete lastUpperBound;
lastUpperBound = upperBound(m_root, nullptr, "", "$" + key, "");
return lastUpperBound;
}
bool contains(const string& key) {
return contains(m_root, "$" + key);
}
vector<string> strings() {
vector<string> result;
if (m_root->isWord) result.emplace_back("");
collect(m_root, "", result);
return result;
}
int size() {
return m_size;
}
~PatriciaTrie() {
Node* x = m_root;
vector<Node*> nodes;
stack<Node*> dfsStack;
dfsStack.push(m_root);
while (!dfsStack.empty()) {
Node* pop = dfsStack.top(); dfsStack.pop();
nodes.emplace_back(pop);
for (Node* n : pop->next) {
dfsStack.push(n);
}
}
for (Node* n : nodes) delete n;
delete lastLowerBound;
delete lastUpperBound;
}
};
random_device rd;
mt19937 gen(rd());
uniform_int_distribution<> dis('a', 'z');
uniform_real_distribution<> disReal(0, 1);
string randomString(int size) {
string s;
for (size_t i = 0; i < size; i++) {
s += ((char)dis(gen));
}
return s;
}
vector<string> randomStrings(int count, int sizeBound) {
vector<string> result(count);
for (size_t i = 0; i < count; i++) {
int size = 1 + (int)(disReal(gen) * sizeBound);
result[i] = randomString(size);
}
return result;
}
int main() {
vector<string> words = { "she","sells","seashells","by","the","seashore","the","shells","she","shells","are","surely","seashells" };
PatriciaTrie pt;
for (const string& s : words) {
pt.put(s);
}
vector<string> strings = pt.strings();
sort(strings.begin(), strings.end());
cout << "sorted strings : " << endl;
for (const string& s : strings) cout << s << endl;
cout << "\n\n\n\n\n";
string* str_ptr = pt.upperBound("surelyy");
if (str_ptr != nullptr)
cout << *str_ptr;
else cout << "no such element";
}