mirror of
https://gitee.com/apaki/unicstl.git
synced 2025-07-03 15:56:52 +08:00
修改左旋和右旋不合理导致卡死的情况
This commit is contained in:
parent
3a2357e2a8
commit
6dc2271ab8
44
src/tree.c
44
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);
|
||||
}
|
||||
|
@ -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");
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user