From 6dc2271ab811938599fc58eca4948c6f3fe7dad1 Mon Sep 17 00:00:00 2001 From: wjf-hs Date: Mon, 24 Jun 2024 16:59:42 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E6=94=B9=E5=B7=A6=E6=97=8B=E5=92=8C?= =?UTF-8?q?=E5=8F=B3=E6=97=8B=E4=B8=8D=E5=90=88=E7=90=86=E5=AF=BC=E8=87=B4?= =?UTF-8?q?=E5=8D=A1=E6=AD=BB=E7=9A=84=E6=83=85=E5=86=B5?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/tree.c | 44 ++++++++++++++++++++++++++++++-------------- test/test_tree.c | 5 ++--- 2 files changed, 32 insertions(+), 17 deletions(-) diff --git a/src/tree.c b/src/tree.c index 768319c..c665392 100644 --- a/src/tree.c +++ b/src/tree.c @@ -1422,11 +1422,15 @@ static struct _tree_node* tree_turn_left(struct _tree* self, struct _tree_node* return root; } - if(root->parent != NULL) + if(root->parent == NULL) { - if(root->parent->left == root) // step1 + self->_root = node; // step1 + } + else + { + if(root->parent->left == root) { - root->parent->left = node; + root->parent->left = node; // step1 } else if(root->parent->right == root) { @@ -1437,7 +1441,11 @@ static struct _tree_node* tree_turn_left(struct _tree* self, struct _tree_node* root->parent = node; // step3 root->right = node->left; // step4 - node->left = root; // step5 + if(node->left != NULL) + { + node->left->parent = root; // step5 + } + node->left = root; // step6 tree_set_balance(self, root); tree_set_balance(self, node); @@ -1454,7 +1462,11 @@ static struct _tree_node* tree_turn_right(struct _tree* self, struct _tree_node* return root; } - if(root->parent != NULL) + if(root->parent == NULL) + { + self->_root = node; // step1 + } + else { if(root->parent->left == root) { @@ -1469,7 +1481,11 @@ static struct _tree_node* tree_turn_right(struct _tree* self, struct _tree_node* root->parent = node; // step3 root->left = node->right; // step4 - node->right = root; // step5 + if(node->right != NULL) + { + node->right->parent = root; // step5 + } + node->right = root; // step6 tree_set_balance(self, root); tree_set_balance(self, node); @@ -1521,10 +1537,10 @@ int32_t tree_height(struct _tree* self, struct _tree_node* root) * * | 情况 | root->balance | node->balance | 调整方式 | * | ---- | ------------ | -------------- | -------- | - * | 1 | 2 | 1 | 左旋 - * | 2 | 2 | -1 | 先右旋后左旋 - * | 3 | -2 | -1 | 右旋 - * | 4 | -2 | 1 | 先左旋后右旋 + * | 1 | 2 | >= 0 | 左旋 + * | 2 | 2 | < 0 | 先右旋后左旋 + * | 3 | -2 | <= 0 | 右旋 + * | 4 | -2 | > 0 | 先左旋后右旋 * * @param self * @return true @@ -1547,22 +1563,22 @@ static bool tree_avl_rebalance(struct _tree* self, struct _tree_node* root) int balance = root->balance; if(balance == 2) { - if(root->right->balance == 1) + if(root->right->balance >= 0) { root = tree_turn_left(self, root); } - else if(root->right->balance == -1) + else { root = tree_trun_right_then_left(self, root); } } else if(balance == -2) { - if(root->left->balance == -1) + if(root->left->balance <= 0) { root = tree_turn_right(self, root); } - else if(root->left->balance == 1) + else { root = tree_trun_left_then_right(self, root); } diff --git a/test/test_tree.c b/test/test_tree.c index 1fb081b..c63ea3e 100644 --- a/test/test_tree.c +++ b/test/test_tree.c @@ -338,7 +338,6 @@ void test_tree_num(void) printf("\n"); - printf("----- left priority -----\n"); tree->order(tree, false); printf("----- breadth -----\n"); @@ -351,13 +350,13 @@ void test_tree_num(void) printf("delete = "); tree->print_obj(&temp); - printf("size = %2d\n", tree->size(tree)); + printf("size = %2d : ", tree->size(tree)); tree->delete(tree, &temp); // printf("----- breadth -----\n"); // tree->breadth(tree, tree->_root); - printf("----- preorder -----\n"); + // printf("----- preorder -----\n"); tree->preorder(tree, tree->_root); printf("\n"); }