-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathretrie.c
376 lines (348 loc) · 10.1 KB
/
retrie.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
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "retrie.h"
#include "posting_list.h"
int total_word_counter = 0;
//head node for map list
struct trie_node *trie_node_head = NULL;
//make a new trie node and zero initialize them
struct trie_node *new_trie_node(char character)
{
struct trie_node *trie_node_current = (struct trie_node *) malloc(sizeof(struct trie_node));
if (trie_node_current == NULL)
{
printf("Not enough memory in heap\n");
exit(EXIT_FAILURE);
}
trie_node_current->key = character;
trie_node_current->last_text_id = 0;
trie_node_current->line_id_counter = 0;
trie_node_current->right_node = NULL;
trie_node_current->down_node = NULL;
trie_node_current->post_list_head = NULL;
trie_node_current->end = NO;
trie_node_current->printed = NO;
return trie_node_current;
}
void clean_trie(void)
{
free_trie(trie_node_head);
}
void free_trie(struct trie_node *root)
{
if (root->right_node != NULL)
{
free_trie(root->right_node);
}
if (root->down_node != NULL)
{
free_trie(root->down_node);
}
if (root->post_list_head != NULL)
{
free_post_list(root->post_list_head);
}
free(root);
}
void reinit_df (struct trie_node *root)
{
if (root == NULL)
{
return;
}
if (root->right_node != NULL)
{
reinit_df(root->right_node);
}
if (root->down_node != NULL)
{
reinit_df(root->down_node);
}
if (root->end == YES)
{
//Reinitialize printed flag
root->printed = YES;
}
}
void df_print_retrie(void)
{
print_retrie(trie_node_head);
reinit_df (trie_node_head);
}
void print_retrie(struct trie_node *root)
{
if (root == NULL)
{
return;
}
if (root->right_node != NULL)
{
print_retrie(root->right_node);
}
if (root->down_node != NULL)
{
print_retrie(root->down_node);
}
if (root->printed == YES)
{
//printf("%s %d\n", root->post_list_head->actual_word, root->line_id_counter);
//Set printed flag to NO so that we don't print it again
root->printed = NO;
}
}
struct trie_node *search_char_to_trie(struct trie_node *root, char character, int i)
{
static int previous;
if (root == NULL)
{
return NULL;
}
if (i==0)
{//for the first stage only
while (1)
{
//character is here
if (root->key == character)
{
return root;
}
//character isn't here but I have more right nodes
else if ((root->key != character) && (root->right_node != NULL))
{
root = root->right_node;
}
//character isn't here and I don't have more right nodes
else
{
return NULL;
}
}
}
else
{//for other stages
previous = FOUND;
while (1)
{
//previous found
if (previous == FOUND)
{
if (root->down_node != NULL)
{
if (root->down_node->key == character)
{
previous = FOUND;
return root->down_node;
}
else
{
if (root->down_node->right_node != NULL)
{
root = root->down_node->right_node;
previous = NOT_FOUND;
}
else
{
previous = NOT_FOUND;
return NULL;
}
}
}
else
{
previous = NOT_FOUND;
return NULL;
}
}
else//previous not found
{
if (root->key == character)
{
previous = FOUND;
return root;
}
else
{
if (root->right_node != NULL)
{
previous = NOT_FOUND;
root = root->right_node;
}
else
{
previous = NOT_FOUND;
return NULL;
}
}
}
}
}
}
struct trie_node *search_word_to_trie(struct word *current_word, struct trie_node *trie_node_head)
{
struct trie_node *trie_node_current = trie_node_head;
if (current_word == NULL)
{
return NULL;
}
for (int i=0; i < current_word->word_length; i++)
{
if (current_word->actual_word[i] != '\0')
{
trie_node_current = search_char_to_trie(trie_node_current, current_word->actual_word[i], i);
}
if (trie_node_current == NULL)
{
return NULL;
}
}
if (trie_node_current->end == YES)
{
return trie_node_current;
}
else
{
return NULL;
}
}
//int get_line_id_counter(struct word *current_word)
//{
// return (search_word_to_trie(current_word, )->line_id_counter);
//}
struct trie_node * insert_char_to_trie(struct trie_node *root, char character, int i)
{
static int previous = NOT_FOUND;
if (root == NULL)
{
trie_node_head = new_trie_node(character);
return (trie_node_head);
}
//for first stage only
if (i==0)
{
while (1)
{
//character is here
if (root->key == character)
{
return root;
}
//character isn't here but I have more right nodes
else if ((root->key != character) && (root->right_node != NULL))
{
root = root->right_node;
continue;
}
//character isn't here and I don't have more right nodes (create one and initialize it)
else
{
root->right_node = new_trie_node(character);
return root->right_node;
}
}
}
//for other stages
else
{
previous = FOUND;
while (1)
{
//previous found
if (previous == FOUND)
{
if (root->down_node != NULL)
{
if (root->down_node->key == character)
{
previous = FOUND;
return root->down_node;
}
else
{
if (root->down_node->right_node != NULL)
{
root = root->down_node->right_node;
previous = NOT_FOUND;
}
else
{
root->down_node->right_node = new_trie_node(character);
previous = FOUND;
return root->down_node->right_node;
}
}
}
else
{
root->down_node = new_trie_node(character);
previous = FOUND;
return root->down_node;
}
}
//previous not found
else
{
if (root->key == character)
{
previous = FOUND;
return root;
}
else
{
if (root->right_node != NULL)
{
previous = NOT_FOUND;
root = root->right_node;
}
else
{
root->right_node = new_trie_node(character);
previous = FOUND;
return root->right_node;
}
}
}
}
}
}
void insert_word_to_trie(struct word *current_word)
{
//from the root
struct trie_node *trie_node_current = trie_node_head;
for (int i=0; i < current_word->word_length; i++)
{
if (current_word->actual_word[i] != '\0')
{
trie_node_current = insert_char_to_trie(trie_node_current, current_word->actual_word[i], i);
}
}
if (trie_node_current->last_text_id == 0 && current_word->number_of_line == 0 && trie_node_current->line_id_counter == 0)
{
trie_node_current->line_id_counter++;
}
else if (trie_node_current->last_text_id != current_word->number_of_line)
{
trie_node_current->line_id_counter++;
trie_node_current->last_text_id = current_word->number_of_line;
}
//in terminating nodes flags "end" and "printed" are YES
trie_node_current->end = trie_node_current->printed = YES;
trie_node_current->post_list_head = update_post_list(trie_node_current->post_list_head,current_word);
}
struct trie_node *load_retrie(void)
{
struct word *current_word;
set_word();
while (1)
{
current_word = get_word();
if (current_word == NULL)
{
break;
}
total_word_counter++;
insert_word_to_trie(current_word);
free(current_word);
}
return trie_node_head;
}