22
22
#define __BTREE_H__
23
23
24
24
#include < stdio.h>
25
- #include < iostream >
25
+ #include < assert.h >
26
26
#include < stdint.h>
27
27
#include < stdlib.h>
28
28
#include < string.h>
@@ -121,12 +121,6 @@ namespace alg {
121
121
if (i<x->n && k == x->key [i]) { // search in [0,n-1]
122
122
ret.offset = x->offset ;
123
123
ret.idx = i;
124
- /*
125
- int t;
126
- for (t=0;t<x->n;t++) {
127
- printf("%d ",x->key[t]);
128
- }
129
- */
130
124
return ret;
131
125
} else if (x->flag & LEAF) { // leaf, no more childs
132
126
ret.offset = 0 ;
@@ -246,10 +240,10 @@ namespace alg {
246
240
247
241
if (i >= 0 && x->key [i] == k) { // key exists in this node.
248
242
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);
250
244
case1 (x, i, k);
251
245
} else {
252
- printf (" in case 2 [%d] [%d]\n " , i,x->n );
246
+ // printf("in case 2 [%d] [%d]\n", i,x->n);
253
247
case2 (x, i, k);
254
248
}
255
249
} else {
@@ -278,9 +272,8 @@ namespace alg {
278
272
std::auto_ptr<node_t > y (READ (x, i));
279
273
if (y->n >= T) {
280
274
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]);
282
276
x->key [i] = k0;
283
- printf (" key[i] %d\n " , k0, x->key [i]);
284
277
WRITE (x);
285
278
delete_op (y.get (), k0);
286
279
return ;
@@ -295,7 +288,7 @@ namespace alg {
295
288
std::auto_ptr<node_t > z (READ (x, i+1 ));
296
289
if (z->n >= T) {
297
290
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]);
299
292
x->key [i] = k0;
300
293
WRITE (x);
301
294
delete_op (z.get (), k0);
@@ -308,7 +301,7 @@ namespace alg {
308
301
// pointer to z, and y now contains 2t - 1 keys.
309
302
// Then free z and recursively delete k from y.
310
303
if (y->n == T-1 && z->n == T-1 ) {
311
- printf (" case2c" );
304
+ // printf("case2c");
312
305
// merge k & z into y
313
306
y->key [y->n ] = k;
314
307
@@ -340,7 +333,9 @@ namespace alg {
340
333
delete_op (y.get (), k);
341
334
return ;
342
335
}
343
- printf (" other in case2" );
336
+
337
+ // cannot reach here
338
+ assert (false );
344
339
}
345
340
346
341
void case3 (node x, int32_t i, int32_t k) {
@@ -354,7 +349,7 @@ namespace alg {
354
349
if (ci->n == T-1 ) {
355
350
std::auto_ptr<node_t > left (READ (x, i-1 ));
356
351
if (i-1 >=0 && left->n >= T) {
357
- printf (" case3a, left" );
352
+ // printf("case3a, left");
358
353
// right shift keys and childs of x.c[i] to make place for a key
359
354
// right shift ci childs
360
355
int j;
@@ -381,7 +376,7 @@ namespace alg {
381
376
// case 3a. right sibling
382
377
std::auto_ptr<node_t > right (READ (x, i+1 ));
383
378
if (i+1 <=x->n && right->n >= T) {
384
- printf (" case3a, right" );
379
+ // printf("case3a, right");
385
380
ci->key [ci->n ] = x->key [i]; // append key from x
386
381
ci->c [ci->n +1 ] = right->c [0 ]; // append child from right
387
382
ci->n = ci->n +1 ;
@@ -409,9 +404,7 @@ namespace alg {
409
404
// with one sibling, which involves moving a key from x down into the new
410
405
// merged node to become the median key for that node.
411
406
if ((i-1 <0 ||left->n == T-1 ) && (i+1 <=x->n || right->n == T-1 )) {
412
- std::cerr << " case3b in" ;
413
407
if (left->n == T-1 ) {
414
- std::cerr<<" case3b, left" ;
415
408
// copy x[i] to left
416
409
left->key [left->n ] = x->key [i];
417
410
left->n = left->n + 1 ;
@@ -452,7 +445,6 @@ namespace alg {
452
445
delete_op (left.get (), k);
453
446
return ;
454
447
} else if (right->n == T-1 ) {
455
- std::cerr<<" case3b, right" ;
456
448
// copy x[i] to x.c[i]
457
449
ci->key [ci->n ] = x->key [i];
458
450
ci->n = ci->n + 1 ;
0 commit comments