平衡二叉树的建立与调节(AVL树)

左-左变化: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;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值