#include <stdlib.h>
// AVL树节点结构
typedef struct AVLNode {
void* data;
struct AVLNode* left;
struct AVLNode* right;
int height;
} AVLNode;
// AVL树结构(含比较函数)
typedef struct {
AVLNode* root;
int (*compare)(const void*, const void*);
} AVLTree;
// 辅助函数:获取节点高度
int height(AVLNode* node) {
return node ? node->height : 0;
}
// 辅助函数:计算平衡因子
int balance_factor(AVLNode* node) {
return height(node->left) - height(node->right);
}
// 辅助函数:最大值计算
int max(int a, int b) {
return (a > b) ? a : b;
}
// 右旋操作
AVLNode* right_rotate(AVLNode* y) {
AVLNode* x = y->left;
AVLNode* T2 = x->right;
x->right = y;
y->left = T2;
y->height = max(height(y->left), height(y->right)) + 1;
x->height = max(height(x->left), height(x->right)) + 1;
return x;
}
// 左旋操作
AVLNode* left_rotate(AVLNode* x) {
AVLNode* y = x->right;
AVLNode* T2 = y->left;
y->left = x;
x->right = T2;
x->height = max(height(x->left), height(x->right)) + 1;
y->height = max(height(y->left), height(y->right)) + 1;
return y;
}
// 插入节点(内部递归实现)
AVLNode* insert_node(AVLNode* node, void* data, int (*compare)(const void*, const void*)) {
if (!node) {
AVLNode* newNode = (AVLNode*)malloc(sizeof(AVLNode));
newNode->data = data;
newNode->left = newNode->right = NULL;
newNode->height = 1;
return newNode;
}
int cmp = compare(data, node->data);
if (cmp < 0)
node->left = insert_node(node->left, data, compare);
else if (cmp > 0)
node->right = insert_node(node->right, data, compare);
else
return node;
node->height = 1 + max(height(node->left), height(node->right));
int balance = balance_factor(node);
// 左左情况
if (balance > 1 && compare(data, node->left->data) < 0)
return right_rotate(node);
// 右右情况
if (balance < -1 && compare(data, node->right->data) > 0)
return left_rotate(node);
// 左右情况
if (balance > 1 && compare(data, node->left->data) > 0) {
node->left = left_rotate(node->left);
return right_rotate(node);
}
// 右左情况
if (balance < -1 && compare(data, node->right->data) < 0) {
node->right = right_rotate(node->right);
return left_rotate(node);
}
return node;
}
// 公共插入接口
void avl_insert(AVLTree* tree, void* data) {
tree->root = insert_node(tree->root, data, tree->compare);
}
// 查找最小节点
AVLNode* min_value_node(AVLNode* node) {
AVLNode* current = node;
while (current->left)
current = current->left;
return current;
}
// 删除节点(内部递归实现)
AVLNode* delete_node(AVLNode* root, void* data, int (*compare)(const void*, const void*)) {
if (!root) return root;
int cmp = compare(data, root->data);
if (cmp < 0)
root->left = delete_node(root->left, data, compare);
else if (cmp > 0)
root->right = delete_node(root->right, data, compare);
else {
if (!root->left || !root->right) {
AVLNode* temp = root->left ? root->left : root->right;
if (!temp) {
temp = root;
root = NULL;
} else {
*root = *temp;
}
free(temp);
} else {
AVLNode* temp = min_value_node(root->right);
root->data = temp->data;
root->right = delete_node(root->right, temp->data, compare);
}
}
if (!root) return root;
root->height = 1 + max(height(root->left), height(root->right));
int balance = balance_factor(root);
// 左左
if (balance > 1 && balance_factor(root->left) >= 0)
return right_rotate(root);
// 左右
if (balance > 1 && balance_factor(root->left) < 0) {
root->left = left_rotate(root->left);
return right_rotate(root);
}
// 右右
if (balance < -1 && balance_factor(root->right) <= 0)
return left_rotate(root);
// 右左
if (balance < -1 && balance_factor(root->right) > 0) {
root->right = right_rotate(root->right);
return left_rotate(root);
}
return root;
}
// 公共删除接口
void avl_delete(AVLTree* tree, void* data) {
tree->root = delete_node(tree->root, data, tree->compare);
}
// 遍历辅助函数
void preorder(AVLNode* node, void (*callback)(void*)) {
if (node) {
callback(node->data);
preorder(node->left, callback);
preorder(node->right, callback);
}
}
void inorder(AVLNode* node, void (*callback)(void*)) {
if (node) {
inorder(node->left, callback);
callback(node->data);
inorder(node->right, callback);
}
}
void postorder(AVLNode* node, void (*callback)(void*)) {
if (node) {
postorder(node->left, callback);
postorder(node->right, callback);
callback(node->data);
}
}
#include <stdio.h>
/* 示例使用 */
int int_compare(const void* a, const void* b) {
return *(int*)a - *(int*)b;
}
void print_int(void* data) {
printf("%d ", *(int*)data);
}
int main() {
AVLTree tree = {NULL, int_compare};
// 插入测试
int nums[] = {9, 5, 10, 0, 6, 11, -1, 1, 2};
for (int i = 0; i < sizeof(nums)/sizeof(nums[0]); i++) {
int* data = malloc(sizeof(int));
*data = nums[i];
avl_insert(&tree, data);
}
printf("Preorder traversal:\n");
preorder(tree.root, print_int);
printf("\n");
// 删除测试
int to_delete = 10;
avl_delete(&tree, &to_delete);
printf("\nAfter deletion of 10:\n");
printf("Inorder traversal:\n");
inorder(tree.root, print_int);
printf("\n");
return 0;
}