• PAT A1066 Root of AVL Tree [平衡二叉树]


    题目描述

    链接
    建立一棵AVL树,输出根结点

    分析

    • 注意下面标注的关键点!!!!
    • 函数: getHeight,getBF,updateHeight,L,R,insert,create
    • L: 左旋,说明右边太重了,所以要成为根的是右子树的根,temp就是取右子树的根,然后提上来时,temp的左子树就要挂到原来root的右子树,然后temp的左子树要变成root。然后更新两个结点高度,然后root=temp
    • R:右旋,说明左边太重了,所以要成为根的是左子树的根,temp就是取左子树的根,然后提上来时,temp的右子树就要挂到原来root的左子树,然后temp的右子树要变成root。然后更新两个结点高度,然后root=temp
    • getBF:记住是max(getHeight, getHeight)+1,不要直接root->lchild->h这样
    • insert:找不到,则说明存在插入:新建结点,注意更新h,w,和lchild,rchild;
      • 然后x小于w,则往左插,左插会导致LL,LR,即判断BF(root)==2,左插对应左子树,故判断root->lchild,BF=1,则是LL,说明左边重,要右旋R(root);BF=-1,则是LR,必须先左旋左子树L(root->lchild),再右旋R(root)
      • x 大于等于w,则往右插,右插会导致RR,RL,即判断BF(root)==-2,右插对应右子树,故判断root->rchild,BF=-1,则是RR,说明右边重,要左旋L(root);BF=1,则是RL,必须先右旋右子树R(root->rchild),再右旋L(root)
    #include<bits/stdc++.h>
    using namespace std;
    
    const int maxn = 25;
    int n, a[maxn];
    
    struct node{
        int w,h;
        node *lchild, *rchild;
    }nodes[maxn];
    
    int getHeight(node *root){
        if(root == NULL) return 0;
        return root->h;
    }
    
    void updateHeight(node *root){
        if(root == NULL) return;
        //不是直接写root->lchild->h
        root->h = max(getHeight(root->lchild), getHeight(root->rchild)) + 1; //注意!!!
    }
    
    int getBF(node *root){
        return getHeight(root->lchild) - getHeight(root->rchild);
    }
    
    void L(node *&root){//一定不要忘记加引用
        node *temp = root->rchild;
        root->rchild = temp->lchild;
        temp->lchild = root;
        updateHeight(root);
        updateHeight(temp);
        root = temp;
    }
    
    void R(node *&root){//一定不要忘记加引用
        node *temp = root->lchild;
        root->lchild = temp->rchild;
        temp->rchild = root;
        updateHeight(root);
        updateHeight(temp);
        root = temp;
    }
    
    void insert(node *&root, int x){ //一定不要忘记加引用
        if(!root){
            root = new node;
            root->w = x;
            root->h = 1;  //不要忘记高度
            root->lchild = root->rchild = NULL; //不要忘记NULL
            return;
        }
        if(x < root->w){
            insert(root->lchild, x);
            updateHeight(root);
            if(getBF(root) == 2){
                if(getBF(root->lchild) == 1) R(root);
                else if(getBF(root->lchild) == -1){
                    L(root->lchild);
                    R(root);
                }
            }
        }else{
            insert(root->rchild, x);
            updateHeight(root);
            if(getBF(root) == -2){
                if(getBF(root->rchild) == -1) L(root);
                else if(getBF(root->rchild) == 1){
                    R(root->rchild);
                    L(root);
                }
            }
        }
    
    }
    
    node *create(){
        node *root = NULL;
        for(int i=0;i<n;i++){
            insert(root, a[i]);
        }
        return root;
    }
    
    int main(){
        cin>>n;
        for(int i=0;i<n;i++){
            cin>>a[i];
        }
        node *root = create();
        cout<<root->w<<endl;
    }
    
    
  • 相关阅读:
    Oracle Data Integrator 12c-----场景(Scenario)和调度(Schedule)
    Oracle 12c 用户创建、角色、权限
    CDB和PDB的创建、连接、启动、关闭
    Oracle Data Integrator 12c----包(Package)
    Oracle Data Integrator 12c-第一个映射
    Oracle Data Integrator 12c-模型(Model)和 数据存储(DataStore)
    解锁scott账户方法
    ORA-12514 TNS 监听程序当前无法识别连接描述符中请求服务
    VirtualBox内Linux系统怎样与Windows共享文件夹
    Changing the Language Used in ODI Studio
  • 原文地址:https://www.cnblogs.com/doragd/p/11272831.html
Copyright © 2020-2023  润新知