-
Notifications
You must be signed in to change notification settings - Fork 0
/
trie.h
219 lines (171 loc) · 4.45 KB
/
trie.h
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
#include <stdio.h>
#include <pthread.h>
#include <unistd.h>
#include <dirent.h>
#include <sys/stat.h>
#include <time.h>
#include <string.h>
#include <stdlib.h>
#define MAX_WORD_LEN 64 //najveca dozvoljena duzina reci, uz \0
#define LETTERS 26 //broj slova u abecedi i broj dece u trie
#define prefix pthread_mutex_t
#define uzmi_prefix pthread_mutex_lock
#define spusti_prefix pthread_mutex_unlock
extern char prefiks[MAX_WORD_LEN];
extern int active_printing;
struct trie_node *root;
char *ans, *p;
typedef struct trie_node //cvor unutar trie strukture
{
char c; //slovo ovog cvora
int notification;
int endOfWord; //flag za kraj reci
int subwords; //broj reci u podstablu, ne racunajuci sebe
struct trie_node *parent; //pokazivac ka roditelju
struct trie_node *children[LETTERS]; //deca
pthread_mutex_t prefix;
}trie_node;
typedef struct search_result //rezultat pretrage
{
int result_count; //duzina niza
char **words; //niz stringova, svaki string duzine MAX_WORD_LEN
} search_result;
trie_node*
createNode(trie_node *parent, char c){
struct trie_node *newNode;
newNode = malloc(sizeof(trie_node));
newNode->notification = 0;
newNode->c = c;
newNode->parent = parent;
newNode->subwords = 0;
newNode->endOfWord = 0;
pthread_mutex_init(&newNode->prefix, NULL);
for(int i = 0 ; i < LETTERS ; i++)
newNode->children[i] = NULL;
return newNode;
}
void
insert(trie_node* node, char *str, int i)
{
if(i < strlen(str)){
int index = str[i] - 'a';
if(node->children[index] == NULL)
node->children[index] = createNode(node, str[i]);
uzmi_prefix(&node->prefix); // mutex_lock
insert(node->children[index], str, i + 1);
if(node->notification == 1){
node->subwords ++;
node->notification = 0;
if(node->parent != NULL)
node->parent->notification = 1;
else
root->notification = 1;
}
spusti_prefix(&node->prefix); // mutex_unlock
}else{
if(node->endOfWord == 1)
return;
// nova rec u stablu
node->parent->notification = 1;
node->endOfWord = 1;
}
}
//operacija za dodavanje reci
void
trie_add_word(char *word)
{
insert(root, word, 0);
if(root->notification == 1)
{
if(active_printing == 1 && strlen(prefiks) > 0 && strncmp(prefiks, word, strlen(prefiks)) == 0)
printf("%s\n", word);
root->notification = 0;
}
}
void
traverse(trie_node* node, char *str, int x)
{
for(int i=0 ; i < LETTERS ; i++){
if(node->children[i] == NULL)
continue;
if(node->children[i]->endOfWord == 1){
str[x] = i + 'a';
strncpy(ans, str, MAX_WORD_LEN - 1);
ans[MAX_WORD_LEN - 1] = '\0';
ans += MAX_WORD_LEN;
}
else
str[x] = i + 'a';
traverse(node->children[i], str, x + 1);
str[x] = 0;
}
}
//operacija za pretragu ; neophodno je proveriti da li se na prefiksu nalazi endOfWord, da bi smo znali da li i tu rec da racunamo
search_result
*trie_get_words(char *prefix)
{
int k = 0;
struct trie_node *tmp = root;
for(int i=0 ; i < strlen(prefix) ; i++){
k = prefix[i] - 'a';
if(tmp->children[k] == NULL)
return NULL;
tmp = tmp->children[k];
}
uzmi_prefix(&tmp->prefix); // mutex_lock
struct search_result *result = malloc(sizeof(search_result));
result->result_count = tmp->subwords;
if(tmp->endOfWord)
result->result_count ++;
p = malloc(result->result_count*(sizeof(char))*MAX_WORD_LEN);
ans = p;
result->words = &p;
// ako je trazeni prefix rec u stablu
if(tmp->endOfWord){
strncpy(ans, prefix, MAX_WORD_LEN - 1);
ans[MAX_WORD_LEN - 1] = '\0';
ans += MAX_WORD_LEN;
}
char str[MAX_WORD_LEN];
int x = strlen(prefix);
strncpy(str, prefix, strlen(prefix));
traverse(tmp, str, x);
spusti_prefix(&tmp->prefix); // mutex_unlock
return result;
}
//rezultat se dinamicki alocira pri pretrazi, tako da treba da postoji funkcija za oslobadjanje tog rezultata
void
trie_free_result(search_result *result)
{
free((char*)(*result->words));
}
void
solve(trie_node* node)
{
if(node == NULL)
return;
for(int i=0 ; i<26 ; i++)
{
pthread_mutex_destroy(&node->prefix);
solve(node->children[i]);
}
}
void
destroy_mutex_in_trie()
{
solve(root);
}
void
trie_init()
{
root = malloc(sizeof(trie_node));
root->notification = 0;
root->c = 0;
root->parent = NULL;
root->subwords = 0;
root->endOfWord = 0;
pthread_mutex_init(&root->prefix, NULL);
for(int i = 0 ; i < LETTERS ; i++){
root->children[i] = NULL;
}
}