pat 甲级 1066. Root of AVL Tree (25)

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.

    

 

    

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

题意:AVL树的实现。
AC代码:
#define _CRT_SECURE_NO_DEPRECATE
#include<iostream>
#include<algorithm>
#include<cmath>
#include<cstring>
#include<string>
#include<set>
#include<queue>
#include<map>
using namespace std;
#define INF 0x3f3f3f
#define N_MAX 100+5
typedef long long ll;
int n,a[N_MAX];
struct AVLtree {
    int key=INF;
    AVLtree* l_child, *r_child;
    int height;
}T;
AVLtree *NIL;
AVLtree* root;
int Height(AVLtree* T) {//返回x的高度
    int l_height=0, r_height=0;
    if (T) {
        l_height = Height(T->l_child);
        r_height = Height(T->r_child);
        return T->height = max(l_height, r_height) + 1;
    }
    return 0;//节点不存在,没有高度
}

AVLtree* LeftRotation(AVLtree* a) {//左单旋
    AVLtree *b = a->l_child;
    a->l_child = b->r_child;
    b->r_child = a;
    a->height = Height(a);
    b->height = Height(b);
    return b;
}
AVLtree* RightRotation(AVLtree* a) {//右单旋
    AVLtree* b = a->r_child;
    a->r_child = b->l_child;
    b->l_child = a;
    a->height = Height(a);
    b->height = Height(b);
    return b;
}

AVLtree* LeftRightRotation(AVLtree* a) {
    a->l_child = RightRotation(a->l_child);
    return LeftRotation(a);
}
AVLtree* RightLeftRotation(AVLtree* a) {
    a->r_child = LeftRotation(a->r_child);
    return RightRotation(a);
}


AVLtree* insert(int key,AVLtree*root) {//root是当前AVL树的根,  返回根
    if (root == NIL) {
        root = new AVLtree;
        root->key = key;
        root->height = 1;
        root->l_child = root->r_child = NIL;
    }
     if (key < root->key) {
        root->l_child = insert(key,root->l_child);
        if (Height(root->l_child)-Height(root->r_child)==2) {
            if (key < root->l_child->key) {
                root = LeftRotation(root);
            }
            else root = LeftRightRotation(root);
        }
    }
    else if (key > root->key) {
        root->r_child = insert(key,root->r_child);
        if (Height(root->r_child) - Height(root->l_child) == 2) {
            if (key > root->r_child->key) {
                root = RightRotation(root);
            }
            else root = RightLeftRotation(root);
        }
    }
    
    root->height = Height(root);//节点插入完毕,计算当前根的高度
    return root;
}

int main() {
    while (scanf("%d",&n)!=EOF) {
        for (int i = 0; i < n; i++) {
            int a; scanf("%d",&a);
            root=insert(a, root);    
        }
        printf("%d\n",root->key);
    }
    return 0;
}

 

posted on 2018-03-12 22:16  ZefengYao  阅读(113)  评论(0编辑  收藏  举报

导航