04-树5 Root of AVL Tree

04-树5 Root of AVL Tree
在这里插入图片描述
在这里插入图片描述
思路分析:这道题思路很清晰,主要就是熟悉二叉平衡树的插入,知道LL,RR,LR,RL旋转的算法就可以解决

#include<iostream>
using namespace std;

typedef struct AVLTNode* AVLTree;
struct AVLTNode
{
    int height;
    AVLTree left, right;
    int data;
};
AVLTree insert(AVLTree T, int v);
int getHeight(AVLTree T);
AVLTree singleLeftRotation(AVLTree T);
AVLTree doubleLeftRightRotarion(AVLTree T);
AVLTree singleRightRotation(AVLTree T);
AVLTree doubleRightLeftRotation(AVLTree T);
int max(int a, int b);

int main()
{
    int num, i =0;
    int v;
    AVLTree T = NULL;
    cin >> num;
    for(i = 0; i<num; i++)
    {
        cin >> v;
        T = insert(T, v);
    }
    cout << T->data;
    return 0;
}

AVLTree insert(AVLTree T, int v)
{
    if(!T)
    {
        T = (AVLTree)malloc(sizeof(AVLTNode));
        T->data = v;
        T->right = T->left = NULL;
        T->height = 1;
    }else
    {
        if(v < T->data) 
        {
            T->left = insert(T->left, v);
            if(getHeight(T->left)-getHeight(T->right) == 2)
            {
                //结点插在左子树的左边-  左单旋
                if(v < T->left->data) T = singleLeftRotation(T);
                //结点插在左子树的右边-  左-右单旋
                else T = doubleLeftRightRotarion(T);
            }
        }else if(v > T->data)
        {
            T->right = insert(T->right, v);
            if(getHeight(T->left)-getHeight(T->right) == -2)
            {
                //结点插在右子树的右边-  右单旋
                if(v > T->right->data) T = singleRightRotation(T);
                //结点插在右子树的左边-  右-左单旋
                else T = doubleRightLeftRotation(T);
            }
        }
    }
        T->height = max(getHeight(T->left), getHeight(T->right))+1;//不要忘记加一
        return T;
}
int getHeight(AVLTree T)
{
    int maxH,lh,rh;
    if(T)
    {
        lh = getHeight(T->left);
        rh = getHeight(T->right);
        maxH = lh > rh ?lh:rh;
        return (maxH+1);
    }else return 0;

}
int max(int a, int b)
{
    return a>b ? a: b;
}
AVLTree singleLeftRotation(AVLTree T)
{
    AVLTree B;
    B = T->left;
    T->left = B->right;
    B->right = T;
    //更新高度
    T->height = max(getHeight(T->left), getHeight(T->right))+1;
    B->height = max(getHeight(B->left),T->height)+1;
    return B;
}
AVLTree doubleLeftRightRotarion(AVLTree T)
{
    T->left = singleRightRotation(T->left);
    return singleLeftRotation(T);
}
AVLTree singleRightRotation(AVLTree T)
{
    AVLTree B;
    B = T->right;
    T->right = B->left;
    B->left = T;
    //更新高度
    T->height = max(getHeight(T->right), getHeight(T->left))+1;
    B->height = max(getHeight(B->right),T->height)+1;
    return B;
}
AVLTree doubleRightLeftRotation(AVLTree T)
{
    T->right = singleLeftRotation(T->right);
    return singleRightRotation(T);
}
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值