【PAT】1066. Root of AVL Tree (25)【平衡二叉树的实现】

题目描述

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.
PAT1066题目图片
Now given a sequence of insertions, you are supposed to tell the root of the resulting AVL tree.

翻译:一棵AVL树为一棵平衡二叉搜索树。在一棵AVL树中,两个子树直接的高度差最多为1。任何时候他们相差超过1,就会自动调整以恢复其特性。图1-4说明了旋转规则。
现在给你一组插入数列,你需要说出结果AVL树的根节点。

INPUT FORMAT

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.

翻译:每个输入文件包含一组测试数据。对于每组输入数据,第一行包括一个正整数N(<=20),代表插入的键值总数。接着下一行给出N个不重复的整数键值。所有数字之间用空格隔开。

OUTPUT FORMAT

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

翻译:对于每组输入数据,输出一行AVL树的根节点。


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


解题思路

模拟平衡二叉树的插入,返回根节点的数字。平衡二叉树实现可看我的【数据结构】快速实现平衡二叉树(可观察每一次插入后的结果)

#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<string>
#include<algorithm>
#define INF 99999999
using namespace std;
struct Node{
    int data,height;
    Node *lc,*rc;
    Node(){}
    Node(int a):data(a),height(0),lc(NULL),rc(NULL){} 
};
int getHeight(Node* &a){
    if(a==NULL)return -1;
    else return a->height;
}
void LL(Node* &root){
    Node *a=root->lc;
    root->lc=a->rc;
    a->rc=root;
    a->height=max(getHeight(a->lc),getHeight(a->rc))+1;
    root->height=max(getHeight(root->lc),getHeight(root->rc))+1;
    root=a;
}
void RR(Node* &root){
    Node *a=root->rc;
    root->rc=a->lc;
    a->lc=root;
    a->height=max(getHeight(a->lc),getHeight(a->rc))+1;
    root->height=max(getHeight(root->lc),getHeight(root->rc))+1;
    root=a;
}
void LR(Node* &root){
    RR(root->lc);
    LL(root);
} 
void RL(Node* &root){
    LL(root->rc);
    RR(root);
} 
void insert(Node* &root,int val){
    if(root==NULL){
        root=new Node(val);
        return ;
    }
    if(val<root->data){
        insert(root->lc,val);
        if(getHeight(root->lc)-getHeight(root->rc)>1){
            if(val<root->lc->data)LL(root);
            else LR(root);
        }
    }
    if(val>=root->data){
        insert(root->rc,val);
        if(getHeight(root->rc)-getHeight(root->lc)>1){
            if(val>root->rc->data)RR(root);
            else RL(root);
        }
    }
    root->height=max(getHeight(root->lc),getHeight(root->rc))+1;
}
int N;
int main(){
    scanf("%d",&N);
    Node *root=NULL;
    int a;
    for(int i=0;i<N;i++){
        scanf("%d",&a);
        insert(root,a);
    }
    printf("%d\n",root->data);
    return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值