@@ -231,32 +231,52 @@ namespace alg {
231
231
i = i - 1 ;
232
232
}
233
233
234
- if (x->key [i] == k) { // key in this node.
235
- if (x->flag & LEAF) { // if it's a leaf node. case 1.
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
+ if (x->flag & LEAF) {
236
238
int j;
237
- for (j = i;j<x->n -1 ;j++) { // shift copy
239
+ for (j = i;j<x->n -1 ;j++) { // shifting the keys.
238
240
x->key [j] = x->key [j+1 ];
239
241
}
240
242
WRITE (x);
241
243
return ;
242
- } else { // in non-leaf node
244
+ } else {
245
+ // case 2a:
246
+ // If the child y that precedes k in node x has at least t
247
+ // keys, then find the predecessor k0 of k in the subtree
248
+ // rooted at y. Recursively delete k0, and replace k by k0 in x.
249
+ // (We can find k0 and delete it in a single downward pass.)
243
250
std::auto_ptr<node_t > y (READ (x, i));
244
- if (y->n >= T) { // case 2a.
245
- x->key [i] = y->key [y->n -1 ];
251
+ if (y->n >= T) {
252
+ int32_t k0 = y->key [y->n -1 ];
253
+ x->key [i] = k0;
246
254
WRITE (x);
247
- delete_op (y.get (), x-> key [i] );
255
+ delete_op (y.get (), k0 );
248
256
return ;
249
257
}
250
-
258
+
259
+ // case 2b.
260
+ // If y has fewer than t keys, then, symmetrically, examine
261
+ // the child z that follows k in node x. If z has at least t keys,
262
+ // then find the successor k0 of k in the subtree rooted at z.
263
+ // Recursively delete k0, and replace k by k0 in x. (We can find k0
264
+ // and delete it in a single downward pass.)
251
265
std::auto_ptr<node_t > z (READ (x, i+1 ));
252
- if (z->n >= T) { // case 2b.
253
- x->key [i] = z->key [0 ];
266
+ if (z->n >= T) {
267
+ int32_t k0 = z->key [0 ];
268
+ x->key [i] = k0;
254
269
WRITE (x);
255
- delete_op (z.get (), x-> key [i] );
270
+ delete_op (z.get (), k0 );
256
271
return ;
257
272
}
258
273
259
- if (y->n == T-1 && z->n == T-1 ) { // case 2c
274
+ // case 2c:
275
+ // Otherwise, if both y and ´ have only t 2 1 keys,
276
+ // merge k and all of ´ into y, so that x loses both k and the
277
+ // pointer to ´, and y now contains 2t c 1 keys.
278
+ // Then free ´ and recursively delete k from y.
279
+ if (y->n == T-1 && z->n == T-1 ) {
260
280
// merge k & z into y
261
281
y->key [y->n ] = k;
262
282
0 commit comments