@@ -39,7 +39,6 @@ void doInOrder();
39
39
struct node * newNode (int value ,struct node * parent );
40
40
struct node * insertN (int value ,struct node * root ,struct node * parent );
41
41
struct nodePosition * searchNValue (int value ,struct node * root );
42
- struct nodePosition * searchNBox (struct node * toFind ,struct node * root );
43
42
44
43
// Printing
45
44
void inOrder (struct node * root );
@@ -68,7 +67,8 @@ void showMenu(){
68
67
printf (" _____ _____ \n" );
69
68
printf (" | __ | ___ |_ _|___ ___ ___ \n" );
70
69
printf (" | __ -| |___| | | | _| -_| -_| \n" );
71
- printf (" |_____| |_| |_| |___|___| \n" );
70
+ printf (" |_____| |_| |_| |___|___| \n\n" );
71
+ printf (" A Project in Data Structures by Raven G. Duran BSIT - 2R1\n" );
72
72
73
73
printf ("\n\n" );
74
74
printf ("(1) Insert a Node\n" );
@@ -227,18 +227,14 @@ struct node* insertN(int value,struct node *root,struct node *parent){
227
227
for (i = 0 ; i <= left ; i ++ ){ // Move all left half data to new LeftHalf Box
228
228
leftHalf = insertN (root -> value [i ],leftHalf ,NULL ); // Set the parent to NULL temporarily
229
229
leftHalf -> keys [i ] = root -> keys [i ];
230
-
231
- if (root -> keys [i ] != NULL ) root -> keys [i ]-> parent = leftHalf ;
232
230
} leftHalf -> keys [left + 1 ] = root -> keys [left + 1 ];
233
- if (root -> keys [treeOrder ] != NULL ) root -> keys [treeOrder ]-> parent = leftHalf ;
234
231
235
- for (i = right ; i < treeOrder ; i ++ ){ // Move all right half data to new LeftHalf Box
232
+ int j = 0 ;
233
+ for (i = right ; i < treeOrder ; i ++ , j ++ ){ // Move all right half data to new LeftHalf Box
236
234
rightHalf = insertN (root -> value [i ],rightHalf ,NULL );
237
- rightHalf -> keys [i ] = root -> keys [i ];
238
-
239
- if (root -> keys [i ] != NULL ) root -> keys [i ]-> parent = rightHalf ;
240
- } rightHalf -> keys [treeOrder ] = root -> keys [treeOrder ];
241
- if (root -> keys [treeOrder ] != NULL ) root -> keys [treeOrder ]-> parent = rightHalf ;
235
+ rightHalf -> keys [j ] = root -> keys [i ];
236
+ } rightHalf -> keys [j ] = root -> keys [treeOrder ];
237
+
242
238
243
239
int pIsNull = parent == NULL ? 1 : 0 ;
244
240
struct node * tempRoot = root ;
@@ -247,37 +243,42 @@ struct node* insertN(int value,struct node *root,struct node *parent){
247
243
pushS (leftHalf ,rightHalf );
248
244
parent = insertN (root -> value [mid ],parent ,pParent );
249
245
246
+ struct splitStack * ss = popS ();
247
+
250
248
if (pIsNull ){ // Special case if splitted is the tRoot (The very Root)
251
- leftHalf -> parent = parent ;
252
- rightHalf -> parent = parent ;
253
- parent -> keys [0 ] = leftHalf ;
254
- parent -> keys [1 ] = rightHalf ;
249
+ ss -> leftHalf -> parent = parent ;
250
+ ss -> rightHalf -> parent = parent ;
251
+ parent -> keys [0 ] = ss -> leftHalf ;
252
+ parent -> keys [1 ] = ss -> rightHalf ;
255
253
256
254
tRoot = parent ;
257
255
//free(tempRoot); // Delete old root node box
258
- return parent ;
259
- } else {
260
256
261
- struct splitStack * ss = popS ();
257
+ printf ("\n\n" );
258
+ inOrder (parent );
259
+ printf ("\n\n" );
260
+
261
+ return parent ;
262
262
263
263
264
+ } else {
264
265
// put left half properly
265
266
if (ss != NULL ){
266
267
// Find the parent of the current left and right box
267
- int max = findMax (leftHalf );
268
+ int max = findMax (ss -> leftHalf );
268
269
for (i = 0 ; i < parent -> keyCount ; i ++ ){
269
270
if (max < parent -> value [i ]){
270
- parent -> keys [i ] = leftHalf ;
271
- parent -> keys [i + 1 ] = rightHalf ;
271
+ parent -> keys [i ] = ss -> leftHalf ;
272
+ parent -> keys [i + 1 ] = ss -> rightHalf ;
272
273
273
- printf ("Left half is: %d\n" ,leftHalf -> value [0 ]);
274
+ printf ("Left half is: %d\n" ,ss -> leftHalf -> value [0 ]);
275
+ printf ("Right half is: %d\n" ,ss -> rightHalf -> value [0 ]);
274
276
275
- leftHalf -> parent = parent ;
276
- rightHalf -> parent = parent ;
277
+ ss -> leftHalf -> parent = parent ;
278
+ ss -> rightHalf -> parent = parent ;
279
+ break ;
277
280
}
278
281
}
279
-
280
- // put right half properly
281
282
}
282
283
283
284
/**
@@ -292,7 +293,7 @@ struct node* insertN(int value,struct node *root,struct node *parent){
292
293
}
293
294
**/
294
295
295
- return leftHalf ;
296
+ return ss -> leftHalf ;
296
297
}
297
298
298
299
}
@@ -320,31 +321,12 @@ struct nodePosition* searchNValue(int value,struct node *root){
320
321
}
321
322
}
322
323
323
- struct nodePosition * searchNBox (struct node * toFind ,struct node * root ){
324
- int i ;
325
- struct nodePosition * nodeF = NULL ;
326
- if (root == NULL ) return NULL ;
327
- else {
328
- for (i = 0 ; i < root -> keyCount ; i ++ ){ // -1 since left and right key of every data box
329
- if (root == toFind ) {
330
- nodeF = (struct nodePosition * )malloc (sizeof (struct nodePosition ));
331
- nodeF -> box = root ;
332
- nodeF -> key = i ;
333
- return nodeF ;
334
- }
335
- else if (nodeF != NULL ) return nodeF ;
336
- else nodeF = searchNBox (toFind ,root -> keys [i ]);
337
- }
338
- return nodeF ;
339
- }
340
- }
341
-
342
324
// Printing
343
325
void inOrder (struct node * root ){
344
326
int i ;
345
327
if (root == NULL ) return ;
346
328
else {
347
- //root = root->keys[1]->keys[3 ];
329
+ //root = root->keys[1]->keys[2 ];
348
330
for (i = 0 ; i < root -> keyCount + 2 ; i ++ ){ // -1 since left and right key of every data box
349
331
inOrder (root -> keys [i ]);
350
332
if (i < root -> keyCount - 1 ) printf ("~%d~\n" ,root -> value [i ]);
@@ -357,7 +339,7 @@ void levelOrder(struct node *root){
357
339
if (root == NULL ) return ;
358
340
else {
359
341
for (i = 0 ; i < root -> keyCount - 1 ; i ++ ){ // -1 since left and right key of every data box
360
- printf ("%d " ,i , root -> value [i ]);
342
+ printf ("%d " ,root -> value [i ]);
361
343
}
362
344
printf (" " );
363
345
if (root -> parent == NULL ) printf ("\n" );
0 commit comments