-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtrie.c
337 lines (309 loc) · 8.04 KB
/
trie.c
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
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
/*
Kamil Tokarski kt361223
Dictionary on radix tree
Radix tree implementation
*/
#ifdef ASSERT
#include <assert.h>
#else
#define assert(x)
#endif
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAX_INPUT_SIZE 2*1024*1024+7
#define DEFAULT_EDGE_ARRAY_SIZE 2
struct Node;
struct Edge;
typedef struct Node *Tree;
typedef struct Edge *Edge;
struct Node{
Tree parent;
Edge *edges;
int capacity, size;
int isWord;
};
struct Edge{
Tree target;
char *beg, *end;
};
Tree root;
static char strings[MAX_INPUT_SIZE];
static char *wordsNumbers[MAX_INPUT_SIZE];
static int wordsLengths[MAX_INPUT_SIZE];
static int stringsIt; //iterator for strings[]
static int wordsCounter;
static int nodesCounter;
int parmV; //=1 if -v parm of execution
static Tree newTree(int isWord, Tree ancestor){
Tree nT = malloc(sizeof *nT);
nT->edges = malloc(sizeof *(nT->edges) * DEFAULT_EDGE_ARRAY_SIZE);
nT->size = 0;
nT->capacity = DEFAULT_EDGE_ARRAY_SIZE;
nT->isWord = isWord;
nT->parent = ancestor;
return nT;
}
static Edge newEdge(Tree target, char *beg, char *end){
Edge nE = malloc (sizeof *nE);
nE->target = target;
nE->beg = beg;
nE->end = end;
return nE;
}
static void addEdge(Tree t, Edge e){
if(t->size == t->capacity){
t->capacity *= 2;
t->edges = realloc(t->edges, (sizeof *(t->edges)) * t->capacity);
}
(t->edges)[(t->size)++] = e;
}
static void removeEdge(Tree t, int it){
if(it < t->size && t->size > 0){
free(t->edges[it]);
t->edges[it] = t->edges[t->size-1];
--(t->size);
}
}
static void destroy(Tree t){
int i;
for(i=0; i < t->size; i++){
destroy((t->edges)[i]->target);
free((t->edges)[i]);
}
free(t->edges);
free(t);
}
static void stderrwr(){
int n = (nodesCounter == 1)? 0 : nodesCounter;
if(parmV == 1)
fprintf(stderr, "nodes: %d\n", n);
}
//merges supr with edge following supr->target (must be exacly one)
//supr->target will be destroyed
static void mergeWithNextEdge(Edge supr){
Tree victim = supr->target;
assert(victim->size == 1);
supr->beg = victim->edges[0]->beg - (supr->end + 1 - supr->beg);
supr->end = victim->edges[0]->end;
supr->target = victim->edges[0]->target;
supr->target->parent = victim->parent;
free(victim->edges[0]);
victim->size = 0;
destroy(victim);
}
static int directEdge(Tree ancs, Tree desc){
int i;
for(i=0; i<ancs->size; i++){
if(ancs->edges[i]->target == desc)
return i;
}
return -1;
}
//returns position of last char in common [edge] and [beg...end] prefix or
//-1 if 0 position doesn't match
static int cmprLabels(Edge e, char *beg, char *end){
assert(beg <= end);
int i = 0;
while ((e->beg + i <= e->end) && (beg + i <= end) && beg[i] == (e->beg)[i]){
++i;
}
return i - 1;
}
//returns node representing the longest common prefix with Tree and *beg
//moves *beg to the first 'protruding' position
static Tree lastMatchedNode(char **beg, char **end){
Tree t = root;
int i=0;
while (i < t->size){
if(cmprLabels((t->edges)[i], *beg, *end) == t->edges[i]->end - t->edges[i]->beg){
*beg = *beg + ((t->edges[i])->end + 1 - (t->edges[i])->beg);
t = (t->edges)[i]->target;
i = -1;
if(*beg > *end)
return t;
}
++i;
}
return t;
}
//0 - not found, 1 - found with isWord=0 , 2 - found with isWord=1
static int exists(char *beg, char *end){
Tree node;
int i;
node = lastMatchedNode(&beg, &end);
if(beg<=end) //not whole word matched
return 0;
else
return node->isWord + 1;
}
static char *endform(char *word){
char *end = word;
while(*(end+1) != '\0')
++end;
return end;
}
static int insertion(char *beg, char *end){
int i, howFarFitted;
Tree parent, child, hub;
Edge fromParent = NULL, nEdge, fromHubToOld;
parent = lastMatchedNode(&beg, &end);
if(beg > end){
parent->isWord = 1;
printf("word number: %d\n", wordsCounter-1);
stderrwr();
return 0;
}
for(i=0; i<parent->size; ++i){ //looking for partially matching edge
howFarFitted=cmprLabels((parent->edges)[i], beg, end);
if(howFarFitted != -1){
assert(howFarFitted <= end-beg);
fromParent = (parent->edges)[i];
break;
}
}
if(fromParent == NULL){ //there's no edge with common prefix
child = newTree(1, parent);
++nodesCounter;
nEdge = newEdge(child, beg, end);
addEdge(parent, nEdge);
}
else if(howFarFitted == end-beg){ //inserting node between parent and fromParent->target
child = newTree(1, parent);
++nodesCounter;
nEdge = newEdge(fromParent->target, fromParent->beg + howFarFitted + 1, fromParent->end);
addEdge(child, nEdge);
fromParent->target->parent = child;
fromParent->end = fromParent->beg + howFarFitted;
fromParent->target = child;
}
else{ //splitting fromParent edge
hub = newTree(0, parent);
child = newTree(1, hub);
nodesCounter+=2;
fromHubToOld = newEdge(fromParent->target, fromParent->beg+howFarFitted+1, fromParent->end);
fromParent->target->parent = hub;
addEdge(hub, fromHubToOld);
fromParent->end = fromParent->beg + howFarFitted;
fromParent->target = hub;
nEdge = newEdge(child, beg+howFarFitted+1, end);
addEdge(hub, nEdge);
}
printf("word number: %d\n", wordsCounter-1);
stderrwr();
return 0;
}
int initTree(){
root = newTree(0, root);
nodesCounter = 1;
}
int insert(char *word){
int length;
char *beg, *end;
if(root == NULL)
initTree();
beg = word; end = endform(word);
if(exists(beg, end) == 2) return -1; //ignore an attempt to insert already inserted word
//insert word to global array
length=0;
beg = strings + stringsIt;
while(word[length] != '\0'){
beg[length] = word[length];
++length;
}
stringsIt = stringsIt + length;
wordsNumbers[wordsCounter] = beg;
wordsLengths[wordsCounter++] = length;
end = beg + length - 1;
assert(beg <= end);
return insertion(beg, end);
}
int prev(int number, int start, int end){
char *begS, *endS;
if(root == NULL)
initTree();
if(number >= wordsCounter || start > end || end >= wordsLengths[number])
return -1;
begS = wordsNumbers[number]+start;
endS = wordsNumbers[number]+end;
if(exists(begS, endS) == 2)
return -1;
wordsNumbers[wordsCounter] = begS;
wordsLengths[wordsCounter++] = end - start + 1;
insertion(begS, endS);
}
int find(char *word){
Tree nearest;
char *beg = word, *end = endform(word);
int i;
if(root == NULL)
initTree();
nearest = lastMatchedNode(&beg, &end);
if(beg>end){
printf("YES\n");
return 1;
}
for(i=0; i<nearest->size; ++i){ //looking for partially matching edge
if(cmprLabels((nearest->edges)[i], beg, end) == end - beg){
printf("YES\n");
return 1;
}
}
printf("NO\n");
return 0;
}
int delete(int number){
Tree grandparent, parent, victim;
Edge parentToVictim;
int iterE;
char *beg, *end;
if(root == NULL)
initTree();
if(number >= wordsCounter || wordsNumbers[number] == NULL)
return -1;
beg = wordsNumbers[number]; end = beg + wordsLengths[number] - 1;
wordsNumbers[number] = NULL;
wordsLengths[number] = 0;
assert(exists(beg, end) == 2);
victim = lastMatchedNode(&beg, &end);
parent = victim->parent;
iterE = directEdge(parent, victim);
assert(iterE != -1);
parentToVictim = parent->edges[iterE];
if(victim->size > 1){//breadwinner, make it hub
victim->isWord = 0;
}
else if(victim->size == 1){ //only child, merge edges, remove victim
mergeWithNextEdge(parentToVictim);
--nodesCounter;
}
else{//no children, look up if hub
destroy(victim);
removeEdge(parent, iterE);
--nodesCounter;
if(parent->size == 1 && parent->isWord == 0 && parent != root){
grandparent = parent->parent;
iterE = directEdge(grandparent, parent);
assert(iterE != -1);
mergeWithNextEdge(grandparent->edges[iterE]);
--nodesCounter;
}
}
printf("deleted: %d\n", number);
stderrwr();
return 0;
}
int clear(){
clear_s();
printf("cleared\n");
stderrwr();
return 0;
}
int clear_s(){
if(root == NULL)
initTree();
destroy(root);
root = NULL;
stringsIt = wordsCounter = nodesCounter = 0;
return 0;
}