PAT 1066. Root of AVL Tree (25) (AVL树)

               1066. Root of AVL Tree (25)

时间限制 100 ms

内存限制 65536 kB

代码长度限制 16000 B

判题程序
Standard 作者 CHEN, Yue

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.

《PAT 1066. Root of AVL Tree (25) (AVL树)》    
《PAT 1066. Root of AVL Tree (25) (AVL树)》

《PAT 1066. Root of AVL Tree (25) (AVL树)》    
《PAT 1066. Root of AVL Tree (25) (AVL树)》

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 ythe 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

提交代

知识点:(1)AVL(平衡二叉树)。 (2) AVL树的建立,请参考这篇博文: http://blog.csdn.net/gabriel1026/article/details/6311339       。

请结合图文参考一下代码:

#include <iostream>
#include <cstdio>
#include <vector>
#include <algorithm>
#include <cstring>
#define PI acos(-1)
#define inf 0x3f3f3f3f
#define SCD(a) scanf("%d",&a)
#define SCDD(a,b) scanf("%d%d",&a,&b)
#define SCF(a) scanf("%lf",&a)
#define PTD(a) printf("%d\n",a)
#define PTS(a) printf("%s\n",a)
#define MST(a,b) memset(a, b, sizeof(a))
using namespace std;
//                      PTA (Advanced Level) Practise 1066 Root of AVL Tree (25)

const int L = 10010;

struct AVLNode{
    int data;
    AVLNode* lchild;
    AVLNode* rchild;
};
AVLNode* T;

void LL(AVLNode* &T)
{
    AVLNode* t = T->rchild;
    T->rchild = t->lchild;
    t->lchild = T;
    T = t;
}

void RR(AVLNode* &T)
{
    AVLNode* t = T->lchild;
    T->lchild = t->rchild;
    t->rchild = T;
    T = t;
}

void RL(AVLNode* &T)
{
    RR(T->rchild);
    LL(T);
}

void LR(AVLNode* &T)
{
    LL(T->lchild);
    RR(T);
}

int Height(AVLNode* &T)
{
    if(!T)
        return 0;
    return max( Height(T->lchild), Height(T->rchild) ) + 1;
}

void InsertAVLNode(AVLNode* &T, int val)
{
    if(!T){
        T = new AVLNode;
        T->data = val;
        T->lchild = NULL;
        T->rchild = NULL;
        return;
    }
    if(val < T->data){
        InsertAVLNode(T->lchild, val);
        if(Height(T->lchild) - Height(T->rchild) == 2){
            if(val < T->lchild->data)
                RR(T);
            else
                LR(T);
        }
    }else{
        InsertAVLNode(T->rchild, val);
        if(Height(T->rchild) - Height(T->lchild) == 2){
            if(val > T->rchild->data)
                LL(T);
            else
                RL(T);
        }
    }
}

int main()
{
    int i, b, t, j, n;
    SCD(n);
    for(i=0;i<n;i++){
        SCD(t);
        InsertAVLNode(T, t);
    }
    PTD(T->data);
}

    原文作者:AVL树
    原文地址: https://blog.csdn.net/qq_37038938/article/details/76849621
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞