Skip to content

Commit d0e4d7f

Browse files
committed
finish test with btree.
1 parent e62428b commit d0e4d7f

File tree

2 files changed

+51
-36
lines changed

2 files changed

+51
-36
lines changed

include/btree.h

Lines changed: 49 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -226,6 +226,15 @@ namespace alg {
226226
*/
227227
void delete_op(node x, int32_t k) {
228228
int32_t i;
229+
/*
230+
int t;
231+
printf("key:%d n:%d\n",k, x->n);
232+
for (t=0;t<x->n;t++) {
233+
printf("=%d=", x->key[t]);
234+
}
235+
printf("\n");
236+
*/
237+
229238
if (x->n == 0) { // emtpy node
230239
return;
231240
}
@@ -234,20 +243,13 @@ namespace alg {
234243
while (i>=0 && k < x->key[i]) { // search the key.
235244
i = i - 1;
236245
}
237-
246+
238247
if (i >= 0 && x->key[i] == k) { // key exists in this node.
239-
printf("in case 1 & 2 [%d] [%d]\n", i,x->n);
240248
if (x->flag & LEAF) {
241-
// case 1.
242-
// If the key k is in node x and x is a leaf, delete the key k from x.
243-
int j;
244-
for (j = i;j<x->n-1;j++) { // shifting the keys.
245-
x->key[j] = x->key[j+1];
246-
}
247-
WRITE(x);
248-
printf("deleted %d offset %x\n", k, x);
249-
return;
249+
printf("in case 1 [%d] [%d]\n", i,x->n);
250+
case1(x, i, k);
250251
} else {
252+
printf("in case 2 [%d] [%d]\n", i,x->n);
251253
case2(x, i, k);
252254
}
253255
} else {
@@ -256,6 +258,17 @@ namespace alg {
256258
}
257259
}
258260

261+
void case1(node x, int32_t i, int32_t k) {
262+
// case 1.
263+
// If the key k is in node x and x is a leaf, delete the key k from x.
264+
int j;
265+
for (j = i;j<x->n-1;j++) { // shifting the keys.
266+
x->key[j] = x->key[j+1];
267+
}
268+
x->n = x->n - 1;
269+
WRITE(x);
270+
}
271+
259272
void case2(node x, int32_t i, int32_t k) {
260273
// case 2a:
261274
// If the child y that precedes k in node x has at least t
@@ -301,10 +314,10 @@ namespace alg {
301314

302315
int j;
303316
for (j=0;j<z->n;j++) { // merge keys of z
304-
y->key[y->n+j+1] = z->key[j];
317+
y->key[y->n+1+j] = z->key[j];
305318
}
306319
for (j=0;j<z->n+1;j++) { // merge childs of z
307-
y->c[y->n+j+1] = z->c[j];
320+
y->c[y->n+1+j] = z->c[j];
308321
}
309322

310323
// mark free z
@@ -314,11 +327,11 @@ namespace alg {
314327
WRITE(y.get());
315328

316329
for (j=i;j<x->n-1;j++) { // delete k from node x
317-
x->key[i] = x->key[i+1];
330+
x->key[j] = x->key[j+1];
318331
}
319332

320333
for (j=i+1;j<x->n;j++){ // delete pointer to z --> (i+1)th
321-
x->c[i] = x->c[i+1];
334+
x->c[j] = x->c[j+1];
322335
}
323336
x->n = x->n - 1;
324337
WRITE(x);
@@ -332,6 +345,7 @@ namespace alg {
332345

333346
void case3(node x, int32_t i, int32_t k) {
334347
std::auto_ptr<node_t> ci(READ(x, i));
348+
335349
// case 3a.
336350
// If x.c[i] has only t - 1 keys but has an immediate sibling with at least t keys,
337351
// give x.c[i] an extra key by moving a key from x down into x.c[i], moving a
@@ -409,11 +423,10 @@ namespace alg {
409423
x->key[j] = x->key[j+1];
410424
}
411425

412-
for (j=i;j<x->n;j++) {
426+
for (j=i+1;j<x->n;j++) {
413427
x->c[j] = x->c[j+1];
414428
}
415429
x->n = x->n - 1;
416-
x->c[0] = left->offset;
417430

418431
// append x.c[i] into left sibling
419432
for (j=0;j<ci->n;j++) {
@@ -425,16 +438,23 @@ namespace alg {
425438
}
426439
left->n += ci->n; // left became 2T-1
427440
ci->flag |= MARKFREE; // free ci
441+
ci->n = 0;
428442
WRITE(ci.get());
429443
WRITE(x);
444+
// root check
445+
if (x->n == 0 && x->offset ==0) {
446+
left->flag |= MARKFREE;
447+
WRITE(left.get());
448+
left->flag &= ~MARKFREE;
449+
left->offset = 0;
450+
}
430451
WRITE(left.get());
431-
432452
delete_op(left.get(), k);
433453
return;
434454
} else if (right->n == T-1) {
435455
std::cerr<<"case3b, right";
436456
// copy x[i] to x.c[i]
437-
ci->key[x->n] = x->key[i];
457+
ci->key[ci->n] = x->key[i];
438458
ci->n = ci->n + 1;
439459
// remove key[i] from x and also the child
440460
// shrink the size & set the child-0 to ci
@@ -443,46 +463,41 @@ namespace alg {
443463
x->key[j] = x->key[j+1];
444464
}
445465

446-
for (j=i;j<x->n;j++) {
466+
for (j=i+1;j<x->n;j++) {
447467
x->c[j] = x->c[j+1];
448468
}
449469
x->n = x->n - 1;
450-
x->c[i] = ci->offset;
451470

452471
// append right sibling into x.c[i]
453472
for (j=0;j<right->n;j++) {
454-
ci->key[ci->n + j] =right->key[j];
473+
ci->key[ci->n + j] = right->key[j];
455474
}
456475

457476
for (j=0;j<right->n+1;j++) {
458477
ci->c[ci->n + j] = right->c[j];
459478
}
460479
ci->n += right->n; // ci became 2T-1
461480
right->flag |= MARKFREE; // free right
481+
right->n = 0;
462482
WRITE(right.get());
463483
WRITE(x);
484+
// root check
485+
if (x->n == 0 && x->offset ==0) {
486+
ci->flag |= MARKFREE;
487+
WRITE(ci.get());
488+
ci->flag &= ~MARKFREE;
489+
ci->offset = 0;
490+
}
464491
WRITE(ci.get());
465492
delete_op(ci.get(), k);
466493
return;
467494
}
468495
}
469496
} else {
470-
int t;
471-
printf("key[%d]\n",k);
472497
delete_op(ci.get(), k);
473498
}
474499
}
475500

476-
477-
/**
478-
* duplicate the node
479-
*/
480-
node DUP(node x) {
481-
void *n = allocate_node();
482-
memcpy(n, x, BLOCKSIZE);
483-
return (node)n;
484-
}
485-
486501
/**
487502
* Load the root block
488503
*/

src/btree_demo.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,11 @@ int main(void) {
66
BTree x("./btree.dat");
77
int32_t i;
88

9-
for (i=0;i<100000;i++) {
9+
for (i=0;i<1000;i++) {
1010
x.Insert(i);
1111
}
1212

13-
for (i=0;i<100000;i++) {
13+
for (i=0;i<1000;i++) {
1414
x.DeleteKey(i);
1515
BTree::nr r = x.Search(i);
1616
printf("key[%d] offset[%x] idx[%d]\n", i, r.offset, r.idx);

0 commit comments

Comments
 (0)