平衡二叉树详解
1. 平衡二叉树的定义
平衡二叉树(Balanced Binary Tree),也称为AVL树,是一种高度平衡的二叉查找树。它的特点是对于每个节点,左子树和右子树的高度差不超过1。具体来说,对于任意节点 (N) :
2. 平衡因子
平衡因子(Balance Factor)是用于衡量节点的左右子树高度差的指标。对于节点 (N) ,其平衡因子定义为:
3. 操作
平衡二叉树的操作包括插入、删除和旋转。以下是这些操作的详细说明和C语言实现。
3.1 插入操作
插入节点后,可能会导致树失去平衡,需要通过旋转操作来恢复平衡。
代码实现:
#include <stdio.h>
#include <stdlib.h>
// 定义节点结构
typedef struct AVLNode {
int key;
struct AVLNode *left;
struct AVLNode *right;
int height;
} AVLNode;
// 获取节点高度
int height(AVLNode *N) {
if (N == NULL)
return 0;
return N->height;
}
// 创建新节点
AVLNode* createNode(int key) {
AVLNode* node = (AVLNode*)malloc(sizeof(AVLNode));
node->key = key;
node->left = NULL;
node->right = NULL;
node->height = 1; // 新节点高度为1
return(node);
}
// 右旋操作
AVLNode *rightRotate(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 *leftRotate(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;
}
// 获取节点平衡因子
int getBalance(AVLNode *N) {
if (N == NULL)
return 0;
return height(N->left) - height(N->right);
}
// 插入节点
AVLNode* insertNode(AVLNode* node, int key) {
if (node == NULL)
return(createNode(key));
if (key < node->key)
node->left = insertNode(node->left, key);
else if (key > node->key)
node->right = insertNode(node->right, key);
else // 相同的键不允许插入
return node;
node->height = 1 + max(height(node->left), height(node->right));
int balance = getBalance(node);
if (balance > 1 && key < node->left->key)
return rightRotate(node);
if (balance < -1 && key > node->right->key)
return leftRotate(node);
if (balance > 1 && key > node->left->key) {
node->left = leftRotate(node->left);
return rightRotate(node);
}
if (balance < -1 && key < node->right->key) {
node->right = rightRotate(node->right);
return leftRotate(node);
}
return node;
}
3.2 删除操作
删除节点后也可能导致树失去平衡,需要通过旋转操作来恢复平衡。
代码实现:
// 查找最小值节点
AVLNode * minValueNode(AVLNode* node) {
AVLNode* current = node;
while (current->left != NULL)
current = current->left;
return current;
}
// 删除节点
AVLNode* deleteNode(AVLNode* root, int key) {
if (root == NULL)
return root;
if (key < root->key)
root->left = deleteNode(root->left, key);
else if (key > root->key)
root->right = deleteNode(root->right, key);
else {
if ((root->left == NULL) || (root->right == NULL)) {
AVLNode *temp = root->left ? root->left : root->right;
if (temp == NULL) {
temp = root;
root = NULL;
} else
*root = *temp;
free(temp);
} else {
AVLNode* temp = minValueNode(root->right);
root->key = temp->key;
root->right = deleteNode(root->right, temp->key);
}
}
if (root == NULL)
return root;
root->height = 1 + max(height(root->left), height(root->right));
int balance = getBalance(root);
if (balance > 1 && getBalance(root->left) >= 0)
return rightRotate(root);
if (balance > 1 && getBalance(root->left) < 0) {
root->left = leftRotate(root->left);
return rightRotate(root);
}
if (balance < -1 && getBalance(root->right) <= 0)
return leftRotate(root);
if (balance < -1 && getBalance(root->right) > 0) {
root->right = rightRotate(root->right);
return leftRotate(root);
}
return root;
}
4. 旋转操作详解
旋转操作是为了维护AVL树的平衡性。常见的旋转操作包括右旋、左旋、左右旋和右左旋。
4.1 右旋(Right Rotation)
右旋是对某个节点进行的顺时针旋转。用于平衡因子大于1,且新插入的节点在左子树的左子树。