-
Notifications
You must be signed in to change notification settings - Fork 1
/
trie.cpp
238 lines (198 loc) · 7.03 KB
/
trie.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
#include "trie.h"
#include "helping_funs.h"
my_trie::my_trie()
{
first = NULL;
trie_size = 0 ;
}
my_trie::~my_trie()
{
destroy(first); //destroy trie recursively starting from its root
}
void my_trie::destroy(node * n)
{
if (n != NULL)
{
destroy(n->down);
destroy(n->right);
if(n->pl_ptr != NULL)
{ //delete node's posting list before deleting the node itself
delete n->pl_ptr;
}
delete n;
trie_size--;
}
}
my_trie::node * my_trie::create_node(char ch)
{
node * new_node = new node;
new_node->ch = ch;
new_node->down = NULL;
new_node->right = NULL;
new_node->pl_ptr = NULL;
return new_node;
}
unsigned int my_trie::insert_docs(char ** docs, unsigned int nlines, unsigned int dir_index, unsigned int text_file_index)
{//inserting every doc's word into trie, counting each doc's number of words - returns total number of all docs' words
unsigned int total_word_counter = 0;
for(unsigned int i = 0; i < nlines; i++)
{ //for each doc
char * line_cp = (char *) malloc((strlen(docs[i]) + 1) * sizeof(char));
strcpy(line_cp, docs[i]);
char * word = strtok (line_cp, " \t");
unsigned int line_word_counter = 0;
while (word != NULL)
{//for each doc's word
line_word_counter++;
insert_word(word, dir_index, text_file_index, i);
word = strtok (NULL, " \t");
}
free(line_cp);
total_word_counter += line_word_counter;
}
return total_word_counter;
}
void my_trie::insert_word(char * word, unsigned int dir_index, unsigned int text_file_index, unsigned int line_index)
{ // inseting a word into the trie or updating its posting list if word is already contained
// every orizontal list is sorted alphabetically
node * parent_node = NULL; //will indicate the parent of the first node of an orizontal list
node * start_node = first; //will indicate the first node of an orizontal list
for(unsigned int i = 0; i < strlen(word); i++)
{ //iterating word letter by letter
node * prev_temp = NULL; // will be used for iterating an orizontal list
node * temp = start_node; // will be used for iterating an orizontal list
node * n = NULL;
bool reallocate_parent_flag = 0;
if(temp == NULL)
{ // a new orizontal list will be created
n = create_node(word[i]); //this will be its first node
reallocate_parent_flag = 1;
}
else
{ // a orizontal list already exists
while(1) //iterating the orizontal list
{
if(temp == NULL)
{ //we have iterated the whole list and no same letter has been found. new letter will be added at the end of the list
n = create_node(word[i]);
prev_temp->right = n;
break;
}
if(temp->ch == word[i])
{ // we have found a same letter
parent_node = temp;
start_node = temp->down; //we will continue looking downwards
break;
}
else if(temp->ch < word[i])
{// keep searching the orizontal list
prev_temp = temp;
temp = temp->right;
}
else
{// no same letter found so far and no same letter will be found in this orizontal list because it is sorted alphabetically
// and current letter is bigger than the inserting one
n = create_node(word[i]);
n->right = temp;
if(prev_temp == NULL) //adding new letter at the start of the orizontal list
{
reallocate_parent_flag = 1;
}
else //adding new letter in the middle of the orizontal list
{
prev_temp->right = n ;
}
break;
}
}
}
if(n != NULL) // a new node has been created (a new letter has been added)
{ // checking if parent's node down pointer needs to be reallocated
if (reallocate_parent_flag == 1)
{
if (parent_node != NULL)
{
parent_node->down = n;
}
else
{
first = n;
}
}
trie_size++;
parent_node = n;
start_node = n->down; //NULL
}
}
//all letters of the inserting word have been consumed
if(parent_node->pl_ptr == NULL)
{ // if the last's letter node does not have an existing posting list (inserting word is not contained in the trie)
parent_node->pl_ptr = new posting_list(); //create one
}
parent_node->pl_ptr->update(word, dir_index, text_file_index, line_index); //inserting word already exists, therefore update its posting list
}
void my_trie::print(dir ** dirs, bool full_print)
{
if(first == NULL)
{
cout<<"-> Trie cannot be printed because it is empty !"<<endl;
return;
}
if(full_print == 1)
{
cout<<"-----> Printing whole trie <-----"<<endl;
}
else
{
cout<<"---> Printing document frequency vector <---"<<endl;
}
print_subtrie(dirs, first, full_print); //print trie recursively starting from the root
}
void my_trie::print_subtrie(dir ** dirs, node * n, bool full_print)
{
if (n != NULL)
{
if(n->pl_ptr != NULL)
{
n->pl_ptr->print(dirs, full_print);
}
print_subtrie(dirs, n->down, full_print);
print_subtrie(dirs, n->right, full_print);
}
}
posting_list * my_trie::search_word(const char * query)
{ //if word exists in the trie return its posting list or NULL otherwise
node * start_node = first;
node * last_char_node = NULL;
for(unsigned int i = 0; i < strlen(query); i++)
{ //iterating query word letter by letter
node * temp = start_node;
while(1)
{
if (temp == NULL) //we have interated the whole orizontal list and a query word's letter does not exist,
// therefore the whole word does not exist
{
return NULL; //query not found
}
if(temp->ch == query[i])
{// a query word's letter has been found so keep searching downwards
last_char_node = temp;
start_node = temp->down;
break;
}
else if(temp->ch > query[i])
{//stop searching. orizontal list is sorted alphabetically
return NULL; // query not found
}
else
{//keep iterating the orizontal list
temp = temp->right;
}
}
}
return last_char_node->pl_ptr;
}
unsigned int my_trie::get_size()
{
return trie_size;
}