@@ -232,9 +232,9 @@ namespace alg {
232
232
}
233
233
234
234
if (x->key [i] == k) { // key exists in this node.
235
- // case 1.
236
- // If the key k is in node x and x is a leaf, delete the key k from x.
237
235
if (x->flag & LEAF) {
236
+ // case 1.
237
+ // If the key k is in node x and x is a leaf, delete the key k from x.
238
238
int j;
239
239
for (j = i;j<x->n -1 ;j++) { // shifting the keys.
240
240
x->key [j] = x->key [j+1 ];
@@ -310,27 +310,33 @@ namespace alg {
310
310
}
311
311
312
312
}
313
- } else { // case 3
314
- i = i+1 ; // child to search
313
+ } else {
314
+ i = i+1 ; // child ci
315
315
std::auto_ptr<node_t > ci (READ (x, i));
316
316
317
- // case 3a. left sibling
317
+ // case 3a.
318
+ // If x.c[i] has only t - 1 keys but has an immediate sibling with at least t keys,
319
+ // give x.c[i] an extra key by moving a key from x down into x.c[i], moving a
320
+ // key from x.c[i]’s immediate left or right sibling up into x, and moving the
321
+ // appropriate child pointer from the sibling into x.c[i].
318
322
if (ci->n == T-1 ) {
319
323
std::auto_ptr<node_t > left (READ (x, i-1 ));
320
324
if (i-1 >=0 && left->n > T) {
325
+ // right shift keys and childs of x.c[i] to make place for a key
326
+ // right shift ci childs
321
327
int j;
322
- for (j=ci->n -2 ;j>= 0 ;j++) { // right shift ci keys
323
- ci->key [j+ 1 ] = ci->key [j];
328
+ for (j=ci->n -1 ;j>0 ;j++) {
329
+ ci->key [j] = ci->key [j- 1 ];
324
330
}
325
331
326
- for (j=ci->n - 1 ;j>= 0 ;j++) { // right shift ci childs
327
- ci->c [j+ 1 ] = ci->c [j];
332
+ for (j=ci->n ;j>0 ;j++) {
333
+ ci->c [j] = ci->c [j- 1 ];
328
334
}
329
335
ci->n = ci->n +1 ;
330
- ci->key [0 ] = x->key [i]; // copy key from x[i] to ci[0]
331
- ci->c [0 ] = left->c [left->n ]; // copy child pointer from left last child
332
- x->key [i] = left->key [left->n -1 ]; // copy from left last key
333
- left->n = left->n -1 ; // decrease left num keys
336
+ ci->key [0 ] = x->key [i]; // copy key from x[i] to ci[0]
337
+ ci->c [0 ] = left->c [left->n ]; // copy child from left last child.
338
+ x->key [i] = left->key [left->n -1 ]; // copy left last key into x[i]
339
+ left->n = left->n -1 ; // decrease left size
334
340
335
341
WRITE (ci.get ());
336
342
WRITE (x);
@@ -342,24 +348,25 @@ namespace alg {
342
348
// case 3a. right sibling
343
349
std::auto_ptr<node_t > right (READ (x, i+1 ));
344
350
if (i+1 <ci->n && right->n > T) {
345
- ci->key [ci->n - 1 ] = x->key [i];
346
- ci->c [ci->n ] = right->c [0 ];
351
+ ci->key [ci->n ] = x->key [i]; // append key from x
352
+ ci->c [ci->n + 1 ] = right->c [0 ]; // append child from right
347
353
ci->n = ci->n +1 ;
348
- x->key [i] = right->key [0 ];
354
+ x->key [i] = right->key [0 ]; // subsitute key in x
349
355
350
356
int j;
351
- for (j=0 ;j<right->n -1 ;j++) { // left shift sibling keys
357
+ for (j=0 ;j<right->n -1 ;j++) { // remove key[0] from right sibling
352
358
right->key [j] = right->key [j+1 ];
353
359
}
354
360
355
- for (j=0 ;j<right->n ;j++) { // left shift ci childs
361
+ for (j=0 ;j<right->n ;j++) { // and also the child c[0] of the right sibling.
356
362
right->c [j] = right->c [j+1 ];
357
363
}
364
+ right->n = right->n - 1 ; // reduce the size of the right sibling.
358
365
359
366
WRITE (ci.get ());
360
367
WRITE (x);
361
368
WRITE (right.get ());
362
- delete_op (ci.get (), k);
369
+ delete_op (ci.get (), k); // recursive delete key in x.c[i]
363
370
return ;
364
371
}
365
372
0 commit comments