码迷,mamicode.com
首页 > 其他好文 > 详细

PAT 1066 Root of AVL Tree[AVL树][难]

时间:2018-08-19 19:57:39      阅读:147      评论:0      收藏:0      [点我收藏+]

标签:osi   分享图片   clu   pac   seq   ++   操作   one   problem   

1066 Root of AVL Tree (25)(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.

技术分享图片 技术分享图片

技术分享图片 技术分享图片

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平衡条件,左右高度相差≤1,根据插入的数不算进行左旋右旋调整平衡。从来没写过,学习一下大佬的算法。

代码来自:https://www.liuchuo.net/archives/2178

#include <iostream>
#include<stdio.h>
using namespace std;
struct node {
    int val;
    struct node *left, *right;
};
node *rotateLeft(node *root) {
    node *t = root->right;
    root->right = t->left;
    t->left = root;
    return t;
}
node *rotateRight(node *root) {
    node *t = root->left;//这个左旋右旋的基本操作很厉害。
    root->left = t->right;
    t->right = root;
    return t;
}
node *rotateLeftRight(node *root) {
    root->left = rotateLeft(root->left);
    return rotateRight(root);
}
node *rotateRightLeft(node *root) {
    root->right = rotateRight(root->right);
    return rotateLeft(root);
}
int getHeight(node *root) {//真厉害,我就写不出这种递归。
    if(root == NULL) return 0;
    return max(getHeight(root->left), getHeight(root->right)) + 1;
}
node *insert(node *root, int val) {//使用指针传入,实时更新
    if(root == NULL) {
        root = new node();
        root->val = val;
        root->left = root->right = NULL;
    } else if(val < root->val) {
        root->left = insert(root->left, val);//递归插入,厉害
        if(getHeight(root->left) - getHeight(root->right) == 2)//如果左子树过高。
            root = val < root->left->val ? rotateRight(root) : rotateLeftRight(root);
    } else {
        root->right = insert(root->right, val);
        if(getHeight(root->left) - getHeight(root->right) == -2)//如果右子树过高。
            root = val > root->right->val ? rotateLeft(root) : rotateRightLeft(root);
            //根据这个来判断是如果>的话,那么就是插入了右子树中的右子树。
            //如果是<的话,那么就是插入了右子树中的左子树,那么就需要右左旋。
    }
    return root;//注意最终返回的是根节点,这样才建成了二叉树~~~
}
int main() {
    int n, val;
    scanf("%d", &n);
    node *root = NULL;
    for(int i = 0; i < n; i++) {
        scanf("%d", &val);
        root = insert(root, val);
    }
    printf("%d", root->val);
    return 0;
}

 

1.递归完成建立与插入的操作。

2.左旋、右旋、基本操作。

3.如何判断是插入左子树还是右子树。 

PAT 1066 Root of AVL Tree[AVL树][难]

标签:osi   分享图片   clu   pac   seq   ++   操作   one   problem   

原文地址:https://www.cnblogs.com/BlueBlueSea/p/9502118.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!