pat 1066 Root of AVL Tree (AVL树)

地址:https://pintia.cn/problem-sets/994805342720868352/problems/994805404939173888

源自:PAT甲级题解-1066. Root of AVL Tree (25)-AVL树模板题
http://www.cnblogs.com/chenxiwenruo/p/6803291.html
AVL树旋转概念理解:http://blog.chinaunix.net/uid-25324849-id-2182877.html
原先一直不会构建AVL树,借助这道题学习一下如何构建AVL树

#include<bits/stdc++.h>
using namespace std;
typedef long long LL;
const int inf = 0x3f3f3f3f;
#define mp make_pair
#define pb push_back
#define fi first
#define se second
const int MAXN = 30;

int n;

typedef struct Node{
    int l,r;
    int val;
    int h;
}Node;

struct AVLTree{
    Node node[MAXN];
    int cnt = 0;
    int height(int u){
        if(u == -1)
            return 0;
        return node[u].h;
    }
    //k1是现在的根节点,对左儿子的左子树做了一次插入,右旋
    int RotateLL(int k1){
        int k2;
        k2 = node[k1].l;
        node[k1].l = node[k2].r;
        node[k2].r = k1;
        node[k1].h = max(height(node[k1].l),height(node[k1].r)) + 1;
        node[k2].h = max(height(node[k2].l),node[k1].h) + 1;
        return k2;  //新根节点
    }
    //对右儿子的右子树做了一次插入,左旋
    int RotateRR(int k1){
        int k2;
        k2 = node[k1].r;
        node[k1].r = node[k2].l;
        node[k2].l = k1;
        node[k1].h = max(height(node[k1].l),height(node[k1].r)) + 1;
        node[k2].h = max(height(node[k2].r),node[k1].h) + 1;
        return k2;
    }
    //对k1的左儿子L的右子树R进行插入
    int RotateLR(int k1){
        node[k1].l = RotateRR(node[k1].l);
        int root = RotateLL(k1);
        return root;
    }
    //对k1的右儿子R的左子树L进行插入
    int RotateRL(int k1){
        node[k1].r = RotateLL(node[k1].r);
        int root = RotateRR(k1);
        return root;
    }
    //插入操作
    int insert_val(int val,int root){
        if(root == -1){ //找到插入位置
            node[cnt].l = node[cnt].r = -1;
            node[cnt].val = val;
            node[cnt].h = 1;
            root = cnt;
            cnt++;
        }else if(val < node[root].val){
            //该结点插入到root结点的左子树中
            node[root].l = insert_val(val,node[root].l);
            int left = node[root].l;
            int right = node[root].r;
            if(height(left) - height(right) == 2){
                if(val < node[left].val){ //LL情况
                    root = RotateLL(root);
                }else{//LR情况
                    root = RotateLR(root);
                }
            }
        }else if(val > node[root].val){
            node[root].r = insert_val(val,node[root].r);
            int left = node[root].l;
            int right = node[root].r;
            if(height(left) - height(right) == -2){
                if(val > node[right].val){ // RR
                    root = RotateRR(root);
                }else{//RL
                    root = RotateRL(root);
                }
            }
        }else{

        }
        node[root].h = max(height(node[root].l),height(node[root].r)) + 1;
        return root;
    }
}avltree;

int main()
{
    int n;
    scanf("%d",&n);
    int root = -1;
    for(int i = 0;i < n;++i){
        int x;
        scanf("%d",&x);
        root = avltree.insert_val(x,root);
    }
    printf("%d\n",avltree.node[root].val);
    return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值