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 {
121121if (i<x->n && k == x->key [i]) {// search in [0,n-1]
122122ret.offset = x->offset ;
123123ret.idx = i;
124- /*
125- int t;
126- for (t=0;t<x->n;t++) {
127- printf("%d ",x->key[t]);
128- }
129- */
130124return ret;
131125} else if (x->flag & LEAF) {// leaf, no more childs
132126ret.offset = 0 ;
@@ -246,10 +240,10 @@ namespace alg {
246240
247241if (i >= 0 && x->key [i] == k) {// key exists in this node.
248242if (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);
250244case1 (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);
253247case2 (x, i, k);
254248}
255249} else {
@@ -278,9 +272,8 @@ namespace alg {
278272std::auto_ptr<node_t > y (READ (x, i));
279273if (y->n >= T) {
280274int32_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]);
282276x->key [i] = k0;
283- printf (" key[i] %d\n " , k0, x->key [i]);
284277WRITE (x);
285278delete_op (y.get (), k0);
286279return ;
@@ -295,7 +288,7 @@ namespace alg {
295288std::auto_ptr<node_t > z (READ (x, i+1 ));
296289if (z->n >= T) {
297290int32_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]);
299292x->key [i] = k0;
300293WRITE (x);
301294delete_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.
310303if (y->n == T-1 && z->n == T-1 ) {
311- printf (" case2c" );
304+ // printf("case2c");
312305// merge k & z into y
313306y->key [y->n ] = k;
314307
@@ -340,7 +333,9 @@ namespace alg {
340333delete_op (y.get (), k);
341334return ;
342335}
343- printf (" other in case2" );
336+
337+ // cannot reach here
338+ assert (false );
344339}
345340
346341void case3 (node x, int32_t i, int32_t k) {
@@ -354,7 +349,7 @@ namespace alg {
354349if (ci->n == T-1 ) {
355350std::auto_ptr<node_t > left (READ (x, i-1 ));
356351if (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
360355int j;
@@ -381,7 +376,7 @@ namespace alg {
381376// case 3a. right sibling
382377std::auto_ptr<node_t > right (READ (x, i+1 ));
383378if (i+1 <=x->n && right->n >= T) {
384- printf (" case3a, right" );
379+ // printf("case3a, right");
385380ci->key [ci->n ] = x->key [i];// append key from x
386381ci->c [ci->n +1 ] = right->c [0 ];// append child from right
387382ci->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.
411406if ((i-1 <0 ||left->n == T-1 ) && (i+1 <=x->n || right->n == T-1 )) {
412- std::cerr << " case3b in" ;
413407if (left->n == T-1 ) {
414- std::cerr<<" case3b, left" ;
415408// copy x[i] to left
416409left->key [left->n ] = x->key [i];
417410left->n = left->n + 1 ;
@@ -452,7 +445,6 @@ namespace alg {
452445delete_op (left.get (), k);
453446return ;
454447} else if (right->n == T-1 ) {
455- std::cerr<<" case3b, right" ;
456448// copy x[i] to x.c[i]
457449ci->key [ci->n ] = x->key [i];
458450ci->n = ci->n + 1 ;
0 commit comments