@@ -45,6 +45,10 @@ struct nodePosition* searchNBox(struct node *toFind,struct node *root);
45
45
void inOrder (struct node * root );
46
46
void levelOrder (struct node * root );
47
47
48
+ // Utils
49
+ int findMax (struct node * root );
50
+ int findMin (struct node * root );
51
+
48
52
int main (){
49
53
// Initialize
50
54
printf ("--- Initialization ---\n" );
@@ -90,7 +94,9 @@ void doInsert(){
90
94
printf ("Value: " );
91
95
scanf ("%d" ,& value );
92
96
93
- tRoot = insertN (value ,tRoot ,NULL );
97
+
98
+ if (tRoot == NULL ) tRoot = insertN (value ,tRoot ,NULL );
99
+ else insertN (value ,tRoot ,NULL );
94
100
95
101
printf ("\n\n\nPress any key to continue...\n" );
96
102
getch ();
@@ -224,20 +230,21 @@ struct node* insertN(int value,struct node *root,struct node *parent){
224
230
225
231
if (root -> keys [i ] != NULL ) root -> keys [i ]-> parent = leftHalf ;
226
232
} leftHalf -> keys [left + 1 ] = root -> keys [left + 1 ];
227
- if (root -> keys [treeOrder ] != NULL ) root -> keys [treeOrder ]-> parent = leftHalf ;
233
+ if (root -> keys [treeOrder ] != NULL ) root -> keys [treeOrder ]-> parent = leftHalf ;
228
234
229
235
for (i = right ; i < treeOrder ; i ++ ){ // Move all right half data to new LeftHalf Box
230
236
rightHalf = insertN (root -> value [i ],rightHalf ,NULL );
231
237
rightHalf -> keys [i ] = root -> keys [i ];
232
238
233
239
if (root -> keys [i ] != NULL ) root -> keys [i ]-> parent = rightHalf ;
234
240
} rightHalf -> keys [treeOrder ] = root -> keys [treeOrder ];
235
- if (root -> keys [treeOrder ] != NULL ) root -> keys [treeOrder ]-> parent = rightHalf ;
241
+ if (root -> keys [treeOrder ] != NULL ) root -> keys [treeOrder ]-> parent = rightHalf ;
236
242
237
243
int pIsNull = parent == NULL ? 1 : 0 ;
238
244
struct node * tempRoot = root ;
239
245
struct node * pParent = pIsNull ? NULL : parent -> parent ;
240
246
247
+ pushS (leftHalf ,rightHalf );
241
248
parent = insertN (root -> value [mid ],parent ,pParent );
242
249
243
250
if (pIsNull ){ // Special case if splitted is the tRoot (The very Root)
@@ -246,35 +253,48 @@ struct node* insertN(int value,struct node *root,struct node *parent){
246
253
parent -> keys [0 ] = leftHalf ;
247
254
parent -> keys [1 ] = rightHalf ;
248
255
256
+ tRoot = parent ;
249
257
//free(tempRoot); // Delete old root node box
250
258
return parent ;
251
259
} else {
252
- // Find the parent of the current left and right box
253
- int found = 0 ;
260
+
261
+ struct splitStack * ss = popS ();
262
+
263
+
264
+ // put left half properly
265
+ if (ss != NULL ){
266
+ // Find the parent of the current left and right box
267
+ int max = findMax (leftHalf );
268
+ for (i = 0 ; i < parent -> keyCount ; i ++ ){
269
+ if (max < parent -> value [i ]){
270
+ parent -> keys [i ] = leftHalf ;
271
+ parent -> keys [i + 1 ] = rightHalf ;
272
+
273
+ printf ("Left half is: %d\n" ,leftHalf -> value [0 ]);
274
+
275
+ leftHalf -> parent = parent ;
276
+ rightHalf -> parent = parent ;
277
+ }
278
+ }
279
+
280
+ // put right half properly
281
+ }
282
+
283
+ /**
254
284
for (i = 0; i < parent->keyCount; i++){
255
285
if (parent->keys[i] != NULL && parent->keys[i] == tempRoot){
256
286
parent->keys[i] = leftHalf;
257
287
parent->keys[i+1] = rightHalf;
258
288
259
289
leftHalf->parent = parent;
260
290
rightHalf->parent = parent;
261
- found = 1 ;
262
291
}
263
292
}
293
+ **/
264
294
265
-
266
- struct nodePosition * nodeF = searchNBox (tempRoot ,tRoot );
267
- if (nodeF != NULL && !found ) {
268
- printf ("Beep: i is %d\n" ,nodeF -> key );
269
- nodeF -> box -> keys [nodeF -> key ] = leftHalf ;
270
- nodeF -> box -> keys [nodeF -> key + 1 ] = rightHalf ;
271
- }
272
-
273
- //free(tempRoot);
274
295
return leftHalf ;
275
296
}
276
297
277
- // To do : Non special case split and distribute... or if parent has a parent with values
278
298
}
279
299
}
280
300
@@ -337,7 +357,7 @@ void levelOrder(struct node *root){
337
357
if (root == NULL ) return ;
338
358
else {
339
359
for (i = 0 ; i < root -> keyCount - 1 ; i ++ ){ // -1 since left and right key of every data box
340
- printf ("Node %d : %d " ,i ,root -> value [i ]);
360
+ printf ("%d " ,i ,root -> value [i ]);
341
361
}
342
362
printf (" " );
343
363
if (root -> parent == NULL ) printf ("\n" );
@@ -372,9 +392,28 @@ void pushS(struct node *leftHalf, struct node *rightHalf){
372
392
struct splitStack * popS (){
373
393
if (head == NULL ) {
374
394
printf ("Stack Underflow!!\n" );
375
- getch ();
376
- exit (-1 );
395
+ return NULL ;
377
396
} else {
378
-
397
+ struct splitStack * temp = head ;
398
+ head = head -> next ;
399
+ return temp ;
379
400
}
380
401
}
402
+
403
+ int findMax (struct node * root ){
404
+ int i , max = root -> value [0 ];
405
+ for (i = 0 ; i < root -> keyCount ; i ++ ){
406
+ if (root -> value [i ] > max ) max = root -> value [i ];
407
+ }
408
+
409
+ return max ;
410
+ }
411
+
412
+ int findMin (struct node * root ){
413
+ int i , min = root -> value [0 ];
414
+ for (i = 0 ; i < root -> keyCount ; i ++ ){
415
+ if (root -> value [i ] < min ) min = root -> value [i ];
416
+ }
417
+
418
+ return min ;
419
+ }
0 commit comments