1. 概述
AVL树是最早提出的自平衡二叉树,在AVL树中任何节点的两个子树的高度最大差别为一,所以它也被称为高度平衡树。AVL树得名于它的发明者G.M. Adelson-Velsky和E.M. Landis。AVL树种查找、插入和删除在平均和最坏情况下都是O(log n),增加和删除可能需要通过一次或多次树旋转来重新平衡这个树。本文介绍了AVL树的设计思想和基本操作。
2. 基本术语
有四种种情况可能导致二叉查找树不平衡,分别为:
(1)LL:插入一个新节点到根节点的左子树(Left)的左子树(Left),导致根节点的平衡因子由1变为2
(2)RR:插入一个新节点到根节点的右子树(Right)的右子树(Right),导致根节点的平衡因子由-1变为-2
(3)LR:插入一个新节点到根节点的左子树(Left)的右子树(Right),导致根节点的平衡因子由1变为2
(4)RL:插入一个新节点到根节点的右子树(Right)的左子树(Left),导致根节点的平衡因子由-1变为-2
针对四种种情况可能导致的不平衡,可以通过旋转使之变平衡。有两种基本的旋转:
(1)左旋转:将根节点旋转到(根节点的)右孩子的左孩子位置
(2)右旋转:将根节点旋转到(根节点的)左孩子的右孩子位置
3. AVL树的旋转操作
AVL树的基本操作是旋转,有四种旋转方式,分别为:左旋转,右旋转,左右旋转(先左后右),右左旋转(先右后左),实际上,这四种旋转操作两两对称,因而也可以说成两类旋转操作。
基本的数据结构:
1 2 3 4 5 6 7 8 9 10 11 12 13 | typedef struct Node* Tree; typedef struct Node* Node_t; typedef Type int ; struct Node{ Node_t left; Node_t right; int height; Type data; }; int Height(Node_t node) { return node->height; } |
3.1 LL
LL情况需要右旋解决,如下图所示:
代码为:
1 2 3 4 5 6 7 8 | Node_t RightRotate(Node_t a) { b = a->left; a->left = b->right; b->right = a; a->height = Max(Height(a->left), Height(a->right)); b->height = Max(Height(b->left), Height(b->right)); return b; } |
3.2 RR
RR情况需要左旋解决,如下图所示:
代码为:
1 2 3 4 5 6 7 8 | Node_t LeftRotate(Node_t a) { b = a->right; a->right = b->left; b->left = a; a->height = Max(Height(a->left), Height(a->right)); b->height = Max(Height(b->left), Height(b->right)); return b; } |
3.3 LR
LR情况需要左右(先B左旋转,后A右旋转)旋解决,如下图所示:
代码为:
1 2 3 4 | Node_t LeftRightRotate(Node_t a) { a->left = LeftRotate(a->left); return RightRotate(a); } |
3.4 RL
RL情况需要右左旋解决(先B右旋转,后A左旋转),如下图所示:
代码为:
1 2 3 4 | Node_t RightLeftRotate(Node_t a) { a->right = RightRotate(a->right); return LeftRotate(a); } |
#include<stdio.h>
#include <stdlib.h>
typedef struct _Tree
{
int nValue;
struct _Tree* pLeft;
struct _Tree* pRight;
struct _Tree* pFather;
}Tree;
Tree* root = NULL;
void CreateTree()
{
root = (Tree*)malloc(sizeof(Tree));
root->nValue = 1;
root->pFather = NULL;
root->pLeft = (Tree*)malloc(sizeof(Tree));
root->pLeft->nValue = 2;
root->pLeft->pFather = root;
root->pLeft->pLeft = (Tree*)malloc(sizeof(Tree));
root->pLeft->pLeft->nValue = 4;
root->pLeft->pLeft->pLeft = NULL;
root->pLeft->pLeft->pRight = NULL;
root->pLeft->pLeft->pFather = root->pLeft;
root->pLeft->pRight = (Tree*)malloc(sizeof(Tree));
root->pLeft->pRight->nValue = 5;
root->pLeft->pRight->pLeft = NULL;
root->pLeft->pRight->pRight = NULL;
root->pLeft->pRight->pFather = root->pLeft;
root->pRight = (Tree*)malloc(sizeof(Tree));
root->pRight->nValue = 3;
root->pRight->pLeft = NULL;
root->pRight->pRight = NULL;
root->pRight->pFather = root;
}
void Rotate_Right(Tree* tree)
{
Tree* temp = tree->pLeft;
// 修改三个子节点
tree->pLeft = temp->pRight;
temp->pRight = tree;
if (tree->pFather == NULL)
{
root = temp;
}
else
{
// 看 tree 原来 放在他父亲节点的 左边还是右边
if (tree->pFather->pLeft == tree)
{
tree->pFather->pLeft = temp;
}
else
{
tree->pFather->pRight = temp;
}
}
// 修改 三个父亲
temp->pFather = tree->pFather;
tree->pFather = temp;
// 看 tree 有没有左
if(tree->pLeft != NULL)
{
tree->pLeft->pFather = tree;
}
}
void Rotate_Left(Tree* tree)
{
Tree* temp = tree->pRight;
// 修改三个子节点
tree->pRight = temp->pLeft;
temp->pLeft = tree;
if (tree->pFather == NULL)
{
root = temp;
}
else
{
// 看 tree 原来 放在他父亲节点的 左边还是右边
if (tree->pFather->pLeft == tree)
{
tree->pFather->pLeft = temp;
}
else
{
tree->pFather->pRight = temp;
}
}
// 修改 三个父亲
temp->pFather = tree->pFather;
tree->pFather = temp;
// 看 tree 有没有左
if(tree->pRight != NULL)
{
tree->pRight->pFather = tree;
}
}
void Qian(Tree* tree)
{
if (tree)
{
printf(“%d “,tree->nValue);
Qian(tree->pLeft);
Qian(tree->pRight);
}
}
int main()
{
CreateTree();
Qian(root);
printf(“\n———————–\n”);
Rotate_Right(root);
Qian(root);
printf(“\n———————–\n”);
Rotate_Left(root);
Qian(root);
printf(“\n———————–\n”);
system(“pause”);
return 0;
}