Skip to content

Commit

Permalink
Deletion algorithm fix
Browse files Browse the repository at this point in the history
  • Loading branch information
strohsnow committed Jul 22, 2023
1 parent c9d2863 commit 383f2ff
Showing 1 changed file with 14 additions and 11 deletions.
25 changes: 14 additions & 11 deletions src/containers/qtreetbl.c
Original file line number Diff line number Diff line change
Expand Up @@ -1062,17 +1062,20 @@ static qtreetbl_obj_t *rotate_right(qtreetbl_obj_t *obj) {

static qtreetbl_obj_t *move_red_left(qtreetbl_obj_t *obj) {
flip_color(obj);
if (obj->right && is_red(obj->right->left)) {
if (is_red(obj->right->left)) {
obj->right = rotate_right(obj->right);
obj = rotate_left(obj);
flip_color(obj);
if (is_red(obj->right->right)) {
obj->right = rotate_left(obj->right);
}
}
return obj;
}

static qtreetbl_obj_t *move_red_right(qtreetbl_obj_t *obj) {
flip_color(obj);
if (obj->left && is_red(obj->left->left)) {
if (is_red(obj->left->left)) {
obj = rotate_right(obj);
flip_color(obj);
}
Expand All @@ -1082,16 +1085,15 @@ static qtreetbl_obj_t *move_red_right(qtreetbl_obj_t *obj) {
static qtreetbl_obj_t *fix(qtreetbl_obj_t *obj) {
// rotate right red to left
if (is_red(obj->right)) {
if (is_red(obj->right->left)) {
obj->right = rotate_right(obj->right);
}
obj = rotate_left(obj);
}
// rotate left red-red to right
if (obj->left && is_red(obj->left) && is_red(obj->left->left)) {
if (is_red(obj->left) && is_red(obj->left->left)) {
obj = rotate_right(obj);
}
// split 4-nodes
if (is_red(obj->left) && is_red(obj->right)) {
flip_color(obj);
}
return obj;
}

Expand Down Expand Up @@ -1226,18 +1228,19 @@ static qtreetbl_obj_t *remove_obj(qtreetbl_t *tbl, qtreetbl_obj_t *obj,

if (tbl->compare(name, namesize, obj->name, obj->namesize) < 0) { // left
// move red left
if (obj->left && (!is_red(obj->left) && !is_red(obj->left->left))) {
if (obj->left != NULL
&& (!is_red(obj->left) && !is_red(obj->left->left))) {
obj = move_red_left(obj);
}
// keep going down to the left
obj->left = remove_obj(tbl, obj->left, name, namesize);
} else { // right or equal
if (is_red(obj->left) && !is_red(obj->right)) {
if (is_red(obj->left)) {
obj = rotate_right(obj);
}
// remove if equal at the bottom
if (tbl->compare(name, namesize, obj->name, obj->namesize)
== 0&& obj->right == NULL) {
if (tbl->compare(name, namesize, obj->name, obj->namesize) == 0
&& obj->right == NULL) {
free(obj->name);
free(obj->data);
free(obj);
Expand Down

0 comments on commit 383f2ff

Please sign in to comment.