@@ -226,6 +226,15 @@ namespace alg {
226
226
*/
227
227
void delete_op (node x, int32_t k) {
228
228
int32_t i;
229
+ /*
230
+ int t;
231
+ printf("key:%d n:%d\n",k, x->n);
232
+ for (t=0;t<x->n;t++) {
233
+ printf("=%d=", x->key[t]);
234
+ }
235
+ printf("\n");
236
+ */
237
+
229
238
if (x->n == 0 ) { // emtpy node
230
239
return ;
231
240
}
@@ -234,20 +243,13 @@ namespace alg {
234
243
while (i>=0 && k < x->key [i]) { // search the key.
235
244
i = i - 1 ;
236
245
}
237
-
246
+
238
247
if (i >= 0 && x->key [i] == k) { // key exists in this node.
239
- printf (" in case 1 & 2 [%d] [%d]\n " , i,x->n );
240
248
if (x->flag & LEAF) {
241
- // case 1.
242
- // If the key k is in node x and x is a leaf, delete the key k from x.
243
- int j;
244
- for (j = i;j<x->n -1 ;j++) { // shifting the keys.
245
- x->key [j] = x->key [j+1 ];
246
- }
247
- WRITE (x);
248
- printf (" deleted %d offset %x\n " , k, x);
249
- return ;
249
+ printf (" in case 1 [%d] [%d]\n " , i,x->n );
250
+ case1 (x, i, k);
250
251
} else {
252
+ printf (" in case 2 [%d] [%d]\n " , i,x->n );
251
253
case2 (x, i, k);
252
254
}
253
255
} else {
@@ -256,6 +258,17 @@ namespace alg {
256
258
}
257
259
}
258
260
261
+ void case1 (node x, int32_t i, int32_t k) {
262
+ // case 1.
263
+ // If the key k is in node x and x is a leaf, delete the key k from x.
264
+ int j;
265
+ for (j = i;j<x->n -1 ;j++) { // shifting the keys.
266
+ x->key [j] = x->key [j+1 ];
267
+ }
268
+ x->n = x->n - 1 ;
269
+ WRITE (x);
270
+ }
271
+
259
272
void case2 (node x, int32_t i, int32_t k) {
260
273
// case 2a:
261
274
// If the child y that precedes k in node x has at least t
@@ -301,10 +314,10 @@ namespace alg {
301
314
302
315
int j;
303
316
for (j=0 ;j<z->n ;j++) { // merge keys of z
304
- y->key [y->n +j+ 1 ] = z->key [j];
317
+ y->key [y->n +1 +j ] = z->key [j];
305
318
}
306
319
for (j=0 ;j<z->n +1 ;j++) { // merge childs of z
307
- y->c [y->n +j+ 1 ] = z->c [j];
320
+ y->c [y->n +1 +j ] = z->c [j];
308
321
}
309
322
310
323
// mark free z
@@ -314,11 +327,11 @@ namespace alg {
314
327
WRITE (y.get ());
315
328
316
329
for (j=i;j<x->n -1 ;j++) { // delete k from node x
317
- x->key [i ] = x->key [i +1 ];
330
+ x->key [j ] = x->key [j +1 ];
318
331
}
319
332
320
333
for (j=i+1 ;j<x->n ;j++){ // delete pointer to z --> (i+1)th
321
- x->c [i ] = x->c [i +1 ];
334
+ x->c [j ] = x->c [j +1 ];
322
335
}
323
336
x->n = x->n - 1 ;
324
337
WRITE (x);
@@ -332,6 +345,7 @@ namespace alg {
332
345
333
346
void case3 (node x, int32_t i, int32_t k) {
334
347
std::auto_ptr<node_t > ci (READ (x, i));
348
+
335
349
// case 3a.
336
350
// If x.c[i] has only t - 1 keys but has an immediate sibling with at least t keys,
337
351
// give x.c[i] an extra key by moving a key from x down into x.c[i], moving a
@@ -409,11 +423,10 @@ namespace alg {
409
423
x->key [j] = x->key [j+1 ];
410
424
}
411
425
412
- for (j=i;j<x->n ;j++) {
426
+ for (j=i+ 1 ;j<x->n ;j++) {
413
427
x->c [j] = x->c [j+1 ];
414
428
}
415
429
x->n = x->n - 1 ;
416
- x->c [0 ] = left->offset ;
417
430
418
431
// append x.c[i] into left sibling
419
432
for (j=0 ;j<ci->n ;j++) {
@@ -425,16 +438,23 @@ namespace alg {
425
438
}
426
439
left->n += ci->n ; // left became 2T-1
427
440
ci->flag |= MARKFREE; // free ci
441
+ ci->n = 0 ;
428
442
WRITE (ci.get ());
429
443
WRITE (x);
444
+ // root check
445
+ if (x->n == 0 && x->offset ==0 ) {
446
+ left->flag |= MARKFREE;
447
+ WRITE (left.get ());
448
+ left->flag &= ~MARKFREE;
449
+ left->offset = 0 ;
450
+ }
430
451
WRITE (left.get ());
431
-
432
452
delete_op (left.get (), k);
433
453
return ;
434
454
} else if (right->n == T-1 ) {
435
455
std::cerr<<" case3b, right" ;
436
456
// copy x[i] to x.c[i]
437
- ci->key [x ->n ] = x->key [i];
457
+ ci->key [ci ->n ] = x->key [i];
438
458
ci->n = ci->n + 1 ;
439
459
// remove key[i] from x and also the child
440
460
// shrink the size & set the child-0 to ci
@@ -443,46 +463,41 @@ namespace alg {
443
463
x->key [j] = x->key [j+1 ];
444
464
}
445
465
446
- for (j=i;j<x->n ;j++) {
466
+ for (j=i+ 1 ;j<x->n ;j++) {
447
467
x->c [j] = x->c [j+1 ];
448
468
}
449
469
x->n = x->n - 1 ;
450
- x->c [i] = ci->offset ;
451
470
452
471
// append right sibling into x.c[i]
453
472
for (j=0 ;j<right->n ;j++) {
454
- ci->key [ci->n + j] =right->key [j];
473
+ ci->key [ci->n + j] = right->key [j];
455
474
}
456
475
457
476
for (j=0 ;j<right->n +1 ;j++) {
458
477
ci->c [ci->n + j] = right->c [j];
459
478
}
460
479
ci->n += right->n ; // ci became 2T-1
461
480
right->flag |= MARKFREE; // free right
481
+ right->n = 0 ;
462
482
WRITE (right.get ());
463
483
WRITE (x);
484
+ // root check
485
+ if (x->n == 0 && x->offset ==0 ) {
486
+ ci->flag |= MARKFREE;
487
+ WRITE (ci.get ());
488
+ ci->flag &= ~MARKFREE;
489
+ ci->offset = 0 ;
490
+ }
464
491
WRITE (ci.get ());
465
492
delete_op (ci.get (), k);
466
493
return ;
467
494
}
468
495
}
469
496
} else {
470
- int t;
471
- printf (" key[%d]\n " ,k);
472
497
delete_op (ci.get (), k);
473
498
}
474
499
}
475
500
476
-
477
- /* *
478
- * duplicate the node
479
- */
480
- node DUP (node x) {
481
- void *n = allocate_node ();
482
- memcpy (n, x, BLOCKSIZE);
483
- return (node)n;
484
- }
485
-
486
501
/* *
487
502
* Load the root block
488
503
*/
0 commit comments