Skip to content

Commit 502da5f

Browse files
committed
adding comment
1 parent 70e5688 commit 502da5f

File tree

1 file changed

+26
-19
lines changed

1 file changed

+26
-19
lines changed

include/btree.h

Lines changed: 26 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -232,9 +232,9 @@ namespace alg {
232232
}
233233

234234
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.
237235
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.
238238
int j;
239239
for (j = i;j<x->n-1;j++) { // shifting the keys.
240240
x->key[j] = x->key[j+1];
@@ -310,27 +310,33 @@ namespace alg {
310310
}
311311

312312
}
313-
} else { // case 3
314-
i = i+1; // child to search
313+
} else {
314+
i = i+1; // child ci
315315
std::auto_ptr<node_t> ci(READ(x, i));
316316

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].
318322
if (ci->n == T-1) {
319323
std::auto_ptr<node_t> left(READ(x, i-1));
320324
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
321327
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];
324330
}
325331

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];
328334
}
329335
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
334340

335341
WRITE(ci.get());
336342
WRITE(x);
@@ -342,24 +348,25 @@ namespace alg {
342348
// case 3a. right sibling
343349
std::auto_ptr<node_t> right(READ(x, i+1));
344350
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
347353
ci->n = ci->n+1;
348-
x->key[i] = right->key[0];
354+
x->key[i] = right->key[0]; // subsitute key in x
349355

350356
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
352358
right->key[j] = right->key[j+1];
353359
}
354360

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.
356362
right->c[j] = right->c[j+1];
357363
}
364+
right->n = right->n - 1; // reduce the size of the right sibling.
358365

359366
WRITE(ci.get());
360367
WRITE(x);
361368
WRITE(right.get());
362-
delete_op(ci.get(), k);
369+
delete_op(ci.get(), k); // recursive delete key in x.c[i]
363370
return;
364371
}
365372

0 commit comments

Comments
 (0)