删除的情况,似乎考虑全了。但是为什么还会出错

This commit is contained in:
建峰 2024-06-28 11:40:21 +08:00
parent e1763db190
commit 6cd3356bdb

View File

@ -1218,12 +1218,13 @@ bool tree_rb_delete_fix(struct _tree* self, struct _tree_node* node)
} }
else else
{ {
// father is black, after delete node, total of black is -1
if(father->color = RBT_BLACK) if(father->color = RBT_BLACK)
{ {
// case 4 // case 4
// father is black, brother has no children
brother->color = RBT_RED; brother->color = RBT_RED;
node = father; node = father;
// After deleting the node, it became unbalanced
} }
else else
{ {
@ -1236,15 +1237,18 @@ bool tree_rb_delete_fix(struct _tree* self, struct _tree_node* node)
} }
else else
{ {
// symmetric
brother = father->left; brother = father->left;
if(brother != NULL && brother->color == RBT_RED) if(brother->color == RBT_RED)
{ {
// case1
brother->color = RBT_BLACK; brother->color = RBT_BLACK;
father->color = RBT_RED; father->color = RBT_RED;
tmp = tree_turn_right(self, father); tmp = tree_turn_right(self, father);
} }
else if(brother->left != NULL && brother->left->color == RBT_RED) else if(brother->left != NULL && brother->left->color == RBT_RED)
{ {
// case2
brother->color = father->color; brother->color = father->color;
father->color = RBT_BLACK; father->color = RBT_BLACK;
brother->left->color = RBT_BLACK; brother->left->color = RBT_BLACK;
@ -1253,19 +1257,23 @@ bool tree_rb_delete_fix(struct _tree* self, struct _tree_node* node)
} }
else if(brother->right != NULL && brother->right->color == RBT_RED) else if(brother->right != NULL && brother->right->color == RBT_RED)
{ {
// case3
brother->color = RBT_RED; brother->color = RBT_RED;
brother->right->color = RBT_BLACK; brother->right->color = RBT_BLACK;
tmp = tree_turn_left(self, brother); tmp = tree_turn_left(self, brother);
// convert to case2
} }
else else
{ {
if(father->color = RBT_BLACK) if(father->color = RBT_BLACK)
{ {
// case4
brother->color = RBT_RED; brother->color = RBT_RED;
node = father; node = father;
} }
else else
{ {
// case5
brother->color = RBT_RED; brother->color = RBT_RED;
father->color = RBT_BLACK; father->color = RBT_BLACK;
break; break;