@@ -226,6 +226,15 @@ namespace alg {
226226 */
227227void delete_op (node x, int32_t k) {
228228int32_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+
229238if (x->n == 0 ) {// emtpy node
230239return ;
231240}
@@ -234,20 +243,13 @@ namespace alg {
234243while (i>=0 && k < x->key [i]) { // search the key.
235244i = i - 1 ;
236245}
237-
246+
238247if (i >= 0 && x->key [i] == k) {// key exists in this node.
239- printf (" in case 1 & 2 [%d] [%d]\n " , i,x->n );
240248if (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);
250251} else {
252+ printf (" in case 2 [%d] [%d]\n " , i,x->n );
251253case2 (x, i, k);
252254}
253255} else {
@@ -256,6 +258,17 @@ namespace alg {
256258}
257259}
258260
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+
259272void case2 (node x, int32_t i, int32_t k) {
260273// case 2a:
261274// If the child y that precedes k in node x has at least t
@@ -301,10 +314,10 @@ namespace alg {
301314
302315int j;
303316for (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];
305318}
306319for (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];
308321}
309322
310323// mark free z
@@ -314,11 +327,11 @@ namespace alg {
314327WRITE (y.get ());
315328
316329for (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 ];
318331}
319332
320333for (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 ];
322335}
323336x->n = x->n - 1 ;
324337WRITE (x);
@@ -332,6 +345,7 @@ namespace alg {
332345
333346void case3 (node x, int32_t i, int32_t k) {
334347std::auto_ptr<node_t > ci (READ (x, i));
348+
335349// case 3a.
336350// If x.c[i] has only t - 1 keys but has an immediate sibling with at least t keys,
337351// 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 {
409423x->key [j] = x->key [j+1 ];
410424}
411425
412- for (j=i;j<x->n ;j++) {
426+ for (j=i+ 1 ;j<x->n ;j++) {
413427x->c [j] = x->c [j+1 ];
414428}
415429x->n = x->n - 1 ;
416- x->c [0 ] = left->offset ;
417430
418431// append x.c[i] into left sibling
419432for (j=0 ;j<ci->n ;j++) {
@@ -425,16 +438,23 @@ namespace alg {
425438}
426439left->n += ci->n ;// left became 2T-1
427440ci->flag |= MARKFREE;// free ci
441+ ci->n = 0 ;
428442WRITE (ci.get ());
429443WRITE (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+ }
430451WRITE (left.get ());
431-
432452delete_op (left.get (), k);
433453return ;
434454} else if (right->n == T-1 ) {
435455std::cerr<<" case3b, right" ;
436456// copy x[i] to x.c[i]
437- ci->key [x ->n ] = x->key [i];
457+ ci->key [ci ->n ] = x->key [i];
438458ci->n = ci->n + 1 ;
439459// remove key[i] from x and also the child
440460// shrink the size & set the child-0 to ci
@@ -443,46 +463,41 @@ namespace alg {
443463x->key [j] = x->key [j+1 ];
444464}
445465
446- for (j=i;j<x->n ;j++) {
466+ for (j=i+ 1 ;j<x->n ;j++) {
447467x->c [j] = x->c [j+1 ];
448468}
449469x->n = x->n - 1 ;
450- x->c [i] = ci->offset ;
451470
452471// append right sibling into x.c[i]
453472for (j=0 ;j<right->n ;j++) {
454- ci->key [ci->n + j] =right->key [j];
473+ ci->key [ci->n + j] = right->key [j];
455474}
456475
457476for (j=0 ;j<right->n +1 ;j++) {
458477ci->c [ci->n + j] = right->c [j];
459478}
460479ci->n += right->n ;// ci became 2T-1
461480right->flag |= MARKFREE;// free right
481+ right->n = 0 ;
462482WRITE (right.get ());
463483WRITE (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+ }
464491WRITE (ci.get ());
465492delete_op (ci.get (), k);
466493return ;
467494}
468495}
469496} else {
470- int t;
471- printf (" key[%d]\n " ,k);
472497delete_op (ci.get (), k);
473498}
474499}
475500
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-
486501/* *
487502 * Load the root block
488503 */
0 commit comments