0
点赞
收藏
分享

微信扫一扫

C++PTAA1066(AVL基本操作)

Greatiga 2022-01-28 阅读 24

An AVL tree is a self-balancing binary search tree. In an AVL tree, the heights of the two child subtrees of any node differ by at most one; if at any time they differ by more than one, rebalancing is done to restore this property. Figures 1-4 illustrate the rotation rules.

Now given a sequence of insertions, you are supposed to tell the root of the resulting AVL tree.

Input Specification:
Each input file contains one test case. For each case, the first line contains a positive integer N (≤20) which is the total number of keys to be inserted. Then N distinct integer keys are given in the next line. All the numbers in a line are separated by a space.

Output Specification:
For each test case, print the root of the resulting AVL tree in one line.

Sample Input 1:

5
88 70 61 96 120

Sample Output 1:

70

Sample Input 2:

7
88 70 61 96 120 90 65

Sample Output 2:

88

核心思路

这道题只要背诵了avl树的基本操作,就会做,主要涉及的难点是AVL树的人,L,RR,LL、RL,LR旋转

完整源码

#include<cstdio>
#include<algorithm>
#include<cmath>
struct node{
    int v,height;//v为结点权值,height为当前子树的高度
    node *lchild,*rchild;//左右孩子结点地址
}*root;

//生成一个新节点,v为结点权值
node* newNode(int v){
    node* Node = new node;//申请一个node型变量的地址空间
    Node->v = v;//结点赋值为v
    Node->height = 1;//结点高度初始为1
    Node->lchild = Node->rchild = NULL;//初始状态下没有左右孩子结点
    return Node; //返回新建结点的地址.
}

//获取以root为根节点的子树当前height
int getHeight(node * root){
    return (root==NULL)?0:root->height;
}
//更新root的height
void updataHeight(node* root){
    root->height = std::max(getHeight(root->lchild),getHeight(root->rchild))+1;
}
//计算结点root的平衡因子
int getBalanceFactor(node *root){
    //左子树高度减右子树高度
    return getHeight(root->lchild)- getHeight(root->rchild);
}
//左旋(Left Rotation)
void L(node *&root){
    node *tmp = root->rchild;
    root->rchild = tmp->lchild;
    tmp->lchild = root;
    updataHeight(root);
    updataHeight(tmp);
    root = tmp;
}
void R(node *&root){
    node *tmp = root->lchild;
    root->lchild = tmp->rchild;
    tmp->rchild = root;
    updataHeight(root);//更新结点B的高度
    updataHeight(tmp);//跟新结点A的高度
    root = tmp;
}
//插入权值为v的点
void insert(node *&root,int v){
    if(root == NULL) {
        root = newNode(v);
        return ;
    }
    if(v<root->v){
        insert(root->lchild,v);
        updataHeight(root);
        if(getBalanceFactor(root)==2){
            if(getBalanceFactor(root->lchild)==1){
                R(root);
            }else if(getBalanceFactor(root->lchild )==-1){
                L(root->lchild);
                R(root);
            }
        }
    }else{
        insert(root->rchild,v);
        updataHeight(root);
        if(getBalanceFactor(root)==-2){
            if(getBalanceFactor(root->rchild)==-1){
                L(root);
            }else if(getBalanceFactor(root->rchild )==1){
                R(root->rchild);
                L(root);
            }
        }
    }
}
//AVL树的建立
node* Create(int data[],int n){
    node *root = NULL;//新建空节点root
    for(int i = 0;i<n;i++){
        insert(root,data[i]);
    }
    return root; //返回根节点.

}
int main()
{
    int n,v;
    scanf("%d",&n);
    for(int i =0;i<n;i++){
        scanf("%d",&v);
        insert(root,v);
    }
    printf("%d\n",root->v);

    return 0;
}


举报

相关推荐

0 条评论