sandbox: btree: deletion fixup bugfix
This commit is contained in:
@@ -10,7 +10,7 @@
|
|||||||
#include <socks/memblock.h>
|
#include <socks/memblock.h>
|
||||||
#include <socks/vm.h>
|
#include <socks/vm.h>
|
||||||
|
|
||||||
#define NR_BTREE_NODES 13
|
#define NR_BTREE_NODES 32
|
||||||
|
|
||||||
/* we're working with 512MiB of simulated system RAM */
|
/* we're working with 512MiB of simulated system RAM */
|
||||||
#define MEMORY_SIZE_MB 512
|
#define MEMORY_SIZE_MB 512
|
||||||
@@ -130,10 +130,28 @@ static int memory_test(void)
|
|||||||
|
|
||||||
void btree_print(btree_node_t *node, int depth)
|
void btree_print(btree_node_t *node, int depth)
|
||||||
{
|
{
|
||||||
|
if (depth > 10) {
|
||||||
|
for (int i = 0; i < depth; i++) {
|
||||||
|
fputs(" ", stdout);
|
||||||
|
}
|
||||||
|
|
||||||
|
printf("OVERFLOW\n");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
if (!node) {
|
if (!node) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (node->b_parent && node != node->b_parent->b_left && node != node->b_parent->b_right) {
|
||||||
|
for (int i = 0; i < depth; i++) {
|
||||||
|
fputs(" ", stdout);
|
||||||
|
}
|
||||||
|
|
||||||
|
printf("BAD PARENT [%llu]\n", node->b_key);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
if (node) {
|
if (node) {
|
||||||
btree_print(node->b_right, depth + 1);
|
btree_print(node->b_right, depth + 1);
|
||||||
}
|
}
|
||||||
@@ -146,9 +164,12 @@ void btree_print(btree_node_t *node, int depth)
|
|||||||
if (node->b_parent) {
|
if (node->b_parent) {
|
||||||
if (node == node->b_parent->b_left) {
|
if (node == node->b_parent->b_left) {
|
||||||
printf("\\ ");
|
printf("\\ ");
|
||||||
} else {
|
} else if (node == node->b_parent->b_right) {
|
||||||
printf("/ ");
|
printf("/ ");
|
||||||
|
} else {
|
||||||
|
printf("? ");
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
printf("%llu (h:%d)\n", node->b_key, node->b_height);
|
printf("%llu (h:%d)\n", node->b_key, node->b_height);
|
||||||
@@ -172,7 +193,7 @@ static int btree_avl_validate(btree_node_t *x)
|
|||||||
}
|
}
|
||||||
|
|
||||||
if (!x->b_left && !x->b_right) {
|
if (!x->b_left && !x->b_right) {
|
||||||
return 1;
|
return x->b_height == 1 ? 1 : -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
int left = 0, right = 0;
|
int left = 0, right = 0;
|
||||||
@@ -241,10 +262,6 @@ static int btree_test(void)
|
|||||||
printf("deleting node #%d: %llu\n", i, nodes[i].b_key);
|
printf("deleting node #%d: %llu\n", i, nodes[i].b_key);
|
||||||
printf("#######################\n");
|
printf("#######################\n");
|
||||||
|
|
||||||
if (nodes[i].b_key == 89) {
|
|
||||||
printf("brk\n");
|
|
||||||
}
|
|
||||||
|
|
||||||
btree_delete(&tree, &nodes[i]);
|
btree_delete(&tree, &nodes[i]);
|
||||||
btree_print(tree.b_root, 0);
|
btree_print(tree.b_root, 0);
|
||||||
|
|
||||||
|
|||||||
@@ -222,15 +222,18 @@ static void delete_fixup(btree_t *tree, btree_node_t *w)
|
|||||||
int nr_rotations = 0;
|
int nr_rotations = 0;
|
||||||
|
|
||||||
while (z) {
|
while (z) {
|
||||||
|
printf("bf(z)=%d\n", bf(z));
|
||||||
|
printf("bf(z->b_left)=%d\n", bf(z->b_left));
|
||||||
|
|
||||||
if (bf(z) > 1) {
|
if (bf(z) > 1) {
|
||||||
if (bf(z->b_left) <= 0) {
|
if (bf(z->b_right) >= 0) {
|
||||||
rotate_left(tree, z);
|
rotate_left(tree, z);
|
||||||
update_height_to_root(z);
|
update_height_to_root(z);
|
||||||
} else {
|
} else {
|
||||||
rotate_double_left(tree, z);
|
rotate_double_left(tree, z); // <==
|
||||||
}
|
}
|
||||||
} else if (bf(z) < -1) {
|
} else if (bf(z) < -1) {
|
||||||
if (bf(z->b_right) > 0) {
|
if (bf(z->b_left) <= 0) {
|
||||||
rotate_right(tree, z);
|
rotate_right(tree, z);
|
||||||
update_height_to_root(z);
|
update_height_to_root(z);
|
||||||
} else {
|
} else {
|
||||||
@@ -354,62 +357,78 @@ static btree_node_t *replace_node_with_one_subtree(btree_t *tree, btree_node_t *
|
|||||||
return w;
|
return w;
|
||||||
}
|
}
|
||||||
|
|
||||||
static btree_node_t *replace_node_with_two_subtrees(btree_t *tree, btree_node_t *node)
|
static btree_node_t *replace_node_with_two_subtrees(btree_t *tree, btree_node_t *z)
|
||||||
{
|
{
|
||||||
debug_msg("remove_node_with_two_subtrees()\n");
|
debug_msg("remove_node_with_two_subtrees()\n");
|
||||||
btree_node_t *cur = node->b_left;
|
|
||||||
|
|
||||||
while (cur->b_right) {
|
/* x will replace z */
|
||||||
cur = cur->b_right;
|
btree_node_t *x = z->b_left;
|
||||||
|
|
||||||
|
while (x->b_right) {
|
||||||
|
x = x->b_right;
|
||||||
}
|
}
|
||||||
|
|
||||||
btree_node_t *p = cur->b_parent;
|
/* y is the node that will replace x (if x has a left child) */
|
||||||
btree_node_t *k = p;
|
btree_node_t *y = x->b_left;
|
||||||
|
|
||||||
if (IS_LEFT_CHILD(p, cur)) {
|
/* w is the starting point for the height update and fixup */
|
||||||
p->b_left = NULL;
|
btree_node_t *w = x;
|
||||||
} else {
|
if (w->b_parent != z) {
|
||||||
p->b_right = NULL;
|
w = w->b_parent;
|
||||||
}
|
}
|
||||||
|
|
||||||
p = node->b_parent;
|
if (y) {
|
||||||
|
w = y;
|
||||||
if (p) {
|
|
||||||
if (IS_LEFT_CHILD(p, node)) {
|
|
||||||
p->b_left = cur;
|
|
||||||
} else {
|
|
||||||
p->b_right = cur;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
cur->b_left = node->b_left;
|
if (IS_LEFT_CHILD(x->b_parent, x)) {
|
||||||
cur->b_right = node->b_right;
|
x->b_parent->b_left = y;
|
||||||
cur->b_height = node->b_height;
|
} else if (IS_RIGHT_CHILD(x->b_parent, x)) {
|
||||||
cur->b_parent = p;
|
x->b_parent->b_right = y;
|
||||||
btree_node_t *w = cur;
|
|
||||||
|
|
||||||
if (!p) {
|
|
||||||
tree->b_root = cur;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if (tree->b_root->b_left) {
|
if (y) {
|
||||||
tree->b_root->b_left->b_parent = tree->b_root;
|
y->b_parent = x->b_parent;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (tree->b_root->b_right) {
|
if (IS_LEFT_CHILD(z->b_parent, z)) {
|
||||||
tree->b_root->b_right->b_parent = tree->b_root;
|
z->b_parent->b_left = x;
|
||||||
|
} else if (IS_RIGHT_CHILD(z->b_parent, z)) {
|
||||||
|
z->b_parent->b_right = x;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
x->b_parent = z->b_parent;
|
||||||
|
x->b_left = z->b_left;
|
||||||
|
x->b_right = z->b_right;
|
||||||
|
|
||||||
|
if (x->b_left) {
|
||||||
|
x->b_left->b_parent = x;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (x->b_right) {
|
||||||
|
x->b_right->b_parent = x;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!x->b_parent) {
|
||||||
|
tree->b_root = x;
|
||||||
|
}
|
||||||
|
|
||||||
|
btree_node_t *cur = w;
|
||||||
while (cur) {
|
while (cur) {
|
||||||
update_height(cur);
|
update_height(cur);
|
||||||
cur = cur->b_parent;
|
cur = cur->b_parent;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
printf("w=%llu, h:%u\n", w->b_key, w->b_height);
|
||||||
return w;
|
return w;
|
||||||
}
|
}
|
||||||
|
|
||||||
void btree_delete(btree_t *tree, btree_node_t *node)
|
void btree_delete(btree_t *tree, btree_node_t *node)
|
||||||
{
|
{
|
||||||
|
if (!node->b_owner) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
assert(node->b_owner == tree);
|
assert(node->b_owner == tree);
|
||||||
btree_node_t *w = NULL;
|
btree_node_t *w = NULL;
|
||||||
|
|
||||||
@@ -421,6 +440,8 @@ void btree_delete(btree_t *tree, btree_node_t *node)
|
|||||||
w = replace_node_with_two_subtrees(tree, node);
|
w = replace_node_with_two_subtrees(tree, node);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
btree_print(tree->b_root, 0);
|
||||||
|
|
||||||
if (w) {
|
if (w) {
|
||||||
delete_fixup(tree, w);
|
delete_fixup(tree, w);
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user