From 6cd3356bdb9344d3fd2fff3b5b93bbdabecbbf4c Mon Sep 17 00:00:00 2001 From: wjf-hs Date: Fri, 28 Jun 2024 11:40:21 +0800 Subject: [PATCH] =?UTF-8?q?=E5=88=A0=E9=99=A4=E7=9A=84=E6=83=85=E5=86=B5?= =?UTF-8?q?=EF=BC=8C=E4=BC=BC=E4=B9=8E=E8=80=83=E8=99=91=E5=85=A8=E4=BA=86?= =?UTF-8?q?=E3=80=82=E4=BD=86=E6=98=AF=E4=B8=BA=E4=BB=80=E4=B9=88=E8=BF=98?= =?UTF-8?q?=E4=BC=9A=E5=87=BA=E9=94=99?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/tree.c | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/src/tree.c b/src/tree.c index 069aca4..ce7c443 100644 --- a/src/tree.c +++ b/src/tree.c @@ -1218,12 +1218,13 @@ bool tree_rb_delete_fix(struct _tree* self, struct _tree_node* node) } else { - // father is black, after delete node, total of black is -1 if(father->color = RBT_BLACK) { // case 4 + // father is black, brother has no children brother->color = RBT_RED; node = father; + // After deleting the node, it became unbalanced } else { @@ -1236,15 +1237,18 @@ bool tree_rb_delete_fix(struct _tree* self, struct _tree_node* node) } else { + // symmetric brother = father->left; - if(brother != NULL && brother->color == RBT_RED) + if(brother->color == RBT_RED) { + // case1 brother->color = RBT_BLACK; father->color = RBT_RED; tmp = tree_turn_right(self, father); } else if(brother->left != NULL && brother->left->color == RBT_RED) { + // case2 brother->color = father->color; father->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) { + // case3 brother->color = RBT_RED; brother->right->color = RBT_BLACK; tmp = tree_turn_left(self, brother); + // convert to case2 } else { if(father->color = RBT_BLACK) { + // case4 brother->color = RBT_RED; node = father; } else { + // case5 brother->color = RBT_RED; father->color = RBT_BLACK; break;