Skip to content

Commit 7ea936f

Browse files
committed
add comment for btree
1 parent 0e6e367 commit 7ea936f

File tree

1 file changed

+32
-12
lines changed

1 file changed

+32
-12
lines changed

include/btree.h

Lines changed: 32 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -231,32 +231,52 @@ namespace alg {
231231
i = i - 1;
232232
}
233233

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) {
236238
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.
238240
x->key[j] = x->key[j+1];
239241
}
240242
WRITE(x);
241243
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.)
243250
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;
246254
WRITE(x);
247-
delete_op(y.get(), x->key[i]);
255+
delete_op(y.get(), k0);
248256
return;
249257
}
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.)
251265
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;
254269
WRITE(x);
255-
delete_op(z.get(), x->key[i]);
270+
delete_op(z.get(), k0);
256271
return;
257272
}
258273

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) {
260280
// merge k & z into y
261281
y->key[y->n] = k;
262282

0 commit comments

Comments
 (0)