diff --git a/src/tree.c b/src/tree.c index f011c44..01ed33f 100644 --- a/src/tree.c +++ b/src/tree.c @@ -1480,7 +1480,7 @@ static struct _tree_node* tree_trun_left_then_right(struct _tree* self, struct _ { assert(self != NULL); assert(root != NULL); - struct _tree_node* node = root->left; + struct _tree_node* node = root->right; if(node != NULL) { tree_turn_left(self, node); @@ -1493,7 +1493,7 @@ static struct _tree_node* tree_trun_right_then_left(struct _tree* self, struct _ { assert(self != NULL); assert(root != NULL); - struct _tree_node* node = root->right; + struct _tree_node* node = root->left; if(node != NULL) { tree_turn_right(self, node); @@ -1524,7 +1524,7 @@ int32_t tree_height(struct _tree* self, struct _tree_node* root) * | 1 | 2 | 1 | 左旋 * | 2 | 2 | -1 | 先右旋后左旋 * | 3 | -2 | -1 | 右旋 - * | 4 | -2 | 1 | 先右旋后左旋 + * | 4 | -2 | 1 | 先左旋后右旋 * * @param self * @return true @@ -1541,7 +1541,7 @@ static bool tree_avl_rebalance(struct _tree* self, struct _tree_node* root) { return true; } - + self->print_obj(root->obj); tree_set_balance(self, root); int balance = root->balance; if(balance == 2) @@ -1552,7 +1552,7 @@ static bool tree_avl_rebalance(struct _tree* self, struct _tree_node* root) } else if(root->right->balance == -1) { - root = tree_trun_left_then_right(self, root); + root = tree_trun_right_then_left(self, root); } } else if(balance == -2) @@ -1563,7 +1563,7 @@ static bool tree_avl_rebalance(struct _tree* self, struct _tree_node* root) } else if(root->left->balance == 1) { - root = tree_trun_right_then_left(self, root); + root = tree_trun_left_then_right(self, root); } } diff --git a/test/test_tree.c b/test/test_tree.c index da3fcf4..1d855fd 100644 --- a/test/test_tree.c +++ b/test/test_tree.c @@ -297,6 +297,10 @@ void test_tree_num(void) for (i = 0; i < len; i++) { tree->insert(tree, &data[i]); + + printf("----- preorder -----\n"); + tree->preorder(tree, tree->_root); + printf("\n"); } printf("----- preorder -----\n"); tree->preorder(tree, tree->_root);