左-左变化:A左B右
AVLTree B = A->Left;
A->Left = B->Right;
B->Right = A;
//右-右 变化:A左B右
AVLTree LeftRotation ( AVLTree A )
{
/* 注意:A必须有一个左子结点B */
/* 将A与B做左单旋,更新A与B的高度,返回新的根结点B */
AVLTree B = A->Left;
A->Left = B->Right;
B->Right = A;
A->Height = max(GetHeight(A->Left), GetHeight(A->Right)) + 1;
B->Height = max(GetHeight(B->Left), GetHeight(B->Right)) + 1;
return B;
}
左-左变化:A右B左
AVLTree B = A->Right;
A->Right = B->Left;
B->Left = A;
//右-右 变化:A右B左
AVLTree RightRotation ( AVLTree A )
{
/* 注意:A必须有一个右子结点B */
/* 将A与B做右单旋,更新A与B的高度,返回新的根结点B */
AVLTree B = A->Right;
A->Right = B->Left;
B->Left = A;
A->Height = max(GetHeight(A->Left), GetHeight(A->Right)) + 1;
B->Height = max(GetHeight(B->Left), GetHeight(B->Right)) + 1;
return B;
}
左-右变化:B与C做右单旋 , A与C做左单旋
/* 将B与C做右单旋,C被返回 */
A->Left = RightRotation(A->Left);
/* 将A与C做左单旋,C被返回 */
return LeftRotation(A);
//左-右 变化:B与C做右单旋 A与C做左单旋
AVLTree LeftRightRotation ( AVLTree A )
{
/* 注意:A必须有一个左子结点B,且B必须有一个右子结点C */
/* 将A、B与C做两次单旋,返回新的根结点C */
/* 将B与C做右单旋,C被返回 */
A->Left = RightRotation(A->Left);
/* 将A与C做左单旋,C被返回 */
return LeftRotation(A);
}
右-左变化:B与C做左单旋 A与C做右单旋
/* 将B与C做左单旋,C被返回 */
A->Right = LeftRotation(A->Right);
/* 将A与C做右单旋,C被返回 */
return RightRotation(A);
//右-左 变化:B与C做左单旋 A与C做右单旋
AVLTree RightLeftRotation ( AVLTree A )
{
/* 注意:A必须有一个右子结点B,且B必须有一个左子结点C */
/* 将A、B与C做两次单旋,返回新的根结点C */
/* 将B与C做左单旋,C被返回 */
A->Right = LeftRotation(A->Right);
/* 将A与C做右单旋,C被返回 */
return RightRotation(A);
}
求树高的递归解法:
int GetHeight(AVLTree T)
{
int h1=0,h2=0;
if(T==NULL)
return 0;
if(T->Left)
h1 = GetHeight(T->Left);
if(T->Right)
h2 = GetHeight(T->Right);
return max(h1,h2)+1;
}
完整过程:
#include <string>
#include <vector>
#include <queue>
#include <stack>
#include <algorithm>
using namespace std;
typedef long long ll;
#define maxn 1000005
#define mod 7654321
#define NIL -1
typedef struct Node
{
int Data;
Node *Left;
Node *Right;
int Height;
}*AVLTree;
int GetHeight(AVLTree T)
{
int h1=0,h2=0;
if(T==NULL)
return 0;
if(T->Left)
h1 = GetHeight(T->Left);
if(T->Right)
h2 = GetHeight(T->Right);
return max(h1,h2)+1;
}
//左-左 变化:A左B右
AVLTree LeftRotation ( AVLTree A )
{
/* 注意:A必须有一个左子结点B */
/* 将A与B做左单旋,更新A与B的高度,返回新的根结点B */
AVLTree B = A->Left;
A->Left = B->Right;
B->Right = A;
A->Height = max(GetHeight(A->Left), GetHeight(A->Right)) + 1;
B->Height = max(GetHeight(B->Left), GetHeight(B->Right)) + 1;
return B;
}
//右-右 变化:A右B左
AVLTree RightRotation ( AVLTree A )
{
/* 注意:A必须有一个右子结点B */
/* 将A与B做右单旋,更新A与B的高度,返回新的根结点B */
AVLTree B = A->Right;
A->Right = B->Left;
B->Left = A;
A->Height = max(GetHeight(A->Left), GetHeight(A->Right)) + 1;
B->Height = max(GetHeight(B->Left), GetHeight(B->Right)) + 1;
return B;
}
//左-右 变化:B与C做右单旋 A与C做左单旋
AVLTree LeftRightRotation ( AVLTree A )
{
/* 注意:A必须有一个左子结点B,且B必须有一个右子结点C */
/* 将A、B与C做两次单旋,返回新的根结点C */
/* 将B与C做右单旋,C被返回 */
A->Left = RightRotation(A->Left);
/* 将A与C做左单旋,C被返回 */
return LeftRotation(A);
}
//右-左 变化:B与C做左单旋 A与C做右单旋
AVLTree RightLeftRotation ( AVLTree A )
{
/* 注意:A必须有一个右子结点B,且B必须有一个左子结点C */
/* 将A、B与C做两次单旋,返回新的根结点C */
/* 将B与C做左单旋,C被返回 */
A->Right = LeftRotation(A->Right);
/* 将A与C做右单旋,C被返回 */
return RightRotation(A);
}
AVLTree Insert( AVLTree T, int X )
{
if ( !T )
{ /* 若插入空树,则新建包含一个结点的树 */
T = (AVLTree)malloc(sizeof(Node));
T->Data = X;
T->Height = 0;
T->Left = T->Right = NULL;
}
else if (X < T->Data)
{
T->Left = Insert( T->Left, X);
if (GetHeight(T->Left)-GetHeight(T->Right) == 2)
{
if ( X < T->Left->Data )
T = LeftRotation(T); /* 左-左旋 */
else
T = LeftRightRotation(T); /* 左-右旋 */
}
}
else if (X > T->Data)
{
/* 插入T的右子树 */
T->Right = Insert( T->Right, X );
/* 如果需要右旋 */
if (GetHeight(T->Left)-GetHeight(T->Right) == -2)
if ( X > T->Right->Data )
T = RightRotation(T); /* 右-右旋 */
else
T = RightLeftRotation(T); /* 右-左旋 */
}
/* 更新树高 */
T->Height = max( GetHeight(T->Left), GetHeight(T->Right) ) + 1;
return T;
}
int main()
{
int N,X;
AVLTree T = NULL;
cin>>N;
for(int i=0;i<N;i++)
{
cin>>X;
T = Insert(T,X);
}
cout<<T->Data<<endl;
return 0;
}