Skip to content

Commit a95d61a

Browse files
author
fuli
committed
update btree demo & comments
1 parent d0e4d7f commit a95d61a

File tree

2 files changed

+15
-20
lines changed

2 files changed

+15
-20
lines changed

include/btree.h

Lines changed: 11 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222
#define __BTREE_H__
2323

2424
#include <stdio.h>
25-
#include <iostream>
25+
#include <assert.h>
2626
#include <stdint.h>
2727
#include <stdlib.h>
2828
#include <string.h>
@@ -121,12 +121,6 @@ namespace alg {
121121
if (i<x->n && k == x->key[i]) { // search in [0,n-1]
122122
ret.offset = x->offset;
123123
ret.idx = i;
124-
/*
125-
int t;
126-
for (t=0;t<x->n;t++) {
127-
printf("%d ",x->key[t]);
128-
}
129-
*/
130124
return ret;
131125
} else if (x->flag & LEAF) { // leaf, no more childs
132126
ret.offset = 0;
@@ -246,10 +240,10 @@ namespace alg {
246240

247241
if (i >= 0 && x->key[i] == k) { // key exists in this node.
248242
if (x->flag & LEAF) {
249-
printf("in case 1 [%d] [%d]\n", i,x->n);
243+
//printf("in case 1 [%d] [%d]\n", i,x->n);
250244
case1(x, i, k);
251245
} else {
252-
printf("in case 2 [%d] [%d]\n", i,x->n);
246+
//printf("in case 2 [%d] [%d]\n", i,x->n);
253247
case2(x, i, k);
254248
}
255249
} else {
@@ -278,9 +272,8 @@ namespace alg {
278272
std::auto_ptr<node_t> y(READ(x, i));
279273
if (y->n >= T) {
280274
int32_t k0 = y->key[y->n-1];
281-
printf("case2a %d %d\n", k0, x->key[i]);
275+
//printf("case2a %d %d\n", k0, x->key[i]);
282276
x->key[i] = k0;
283-
printf("key[i] %d\n", k0, x->key[i]);
284277
WRITE(x);
285278
delete_op(y.get(), k0);
286279
return;
@@ -295,7 +288,7 @@ namespace alg {
295288
std::auto_ptr<node_t> z(READ(x, i+1));
296289
if (z->n >= T) {
297290
int32_t k0 = z->key[0];
298-
printf("case2b %d %d\n", k0, x->key[i]);
291+
//printf("case2b %d %d\n", k0, x->key[i]);
299292
x->key[i] = k0;
300293
WRITE(x);
301294
delete_op(z.get(), k0);
@@ -308,7 +301,7 @@ namespace alg {
308301
// pointer to z, and y now contains 2t - 1 keys.
309302
// Then free z and recursively delete k from y.
310303
if (y->n == T-1 && z->n == T-1) {
311-
printf("case2c");
304+
//printf("case2c");
312305
// merge k & z into y
313306
y->key[y->n] = k;
314307

@@ -340,7 +333,9 @@ namespace alg {
340333
delete_op(y.get(), k);
341334
return;
342335
}
343-
printf("other in case2");
336+
337+
// cannot reach here
338+
assert(false);
344339
}
345340

346341
void case3(node x, int32_t i, int32_t k) {
@@ -354,7 +349,7 @@ namespace alg {
354349
if (ci->n == T-1) {
355350
std::auto_ptr<node_t> left(READ(x, i-1));
356351
if (i-1>=0 && left->n >= T) {
357-
printf("case3a, left");
352+
// printf("case3a, left");
358353
// right shift keys and childs of x.c[i] to make place for a key
359354
// right shift ci childs
360355
int j;
@@ -381,7 +376,7 @@ namespace alg {
381376
// case 3a. right sibling
382377
std::auto_ptr<node_t> right(READ(x, i+1));
383378
if (i+1<=x->n && right->n >= T) {
384-
printf("case3a, right");
379+
// printf("case3a, right");
385380
ci->key[ci->n] = x->key[i]; // append key from x
386381
ci->c[ci->n+1] = right->c[0]; // append child from right
387382
ci->n = ci->n+1;
@@ -409,9 +404,7 @@ namespace alg {
409404
// with one sibling, which involves moving a key from x down into the new
410405
// merged node to become the median key for that node.
411406
if ((i-1<0 ||left->n == T-1) && (i+1 <=x->n || right->n == T-1)) {
412-
std::cerr << "case3b in";
413407
if (left->n == T-1) {
414-
std::cerr<<"case3b, left";
415408
// copy x[i] to left
416409
left->key[left->n] = x->key[i];
417410
left->n = left->n + 1;
@@ -452,7 +445,6 @@ namespace alg {
452445
delete_op(left.get(), k);
453446
return;
454447
} else if (right->n == T-1) {
455-
std::cerr<<"case3b, right";
456448
// copy x[i] to x.c[i]
457449
ci->key[ci->n] = x->key[i];
458450
ci->n = ci->n + 1;

src/btree_demo.cpp

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,14 @@ int main(void) {
88

99
for (i=0;i<1000;i++) {
1010
x.Insert(i);
11+
printf("insert %d\n", i);
1112
}
1213

1314
for (i=0;i<1000;i++) {
1415
x.DeleteKey(i);
1516
BTree::nr r = x.Search(i);
16-
printf("key[%d] offset[%x] idx[%d]\n", i, r.offset, r.idx);
17+
if (r.idx == -1) {
18+
printf("key[%d] removed\n", i);
19+
}
1720
}
1821
}

0 commit comments

Comments
 (0)