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

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.如何判断是插入左子树还是右子树。

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

时间: 2024-10-18 04:54:50

PAT 1066 Root of AVL Tree[AVL树][难]的相关文章

04-树5 Root of AVL Tree + AVL树操作集

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 illust

PAT 1066 Root of AVL Tree

#include <cstdio> #include <cstdlib> class Node { public: Node* L; Node* R; int height; int data; Node(int val, Node* l = NULL, Node* r = NULL, int h = 0): data(val), L(l), R(r), height(h) {} }; inline int height(Node* node) { if (node == NULL

Geeks Splay Tree Insert 树的插入操作

Splay树的插入操作,只需要处理好插入节点的孩子节点就可以了,最重要的是不要破坏了BST的基本规则. 因为高度并不是Splay树的首要因素,所以插入的时候也是使用splay操作,然后在根节点插入. 参考:http://www.geeksforgeeks.org/splay-tree-set-2-insert-delete/ 对比一下使用插入创建的树和手工创建数的区别,先序遍历的结果: #pragma once #include<stdio.h> #include <stdlib.h&g

1066. Root of AVL Tree (25)【AVL树】——PAT (Advanced Level) Practise

题目信息 1066. Root of AVL Tree (25) 时间限制100 ms 内存限制65536 kB 代码长度限制16000 B 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 tha

PAT 甲级 1066 Root of AVL Tree

https://pintia.cn/problem-sets/994805342720868352/problems/994805404939173888 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 m

PAT 甲级 1066 Root of AVL Tree (25 分)(快速掌握平衡二叉树的旋转,内含代码和注解)***

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

pat(A) 1066. Root of AVL Tree

代码: #include<iostream> #include<cstdio> #include<cmath> #include<stdlib.h> #define Max(a,b) ((a)>(b)?(a):(b)) using namespace std; struct Node { int value; Node* l; Node* r; int BF; //左子树高度 - 右子树高度 void init(int v) { value=v; l=

PAT Root of AVL Tree

Root of AVL Tree 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. F

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 o