1:平衡二叉树定义
typedef struct AVLNode{
int data;
int height;
struct AVLNode *lchild;
struct AVLNode *rchild;
}*AVLTree;
2:删除树
AVLTree Empty(AVLTree &T)//删除树
{
if (nullptr == T)
{
return nullptr;
}
Empty(T->lchild);
Empty(T->rchild);
delete T;
return nullptr;
}
3:计算高度
int Height(AVLTree T)//计算高度
{
if (nullptr == T)
{
return 0;
}
return T->height;
}
4:更新高度
void updateHeight(AVLTree &T)
{
if (nullptr == T)
{
return;
}
T->height = max(Height(T->lchild),Height(T->rchild))+1;
}
5:LL旋转
AVLTree LL_Rotation(AVLTree &T)//LL旋转
{
AVLTree temp = T->lchild;
T->lchild = temp->rchild;
temp->rchild = T;
updateHeight(T);//更新高度
updateHeight(temp);
return temp;
}
6:RR旋转
AVLTree RR_Rotation(AVLTree &T)//RR旋转
{
AVLTree temp = T->rchild;
T->rchild = temp->lchild;
temp->lchild = T;
updateHeight(T);//更新高度
updateHeight(temp);
return temp;
}
7:LR旋转
AVLTree LR_Rotation(AVLTree &T)//LR旋转
{
T->lchild = RR_Rotation(T->lchild);
return LL_Rotation(T);
}
8:RL旋转
AVLTree RL_Rotation(AVLTree &T)//RL旋转
{
T->rchild = LL_Rotation(T->rchild);
return RR_Rotation(T);
}
9:插入结点
AVLTree Insert(AVLTree &T,int x)
{
if(nullptr == T) //如果为空,创建新结点
{
T = new AVLNode;
T->lchild = T->rchild = NULL;
T->data = x;
T->height = 1;
return T;
}
if (T->data == x)//查找成功,什么也不做,查找失败时才插入
{
return T;
}
if(x < T->data)//插入到左子树
{
T->lchild = Insert(T->lchild,x);//注意插入后饭后结果挂接到T->lchild
if(Height(T->lchild)-Height(T->rchild) == 2)//插入后看是否平衡,如果不平衡显然是插入的那一边高度大
{ //沿着高度大的那条路径判断
//判断是LL还是LR,即插入的是lchild节点的lchild 还是rchild
T = (x < T->lchild->data) ? LL_Rotation(T) : LR_Rotation(T);
}
}
else//插入到右子树
{
T->rchild = Insert(T->rchild,x);
if(Height(T->rchild)-Height(T->lchild) == 2)
{
T = (x > T->rchild->data) ? RR_Rotation(T) : RL_Rotation(T);
}
}
updateHeight(T);
return T;
}
10:平衡调整
AVLTree adjust(AVLTree &T)//删除结点后,需要判断是否还是平衡,如果不平衡,就要调整
{
if (nullptr == T)
{
return nullptr;
}
if(Height(T->lchild)-Height(T->rchild) == 2)//沿着高度大的那条路径判断
{
T = (Height(T->lchild->lchild) >= Height(T->lchild->rchild)) ? LL_Rotation(T) : LR_Rotation(T);
}
if(Height(T->rchild)-Height(T->lchild)==2)//沿着高度大的那条路径判断
{
T = (Height(T->rchild->rchild) >= Height(T->rchild->lchild)) ? RR_Rotation(T) : RL_Rotation(T);
}
updateHeight(T);
return T;
}
11:删除结点
AVLTree Delete(AVLTree &T,int x)
{
if (nullptr == T)
{
return nullptr;
}
if(T->data == x)//如果找到删除节点
{
if(nullptr == T->rchild)//如果该节点的右孩子为NULL,那么直接删除
{
AVLTree temp = T;
T = T->lchild;
delete temp;
}
else//否则,将其右子树的最左孩子作为这个节点,并且递归删除这个节点的值
{
AVLTree temp;
temp = T->rchild;
while (nullptr != temp->lchild)
{
temp = temp->lchild;
}
T->data = temp->data;
T->rchild = Delete(T->rchild,T->data);
updateHeight(T);
}
return T;
}
if (T->data > x)//调节删除节点后可能涉及的节点
{
T->lchild = Delete(T->lchild, x);
}
else if(T->data < x)
{
T->rchild = Delete(T->rchild, x);
}
updateHeight(T);
if (nullptr != T->lchild)
{
T->lchild = adjust(T->lchild);
}
if (nullptr != T->rchild)
{
T->rchild = adjust(T->rchild);
}
if (nullptr != T)
{
T = adjust(T);
}
return T;
}
12:创建平衡二叉树
AVLTree CreateAVL(AVLTree &T)
{
int n,x;
cin >> n;
for(int i = 0;i < n; i++)
{
cin>>x;
T = Insert(T,x);
}
return T;
}