@@ -15,6 +15,16 @@ struct nodePosition {
15
15
struct node * box ;
16
16
};
17
17
18
+ // Left-right stack for multiple splitting
19
+ struct splitStack {
20
+ struct node * leftHalf ;
21
+ struct node * rightHalf ;
22
+ struct splitStack * next ;
23
+ } * head = NULL ;
24
+
25
+ void pushS (struct node * leftHalf , struct node * rightHalf );
26
+ struct splitStack * popS ();
27
+
18
28
// Globals
19
29
int treeOrder ;
20
30
@@ -28,7 +38,7 @@ void doInOrder();
28
38
// Tree Operations
29
39
struct node * newNode (int value ,struct node * parent );
30
40
struct node * insertN (int value ,struct node * root ,struct node * parent );
31
- struct node * searchNValue (int value ,struct node * root );
41
+ struct nodePosition * searchNValue (int value ,struct node * root );
32
42
struct nodePosition * searchNBox (struct node * toFind ,struct node * root );
33
43
34
44
// Printing
@@ -209,14 +219,20 @@ struct node* insertN(int value,struct node *root,struct node *parent){
209
219
struct node * rightHalf = NULL ;
210
220
211
221
for (i = 0 ; i <= left ; i ++ ){ // Move all left half data to new LeftHalf Box
212
- leftHalf = insertN (root -> value [i ],leftHalf ,parent ); // Set the parent to NULL temporarily
222
+ leftHalf = insertN (root -> value [i ],leftHalf ,NULL ); // Set the parent to NULL temporarily
213
223
leftHalf -> keys [i ] = root -> keys [i ];
224
+
225
+ if (root -> keys [i ] != NULL ) root -> keys [i ]-> parent = leftHalf ;
214
226
} leftHalf -> keys [left + 1 ] = root -> keys [left + 1 ];
227
+ if (root -> keys [treeOrder ] != NULL ) root -> keys [treeOrder ]-> parent = leftHalf ;
215
228
216
229
for (i = right ; i < treeOrder ; i ++ ){ // Move all right half data to new LeftHalf Box
217
- rightHalf = insertN (root -> value [i ],rightHalf ,parent );
230
+ rightHalf = insertN (root -> value [i ],rightHalf ,NULL );
218
231
rightHalf -> keys [i ] = root -> keys [i ];
232
+
233
+ if (root -> keys [i ] != NULL ) root -> keys [i ]-> parent = rightHalf ;
219
234
} rightHalf -> keys [treeOrder ] = root -> keys [treeOrder ];
235
+ if (root -> keys [treeOrder ] != NULL ) root -> keys [treeOrder ]-> parent = rightHalf ;
220
236
221
237
int pIsNull = parent == NULL ? 1 : 0 ;
222
238
struct node * tempRoot = root ;
@@ -234,30 +250,27 @@ struct node* insertN(int value,struct node *root,struct node *parent){
234
250
return parent ;
235
251
} else {
236
252
// Find the parent of the current left and right box
237
- struct nodePosition * nodeF = searchNBox (tempRoot ,parent );
238
- if (nodeF != NULL ){
239
- nodeF -> box -> keys [nodeF -> key ] = leftHalf ;
240
- nodeF -> box -> keys [nodeF -> key + 1 ] = rightHalf ;
241
- }
242
-
243
- /*
253
+ int found = 0 ;
244
254
for (i = 0 ; i < parent -> keyCount ; i ++ ){
245
- if (parent->keys[i] == tempRoot){
255
+ if (parent -> keys [i ] != NULL && parent -> keys [ i ] == tempRoot ){
246
256
parent -> keys [i ] = leftHalf ;
247
257
parent -> keys [i + 1 ] = rightHalf ;
248
- } else if (parent->keys[i] != NULL) {
249
- for (j = 0; j < parent->keys[i]->keyCount; j++){
250
- if (parent->keys[i]->keys[j] == tempRoot){
251
- parent->keys[i]->keys[j] = leftHalf;
252
- parent->keys[i]->keys[j+1] = rightHalf;
253
- }
254
- }
258
+
259
+ leftHalf -> parent = parent ;
260
+ rightHalf -> parent = parent ;
261
+ found = 1 ;
255
262
}
256
263
}
257
- */
258
264
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
+ }
259
272
260
- free (tempRoot );
273
+ // free(tempRoot);
261
274
return leftHalf ;
262
275
}
263
276
@@ -268,13 +281,18 @@ struct node* insertN(int value,struct node *root,struct node *parent){
268
281
return root ;
269
282
}
270
283
271
- struct node * searchNValue (int value ,struct node * root ){
284
+ struct nodePosition * searchNValue (int value ,struct node * root ){
272
285
int i ;
273
- struct node * nodeF = NULL ;
286
+ struct nodePosition * nodeF = NULL ;
274
287
if (root == NULL ) return NULL ;
275
288
else {
276
289
for (i = 0 ; i < root -> keyCount ; i ++ ){ // -1 since left and right key of every data box
277
- if (root -> value [i ] == value ) return root ;
290
+ if (root -> value [i ] == value ) {
291
+ nodeF = (struct nodePosition * )malloc (sizeof (struct nodePosition ));
292
+ nodeF -> box = root ;
293
+ nodeF -> key = i ;
294
+ return nodeF ;
295
+ }
278
296
else if (nodeF != NULL ) return nodeF ;
279
297
else nodeF = searchNValue (value ,root -> keys [i ]);
280
298
}
@@ -289,6 +307,7 @@ struct nodePosition* searchNBox(struct node *toFind,struct node *root){
289
307
else {
290
308
for (i = 0 ; i < root -> keyCount ; i ++ ){ // -1 since left and right key of every data box
291
309
if (root == toFind ) {
310
+ nodeF = (struct nodePosition * )malloc (sizeof (struct nodePosition ));
292
311
nodeF -> box = root ;
293
312
nodeF -> key = i ;
294
313
return nodeF ;
@@ -305,8 +324,8 @@ void inOrder(struct node *root){
305
324
int i ;
306
325
if (root == NULL ) return ;
307
326
else {
308
- // root = root->keys[2 ];
309
- for (i = 0 ; i < root -> keyCount ; i ++ ){ // -1 since left and right key of every data box
327
+ // root = root->keys[1]->keys[3 ];
328
+ for (i = 0 ; i < root -> keyCount + 2 ; i ++ ){ // -1 since left and right key of every data box
310
329
inOrder (root -> keys [i ]);
311
330
if (i < root -> keyCount - 1 ) printf ("~%d~\n" ,root -> value [i ]);
312
331
}
@@ -318,12 +337,12 @@ void levelOrder(struct node *root){
318
337
if (root == NULL ) return ;
319
338
else {
320
339
for (i = 0 ; i < root -> keyCount - 1 ; i ++ ){ // -1 since left and right key of every data box
321
- printf ("%d " ,root -> value [i ]);
340
+ printf ("Node %d : %d " , i ,root -> value [i ]);
322
341
}
323
342
printf (" " );
324
343
if (root -> parent == NULL ) printf ("\n" );
325
344
else {
326
- for (i = 0 ; i < root -> parent -> keyCount ; i ++ ){ // Check if had a right sibling, if none then proceed to next level
345
+ for (i = 0 ; i < root -> parent -> keyCount + 2 ; i ++ ){ // Check if had a right sibling, if none then proceed to next level
327
346
if (root -> parent -> keys [i ] == root ){
328
347
if (root -> parent -> keys [i + 1 ] == NULL ) printf ("\n" );
329
348
break ;
@@ -336,3 +355,26 @@ void levelOrder(struct node *root){
336
355
}
337
356
}
338
357
}
358
+
359
+ void pushS (struct node * leftHalf , struct node * rightHalf ){
360
+ struct splitStack * newNode = (struct splitStack * )malloc (sizeof (struct splitStack ));
361
+ newNode -> leftHalf = leftHalf ;
362
+ newNode -> rightHalf = rightHalf ;
363
+ if (head == NULL ){
364
+ head = newNode ;
365
+ head -> next = NULL ;
366
+ } else {
367
+ newNode -> next = head ;
368
+ head = newNode ;
369
+ }
370
+ }
371
+
372
+ struct splitStack * popS (){
373
+ if (head == NULL ) {
374
+ printf ("Stack Underflow!!\n" );
375
+ getch ();
376
+ exit (-1 );
377
+ } else {
378
+
379
+ }
380
+ }
0 commit comments