From a8d378570f64047cfe75131fe7668c91f9d5c31e Mon Sep 17 00:00:00 2001 From: wjf-hs Date: Mon, 24 Jun 2024 10:23:05 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E6=94=B9=E5=88=A4=E5=AE=9A=E6=96=B9?= =?UTF-8?q?=E5=BC=8F=EF=BC=8C=E7=94=A82=E5=92=8C1=E6=9D=A5=E5=88=A4?= =?UTF-8?q?=E5=AE=9A=E6=9B=B4=E5=8A=A0=E5=90=88=E7=90=86=EF=BC=8C=E4=BB=A3?= =?UTF-8?q?=E7=A0=81=E9=87=8F=E5=B0=91=E3=80=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/tree.c | 24 ++++++++++-------------- test/test_tree.c | 4 ++-- 2 files changed, 12 insertions(+), 16 deletions(-) diff --git a/src/tree.c b/src/tree.c index 01aa2a2..f011c44 100644 --- a/src/tree.c +++ b/src/tree.c @@ -1521,10 +1521,10 @@ int32_t tree_height(struct _tree* self, struct _tree_node* root) * * | 情况 | root->balance | node->balance | 调整方式 | * | ---- | ------------ | -------------- | -------- | - * | 1 | > 1 | > 0 | 左旋 - * | 2 | > 1 | < 0 | 先右旋后左旋 - * | 3 | < -1 | < 0 | 右旋 - * | 4 | < -1 | > 0 | 先右旋后左旋 + * | 1 | 2 | 1 | 左旋 + * | 2 | 2 | -1 | 先右旋后左旋 + * | 3 | -2 | -1 | 右旋 + * | 4 | -2 | 1 | 先右旋后左旋 * * @param self * @return true @@ -1544,28 +1544,24 @@ static bool tree_avl_rebalance(struct _tree* self, struct _tree_node* root) tree_set_balance(self, root); int balance = root->balance; - if(balance == 0) + if(balance == 2) { - // no need to rebalance - } - else if(balance > 1) - { - if(root->right->balance > 0) + if(root->right->balance == 1) { root = tree_turn_left(self, root); } - else if(root->right->balance < 0) + else if(root->right->balance == -1) { root = tree_trun_left_then_right(self, root); } } - else if(balance < 1) + else if(balance == -2) { - if(root->left->balance < 0) + if(root->left->balance == -1) { root = tree_turn_right(self, root); } - else if(root->left->balance > 0) + else if(root->left->balance == 1) { root = tree_trun_right_then_left(self, root); } diff --git a/test/test_tree.c b/test/test_tree.c index 6af1fbb..da3fcf4 100644 --- a/test/test_tree.c +++ b/test/test_tree.c @@ -281,8 +281,8 @@ void test_tree_num(void) uint32_t i = 0; // int data[] = { 2,1,3,4}; // int data[] = { 1,2,3,4,5,6}; - int data[] = { 5,2,3,1,7,8,6 }; - // int data[] = { 5,2,3,1,7,8,6,4,9,10,12,11,15,14,13 }; + // int data[] = { 5,2,3,1,7,8,6 }; + int data[] = { 5,2,3,1,7,8,6,4,9,10,12,11,15,14,13 }; int temp = 0; uint32_t len = sizeof(data) / sizeof(data[0]);