平衡二叉树的c语言实现
时间: 2023-10-17 09:22:49 浏览: 44
平衡二叉树(AVL树)是一种自平衡的二叉搜索树,保证了任意节点的左右子树高度差不超过1,从而实现了较快的查找、插入和删除操作。
以下是一个简单的平衡二叉树的C语言实现,包括节点结构体和插入、删除、查找等基本操作:
```c
#include <stdio.h>
#include <stdlib.h>
// 定义平衡二叉树节点结构体
typedef struct AVLNode {
int key; // 关键字
int height; // 节点高度
struct AVLNode *left; // 左子节点
struct AVLNode *right; // 右子节点
} AVLNode;
// 获取节点高度
int getHeight(AVLNode *node) {
if (node == NULL) {
return -1;
}
return node->height;
}
// 获取节点的平衡因子
int getBalanceFactor(AVLNode *node) {
return getHeight(node->left) - getHeight(node->right);
}
// 更新节点高度
void updateHeight(AVLNode *node) {
int leftHeight = getHeight(node->left);
int rightHeight = getHeight(node->right);
node->height = (leftHeight > rightHeight ? leftHeight : rightHeight) + 1;
}
// 左旋
AVLNode* leftRotation(AVLNode *node) {
AVLNode *rightNode = node->right;
node->right = rightNode->left;
rightNode->left = node;
updateHeight(node);
updateHeight(rightNode);
return rightNode;
}
// 右旋
AVLNode* rightRotation(AVLNode *node) {
AVLNode *leftNode = node->left;
node->left = leftNode->right;
leftNode->right = node;
updateHeight(node);
updateHeight(leftNode);
return leftNode;
}
// 插入节点
AVLNode* insertNode(AVLNode *root, int key) {
if (root == NULL) {
AVLNode *newNode = (AVLNode*)malloc(sizeof(AVLNode));
newNode->key = key;
newNode->height = 0;
newNode->left = NULL;
newNode->right = NULL;
return newNode;
}
if (key < root->key) {
root->left = insertNode(root->left, key);
updateHeight(root);
if (getBalanceFactor(root) == 2) {
if (getBalanceFactor(root->left) == 1) {
root = rightRotation(root);
} else {
root->left = leftRotation(root->left);
root = rightRotation(root);
}
}
} else if (key > root->key) {
root->right = insertNode(root->right, key);
updateHeight(root);
if (getBalanceFactor(root) == -2) {
if (getBalanceFactor(root->right) == -1) {
root = leftRotation(root);
} else {
root->right = rightRotation(root->right);
root = leftRotation(root);
}
}
}
return root;
}
// 查找节点
AVLNode* searchNode(AVLNode *root, int key) {
if (root == NULL || root->key == key) {
return root;
}
if (key < root->key) {
return searchNode(root->left, key);
} else {
return searchNode(root->right, key);
}
}
// 找到最小节点
AVLNode* findMinNode(AVLNode *root) {
if (root == NULL) {
return NULL;
} else if (root->left == NULL) {
return root;
} else {
return findMinNode(root->left);
}
}
// 删除节点
AVLNode* deleteNode(AVLNode *root, int key) {
if (root == NULL) {
return NULL;
}
if (key < root->key) {
root->left = deleteNode(root->left, key);
updateHeight(root);
if (getBalanceFactor(root) == -2) {
AVLNode *rightNode = root->right;
if (getBalanceFactor(rightNode) == -1) {
root = leftRotation(root);
} else {
root->right = rightRotation(rightNode);
root = leftRotation(root);
}
}
} else if (key > root->key) {
root->right = deleteNode(root->right, key);
updateHeight(root);
if (getBalanceFactor(root) == 2) {
AVLNode *leftNode = root->left;
if (getBalanceFactor(leftNode) == 1) {
root = rightRotation(root);
} else {
root->left = leftRotation(leftNode);
root = rightRotation(root);
}
}
} else {
if (root->left == NULL && root->right == NULL) {
free(root);
root = NULL;
} else if (root->left == NULL) {
AVLNode *temp = root;
root = root->right;
free(temp);
} else if (root->right == NULL) {
AVLNode *temp = root;
root = root->left;
free(temp);
} else {
AVLNode *minNode = findMinNode(root->right);
root->key = minNode->key;
root->right = deleteNode(root->right, minNode->key);
updateHeight(root);
if (getBalanceFactor(root) == 2) {
AVLNode *leftNode = root->left;
if (getBalanceFactor(leftNode) == 1) {
root = rightRotation(root);
} else {
root->left = leftRotation(leftNode);
root = rightRotation(root);
}
}
}
}
return root;
}
// 中序遍历
void inorderTraversal(AVLNode *root) {
if (root != NULL) {
inorderTraversal(root->left);
printf("%d ", root->key);
inorderTraversal(root->right);
}
}
int main() {
AVLNode *root = NULL;
root = insertNode(root, 10);
root = insertNode(root, 20);
root = insertNode(root, 30);
root = insertNode(root, 40);
root = insertNode(root, 50);
root = insertNode(root, 25);
printf("Inorder traversal of the constructed AVL tree is: ");
inorderTraversal(root);
printf("\n");
root = deleteNode(root, 30);
printf("Inorder traversal after deletion of 30: ");
inorderTraversal(root);
printf("\n");
return 0;
}
```
在上述代码中,我们使用了递归的方式实现了插入、查找和删除节点的操作。其中,插入和删除操作在每次操作后都会对节点进行高度更新,并检查是否需要进行旋转来保持平衡。在代码末尾,我们使用中序遍历来验证树的正确性。