-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathavlTree.c
More file actions
337 lines (300 loc) · 8.96 KB
/
avlTree.c
File metadata and controls
337 lines (300 loc) · 8.96 KB
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
//
// Created by Tommy O'Brien on 3/9/2019.
//
#include "avlTree.h"
#include "catalogs.h"
Movie* LoadDatabase()
{
FILE *titleBasics = NULL;
titleBasics = fopen("title.basics.tsv", "r"); //file with all of the movie information
if(titleBasics == NULL)
{
printf("\ntitle.basics.tsv was not found\n\n");
return NULL;
}
char tconst[15], titleType[15], primaryTitle[450], originalTitle[450], genres[GENRE_SPACE];
char isAdult[3], startYear[NUM_SPACE], endYear[NUM_SPACE], runtimeMinutes[NUM_SPACE];
char headers[20], trash;
for(int i = 1; i <= 8; i++) //reads in top line of categories
{
fscanf(titleBasics, "%s\t", headers);
}
fscanf(titleBasics, "%s", headers);
Movie *tree = NULL;
while(!feof(titleBasics)) //this loop adds everything designated as a movie into the AVL tree
{
fscanf(titleBasics, "%c", &trash);
fscanf(titleBasics, " %s %[^\t] %[^\t] %[^\t] %s %s %s %s %[^\n]", tconst, titleType, primaryTitle, originalTitle, isAdult, startYear, endYear, runtimeMinutes, genres);
if(strcmp(titleType,"movie") == 0)
{
tree = Insert(tree, ConvertToKey(primaryTitle), primaryTitle, genres, startYear, runtimeMinutes);
}
}
fclose(titleBasics);
return tree;
}
Movie* Insert(Movie *avl, char *nameKey, char *nameReg, char *types, char *yearStart, char *minutes) //see citation in header file
{
if(avl == NULL) //inserting Movie as a leaf in the correct ordering position
{
avl = (Movie *)malloc(sizeof(Movie));
strcpy(avl->key, nameKey);
strcpy(avl->title, nameReg);
strcpy(avl->genre, types);
strcpy(avl->year, yearStart);
strcpy(avl->time, minutes);
strcpy(avl->date, "empty");
strcpy(avl->media, "empty");
avl->left = NULL;
avl->right = NULL;
}
else
{
if(strcmp(nameKey, avl->key) == 0) //duplicate titles/keys are not allowed
return avl;
else if(strcmp(nameKey, avl->key) > 0)
{
avl->right = Insert(avl->right, nameKey, nameReg, types, yearStart, minutes);
if(BF(avl) == -2) //ensuring AVL property as recursion unfolds back up the tree
{
if(strcmp(nameKey, avl->right->key) > 0)
avl = RR(avl);
else
avl = RL(avl);
}
}
else
{
avl->left = Insert(avl->left, nameKey, nameReg, types, yearStart, minutes);
if(BF(avl) == 2) //ensuring AVL property as recursion unfolds back up the tree
{
if(strcmp(nameKey, avl->left->key) < 0)
avl = LL(avl);
else
avl = LR(avl);
}
}
}
avl->height = height(avl);
return avl;
}
Movie* Remove(Movie *avl, char *name) //see citation in header file
{
Movie *temp; //needed when removing a match
if(avl == NULL)
{
printf("\nTitle not found. Movie not removed. Before trying again, view all movies in your catalog with option 6.\n");
return NULL;
}
else
{
if(strcmp(name, avl->key) > 0)
{
avl->right = Remove(avl->right, name);
if(BF(avl) == 2) //ensuring AVL property as recursion unfolds back up the tree
{
if(BF(avl->left) >= 0)
{
avl = LL(avl);
}
else
{
avl = LR(avl);
}
}
}
else if(strcmp(name, avl->key) < 0)
{
avl->left = Remove(avl->left, name);
if(BF(avl) == -2) //ensuring AVL property as recursion unfolds back up the tree
{
if(BF(avl->right) <= 0)
{
avl = RR(avl);
}
else
{
avl = RL(avl);
}
}
}
else
{
if(avl->right != NULL)
{
temp = avl->right; //finding successor
while(temp->left != NULL)
{
temp = temp->left;
}
strcpy(avl->key, temp->key); //essentially switching nodes
strcpy(avl->title, temp->title);
strcpy(avl->year, temp->year);
strcpy(avl->time, temp->time);
strcpy(avl->genre, temp->genre);
strcpy(avl->media, temp->media);
strcpy(avl->date, temp->date);
avl->right = Remove(avl->right, temp->key);
if(BF(avl) == 2) //ensuring AVL property
{
if(BF(avl->left) >= 0)
{
avl = LL(avl);
}
else
{
avl = LR(avl);
}
}
}
else
{
temp = avl->left; //Movie to remove is replaced by its left child
return temp;
}
}
}
avl->height = height(avl);
return avl;
}
Movie* UpdateMediaType(Movie *avl, char *nameKey, char *mediaType)
{
if(avl == NULL) //reached NULL leaf location along path where nameKey's movie should have existed somewhere in, so the title is not in the tree
{
printf("\nERROR: Movie not found. Media type not added.\n");
return NULL;
}
else
{
if(strcmp(nameKey, avl->key) == 0) //movie found and date is updated
{
strcpy(avl->media, mediaType);
return avl;
}
else if(strcmp(nameKey, avl->key) > 0)
{
avl->right = UpdateMediaType(avl->right, nameKey, mediaType);
}
else
{
avl->left = UpdateMediaType(avl->left, nameKey, mediaType);
}
}
return avl;
}
Movie* UpdateDate(Movie *avl, char *nameKey, char *dateAcquired)
{
if(avl == NULL) //reached NULL leaf location along path where nameKey's movie should have existed somewhere in, so the title is not in the tree
{
printf("\nERROR: Movie not found. Date of acquisition not added.\n");
return NULL;
}
else
{
if(strcmp(nameKey, avl->key) == 0) //movie found and date is updated
{
strcpy(avl->date, dateAcquired);
return avl;
}
else if(strcmp(nameKey, avl->key) > 0)
{
avl->right = UpdateDate(avl->right, nameKey, dateAcquired);
}
else
{
avl->left = UpdateDate(avl->left, nameKey, dateAcquired);
}
}
return avl;
}
int height(Movie *avl) //see citation in header file
{
int lh, rh;
if(avl == NULL)
return 0;
if(avl->left == NULL)
lh = 0;
else
lh = 1 + avl->left->height;
if(avl->right == NULL)
rh = 0;
else
rh = 1 + avl->right->height;
if(lh > rh) //return length of longest path to NULL leaf
return lh;
return rh;
}
int BF(Movie *avl) //see citation in header file
{
int lh, rh;
if(avl == NULL)
return 0;
if(avl->left == NULL)
lh = 0;
else
lh = 1 + avl->left->height;
if(avl->right == NULL)
rh = 0;
else
rh = 1 + avl->right->height;
return (lh - rh); //difference in height between left and right children
}
Movie *rotateRight(Movie *x) //see citation in header file
{
Movie *y;
y = x->left;
x->left = y->right;
y->right = x;
x->height = height(x);
y->height = height(y);
return y;
}
Movie *rotateLeft(Movie *x) //see citation in header file
{
Movie *y;
y = x->right;
x->right = y->left;
y->left = x;
x->height = height(x);
y->height = height(y);
return y;
}
Movie *RR(Movie *x) //see citation in header file
{
x = rotateLeft(x);
return x;
}
Movie *LL(Movie *x) //see citation in header file
{
x = rotateRight(x);
return x;
}
Movie *LR(Movie *x) //see citation in header file
{
x->left = rotateLeft(x->left);
x = rotateRight(x);
return x;
}
Movie *RL(Movie *x) //see citation in header file
{
x->right = rotateRight(x->right);
x = rotateLeft(x);
return x;
}
void SearchForMovie(Movie* database, char* titleKey, int keyLen)
{
if(database == NULL)
return;
else if(strncmp(database->key, titleKey, keyLen) == 0) //when a movie matches, it is possible that subsequent movies to the left or right in its subtree could match
{
SearchForMovie(database->left, titleKey, keyLen);
printf("%s\n", database->title);
SearchForMovie(database->right, titleKey, keyLen);
}
else if(strncmp(database->key, titleKey, keyLen) < 0)
{
SearchForMovie(database->right, titleKey, keyLen);
}
else
SearchForMovie(database->left, titleKey, keyLen);
}