mirror of
https://gitee.com/apaki/unicstl.git
synced 2025-07-03 15:56:52 +08:00
修改判定方式,用2和1来判定更加合理,代码量少。
This commit is contained in:
parent
202832f29c
commit
a8d378570f
24
src/tree.c
24
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);
|
||||
}
|
||||
|
@ -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]);
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user