-
Notifications
You must be signed in to change notification settings - Fork 0
/
myrecs.c
353 lines (291 loc) · 10.8 KB
/
myrecs.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
/*
Kim Cooperrider
kac162
cs1550
Project 1
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "my_header.h"
#include "linked_node_list.c"
#include "gpa_operations.c"
#include "top_operations.c"
#include "info_operations.c"
void printNode( struct node* node ) { // Print the node
if ( node != NULL ) {
println(">>NODE:: numChildren: %d, isLeafNode: %d", node->numChildren, node->isLeafNode);
int i;
for ( i = 0; i < node->numChildren; i++ ) {
println(">>Node->keys[%d]=%d, \t", i, node->keys[i]);
if ( node->isLeafNode ) { // then has linkedList
PrintItem( node->courseList[i] );
}
}
if ( node->isLeafNode && node->nextLeaf != NULL ) {
println(">>Node's rightmost child points to new node, with key[0] = %d", node->nextLeaf->keys[0]);
}
} else {
println("Node is null." );
}
} // printNode
void printTree( struct node* root ) {
println("Printing tree.");
printNode( root );
if ( root->numChildren > 0 ) {
println(" Children ");
int i;
for ( i = 0; i < root->numChildren; i++ ) {
println( "i is %d", i );
printNode( root->children[i] );
}
}
} // printTree
struct node* createTree( void ) { // return a pointer to the root node
struct node* root = (struct node*) malloc( sizeof(struct node)+1 );
root->isLeafNode = YES;
root->numChildren = 0;
return root;
} // createTree
struct nodeIndex* search( struct node* node, int studentId ) {
struct nodeIndex* nodeIndex = (struct nodeIndex*) nodeIndexForKey( node, studentId );
int i = nodeIndex->index;
if ( nodeIndex->wasFound ) { // studentId exactly matches node's child at i
return nodeIndex;
} else if ( node->isLeafNode == YES ) { // if we hit a leaf node, we've run out
nodeIndex->wasFound = NO;
return nodeIndex;
} else {
return search( node->children[i], studentId ); // return
}
} // search
struct node* split( struct node* node, int i ) {
struct node* z = (struct node*) malloc( sizeof(struct node) );
struct node* y = (struct node*) node->children[i]; // y is node's leftmost child
int median = y->numChildren / 2; // median is half the number of y's children
z->numChildren = y->numChildren - median; // equally divide y's children (take median number)
z->isLeafNode = y->isLeafNode;
int j;
for ( j=0; j < median; j++ ) {
z->keys[j] = y->keys[j+median];
}
if ( y->isLeafNode ) {
y->nextLeaf = z;
for ( j=0; j < median; j++ ) {
z->children[j] = y->children[j+median];
z->courseList[j] = y->courseList[j+median];
}
}
y->numChildren = median;
for ( j=node->numChildren; j >= i; j-- ) {
node->keys[j] = node->keys[i];
node->children[j] = node->children[i];
node->courseList[j] = node->courseList[i];
}
node->children[i+1] = z; // plop new node into place
node->keys[i] = y->keys[median-1];
node->numChildren = node->numChildren+1;
return node;
} // split
struct node* insertNonfull( struct node* node, int studentId, struct item* item ) {
int i = node->numChildren;
if ( node->isLeafNode == YES ) {
while ( i > 0 && studentId < node->keys[i-1] ) { // while we have at least one child node and the studentId is less than current key
node->keys[i] = node->keys[i-1]; // shift keys right by one node
node->courseList[i] = node->courseList[i-1]; // shift courseList right by one
i--;
}
if ( node->keys[i] != studentId ) {
node->courseList[i] = InsertItem( NULL, item ); // clear the list that was previously in this slot
} else {
node->courseList[i] = InsertItem( node->courseList[i], item );
}
node->keys[i] = studentId; // slot studentId in the right place
node->numChildren = node->numChildren + 1;
} else {
while ( i > 0 && studentId < node->keys[i-1] ) { // while studentId < node's greatest key
i--;
}
if ( node->children[i]->numChildren == 4 ) { // if the child node at index where studentId should go has no free slots,
node = split( node, i ); // split the node
if ( studentId > node->keys[i] ) { // if studentId greater than node keys[i]
i++;
}
}
node = insertNonfull( node->children[i], studentId, item ); // insert studentId into node's ith child (nonfull)
}
return node;
} // insertNonfull
struct node* insertMax( struct node* node, int studentId, struct item* item ) {
int i = node->numChildren;
node->keys[i] = studentId; // node's greatest key is studentId
if ( node->isLeafNode == YES ) {
node->courseList[i] = InsertItem( node->courseList[i], item );
node->numChildren = node->numChildren + 1;
} else {
if ( node->children[i-1]->numChildren == 4 ) { // if slotting in studentId made 4 children
node = split( node, i );
i++;
}
node = insertMax( node->children[i-1], studentId, item );
}
return node;
} // insertMax
struct node* insertData( struct node* root, int studentId, char* courseId, char* courseName, char* grade ) { // char courseId[7], courseName[8], grade[3];
if ( studentId > 0 ) {
struct item* item = CreateItemWithData( courseId, courseName, grade );
root = insert( root, studentId, item );
return root;
} else {
println("ERROR:\nMalformed studentId. Trailing newline at the end of input file?");
exit(0);
}
} // insertData
struct nodeIndex* nodeIndexForKey( struct node* node, int studentId ) {
struct nodeIndex* nodeIndex = (struct nodeIndex*) malloc( sizeof(struct nodeIndex)+1 );
nodeIndex->node = node;
int i=0;
while ( node->keys[i] < studentId && i < node->numChildren ) {
i++;
}
if ( node->keys[i] == studentId ) {
nodeIndex->wasFound = YES;
nodeIndex->index = i;
}
return nodeIndex;
}// nodeIndexForKey
struct node* insert( struct node* root, int studentId, struct item* item ) {
// returns root node
struct nodeIndex* nodeIndex = (struct nodeIndex*) nodeIndexForKey( root, studentId );
if ( nodeIndex->wasFound ) { // quick search to determine if studentId is already in tree
nodeIndex->node->courseList[ nodeIndex->index ] = InsertItem( nodeIndex->node->courseList[ nodeIndex->index ], item );
} else {
int i = root->numChildren;
if ( i == 4 ) { // if node has no free slots
struct node* node = (struct node*) malloc( sizeof(struct node)+1 ); // allocate new node
node->isLeafNode = NO;
node->numChildren = 1;
node->children[0] = root; // new node's first child is root
node->keys[0] = root->keys[3]; // first child of new node is greatest child of root
root = node;
node = split( node, 0 );
if ( studentId > node->keys[1] ) { // if studentId > max of newly created root
node = insertMax( node, studentId, item );
} else {
node = insertNonfull( node, studentId, item );
}
} else {
int j = i != 0 ? i-1 : 0;
if ( studentId > root->keys[j] ) { // if studentId > the the node's greatest child
root = insertMax( root, studentId, item ); // then studentId is the max for this node
} else {
root = insertNonfull( root, studentId, item ); // else insert amidst the children nodes
}
}
}
return root;
}// insert
void freeNode( struct node* node ) {
if ( node != NULL && node->numChildren > 0 ) {
int i;
for (i = 0; i < node->numChildren; i++) {
freeNode( node->children[i] );
}
free( node );
}
}
void freeTree( struct node* root ) {
println( "Freeing tree.");
if ( !root->isLeafNode && root->numChildren > 0 ) {
int i;
for ( i = 0; i < root->numChildren; i++ ) {
freeNode( root->children[i] );
}
}
free( root );
} // freeTree
int loadFile( struct node* root, char* filename ) {
struct item *courseList = NULL;
FILE *fp = NULL;
char separator;
int numInserts = 0;
if ( (fp = fopen( filename, "r" )) == NULL ) {
println("Unknown file");
exit(1);
} else {
while ( !feof(fp) ) { // studentId > 0 protects against trailing newline in file
int studentId = 0;
char courseId[7], courseName[8], grade[3];
fscanf( fp, "%d %s %s %s", &studentId, courseId, courseName, grade );
fscanf( fp, "%c", &separator );
root = insertData( root, studentId, courseId, courseName, grade );
if ( studentId != 0) numInserts++;
studentId = 0; // reset
} // end while
fclose(fp);
}
return numInserts;
} // loadFile
int main( int argc, char *argv[] ) {
struct node* root = NULL;
root = createTree(); // points to root node of tree
int execute = YES; // while loop control
int numInserts = 0;
if ( argc != 2 ) {
println("No filename provided." );
} else {
int numInsertsFromFile = loadFile( root, argv[1] );
numInserts += numInsertsFromFile;
}
while ( execute ) {
char cmd[MAX_COMMAND_SIZE];
int studentId, studentId_a, studentId_b, top;
char courseId[7], courseName[8], grade[3];
char filename[MAX_COMMAND_SIZE];
printf("Please enter your action: ");
scanf("%s", cmd);
if ( strEqual(cmd, "exit") ) {
execute = NO;
} else if ( strEqual(cmd, "printtree") ) {
printTree( root );
} else if ( strEqual(cmd, "print") ) {
printNode( root );
} else if ( strEqual(cmd, "find") ) {
scanf("%d", &studentId);
struct nodeIndex* nodeIndex = search( root, studentId );
if ( nodeIndex->wasFound ) {
println("StudentId %d was found in tree.", studentId);
} else {
println("StudentId %d was not found in tree.", studentId);
}
} else if ( strEqual(cmd, "ins") ) { // studentId, courseId, courseName, grade
scanf("%d %s %s %s", &studentId, courseId, courseName, grade);
root = insertData( root, studentId, courseId, courseName, grade); // insert new data
if ( studentId > 0 ) numInserts++;
} else if ( strEqual(cmd, "load") ) { // filename
scanf("%s", filename);
int numInsertsFromFile = loadFile( root, filename );
numInserts += numInsertsFromFile;
} else if ( strEqual(cmd, "range") ) { // studentId_a, studentId_b
scanf("%d %d", &studentId_a, &studentId_b);
getInfoInRange( root, studentId_a, studentId_b );
} else if ( strEqual(cmd, "gpa") ) { // gpa <studentId>
scanf("%d", &studentId);
getGPA( root, studentId );
} else if ( strEqual(cmd, "gpa_range") ) { // gpa_range <studentId_a> <studentId_b>
scanf("%d %d", &studentId_a, &studentId_b);
getGPAinRange( root, studentId_a, studentId_b );
} else if ( strEqual(cmd, "top") ) {
scanf("%d", &top);
getTopCourses( root, top, numInserts );
} else if ( strEqual(cmd, "verify") ) { // check all nodes to ensure properties of 2-4 tree
println("STUB: verify.");
} else if ( strEqual(cmd, "leaves") ) { // check all nodes to ensure properties of 2-4 tree
traverseLeaves( root );
} else {
println("ERROR\nCommand '%s' not recognized.", cmd);
}
} // end while
freeTree( root );
return 0;
}