-
Notifications
You must be signed in to change notification settings - Fork 0
/
Tema2.c
292 lines (231 loc) · 6.5 KB
/
Tema2.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
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define BUFLEN 1024
#define ELEMENT_TREE_LENGTH 3
#include "AVLTree.h"
typedef struct Range{
int *index;
int size;
int capacity;
}Range;
void printFile(char* fileName){
if(fileName == NULL) return;
FILE * file = fopen(fileName,"r");
if (file == NULL) return;
char *buf = (char*) malloc(BUFLEN+1);
while(fgets(buf,BUFLEN,file) != NULL){
printf("%s",buf);
}
printf("\n");
free(buf);
fclose(file);
}
void printWordsInRangeFromFile(Range* range, char* fileName){
if(fileName == NULL || range == NULL) return;
FILE * file = fopen(fileName,"r");
if (file == NULL) return;
char *buf = (char*) malloc(BUFLEN+1);
for(int i = 0; i < range->size;i++){
fseek(file,range->index[i],SEEK_SET);
if(fgets(buf,BUFLEN,file) != NULL){
char* token = strtok(buf," .,\n");
printf("%d. %s:%d\n",i+1, token, range->index[i]);
}
}
printf("\n");
free(buf);
fclose(file);
}
void printTreeInOrderHelper(TTree* tree, TreeNode* node){
if(node != NULL){
printTreeInOrderHelper(tree, node->lt);
TreeNode* begin = node;
TreeNode* end = node->end->next;
while(begin != end){
printf("%d:%s ",*((int*)begin->info),((char*)begin->elem));
begin = begin->next;
}
printTreeInOrderHelper(tree, node->rt);
}
}
void printTreeInOrder(TTree* tree){
if(tree == NULL) return;
printTreeInOrderHelper(tree, tree->root);
}
void* createStrElement(void* str){
// TODO: Cerinta 2
/* Allocate a string element to store in the tree.
*/
char* s = malloc(4*sizeof(char));
// Save the first 3 characters in the element.
strncpy(s, (char*) str, 3);
s[3] = '\0';
return s;
}
void destroyStrElement(void* elem){
// TODO: Cerinta 2
/* Free a string element.
*/
free((char*)elem);
elem = NULL;
}
void* createIndexInfo(void* index){
// TODO: Cerinta 2
/* Allocate space to store a new index
*/
int *l = malloc(sizeof(int));
// Save the index.
*l = *((int*) (index));
return (void*) l;
}
void destroyIndexInfo(void* index){
// TODO: Cerinta 2
/* Free storage space for the index.
*/
free((int*)index);
index = NULL;
}
int compareStrElem(void* str1, void* str2){
// TODO: Cerinta 2
/* 0 means strings are equal
* -1 means str1 < str2
* 1 means str1 > str2
*/
int cmp = strcmp((char*) str1, (char*) str2);
if (cmp < 0) return -1; // str1 < str2
if (cmp > 0) return 1; // str1 > str2
return 0; // str1 == str2
}
TTree* buildTreeFromFile(char* fileName){
// TODO: Cerinta 2
/* Parse the text file keeping track of indices!
* Words separators are the following: " :.,\r\n"
* At each step insert the <token, index> pair in the tree.
*/
FILE * file = fopen(fileName,"r");
if (file == NULL) return NULL;
TTree* tree = createTree(createStrElement, destroyStrElement,
createIndexInfo, destroyIndexInfo, compareStrElem);
int index = 0;
char *buf = (char*) malloc(BUFLEN+1);
if (buf == NULL) return NULL;
// Read the whole text from file.
fgets(buf, 250, file);
// Extract the first word.
char* str2 = strtok(buf, " .,\n");
while (str2 != NULL)
{
// Insert the word in the tree.
insert(tree, (void*) str2, (void*) &index);
// Update the new index.
index = index + strlen(str2) + 1;
// Extract the next word.
str2 = strtok(NULL, " .,\n");
}
free(buf);
buf = NULL;
fclose(file);
return tree;
}
Range* createRange(int capacity)
{
Range* range = (Range*) calloc(1, sizeof(Range));
if (range == NULL) return NULL;
range->index = (int*) calloc(capacity, sizeof(int));
if (range->index == NULL) return NULL;
range->size = 0;
range->capacity = capacity;
return range;
}
Range* singleKeyRangeQuery(TTree* tree, char* q){
// TODO: Cerinta 3
/* Get the words indices begining with the patern specified by q
* How can do you traverse the tree?
* There might be duplicates, can you make use of the tree duplicate lists?
*/
Range* range = createRange(1);
// Start parsing the list from the minimum element in the tree.
TreeNode* list = minimum(tree, tree->root);
int keyLen = strlen(q);
char* str = (char*) calloc(ELEMENT_TREE_LENGTH, sizeof(char));
if (str == NULL) return NULL;
while (list != NULL)
{
// Copy as many characters as the key has from the element in the tree.
strncpy(str, (char*)(list->elem), keyLen);
if (strcmp(str, q) == 0)
{
// if those characters are equal to the given key,
// the element index must be saved.
if (range->size == range->capacity)
{
// Realloc at double capacity if the index array is full.
range->capacity = 2 * range->capacity;
range->index = (int*) realloc(range->index, range->capacity * sizeof(range->index));
}
range->index[range->size++] = *(int*)(list->info);
}
list = list->next;
}
free(str);
str = NULL;
return range;
}
Range* multiKeyRangeQuery(TTree* tree, char* q, char* p){
// TODO: Cerinta 4
/* Get the words indices begining with the any patern
* between q and p (inclusive).
*/
Range* range = createRange(1);
// start parsing the list from the minimum element in the tree.
TreeNode* list = minimum(tree, tree->root);
int keyStartLen = strlen(q);
int keyStopLen = strlen(p);
char* str1 = (char*) calloc(ELEMENT_TREE_LENGTH, sizeof(char));
char* str2 = (char*) calloc(ELEMENT_TREE_LENGTH, sizeof(char));
while (list != NULL)
{
// Copy as many characters as the keys have from the element in the tree.
strncpy(str1, (char*)(list->elem), keyStartLen);
strncpy(str2, (char*)(list->elem), keyStopLen);
if (strcmp(str1, q) >= 0 && strcmp(str2, p) <= 0)
{
// If the element is in the range determined by the keys,
// the leement index must be saved.
if (range->size == range->capacity)
{
// Realloc at double capacity if the index array is full.
range->capacity = 2 * range->capacity;
range->index = (int*) realloc(range->index, range->capacity * sizeof(range->index));
}
range->index[range->size++] = *(int*)(list->info);
}
list = list->next;
}
free(str1);
str1 = NULL;
free(str2);
str2 = NULL;
return range;
}
int main(void) {
printf("The text file:\n");
printFile("text.txt");
TTree* tree = buildTreeFromFile("text.txt");
printf("Tree In Order:\n");
printTreeInOrder(tree);
printf("\n\n");
printf("Single search:\n");
Range *range = singleKeyRangeQuery(tree,"v");
printWordsInRangeFromFile(range,"text.txt");
printf("Multi search:\n");
Range *range2 = multiKeyRangeQuery(tree,"j","pr");
printWordsInRangeFromFile(range2,"text.txt");
if(range != NULL) free(range->index);
free(range);
if(range2 != NULL) free(range2->index);
free(range2);
destroyTree(tree);
return 0;
}