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) return -1;
    return node->height;
}

inline int max(int a, int b) {return a>b?a:b;}
/* K2 is the first node violates the AVL property, K1 is its left node
   violation is caused by insert a node into the K1‘s right sub-tree
                (K2)                      (K1)
                /      LL-rotate          /              (K1)     -------------->   (new) (K2)
            /
        (new)
 */
Node* rotateLL(Node* root) {
    Node* K1 = root->L;
    Node* K2 = root;

    Node* k1_rsub = K1->R;
    K1->R = K2;
    K2->L = k1_rsub;

    K1->height = max(height(K1->L), height(K1->R)) + 1;
    K2->height = max(height(K2->L), height(K2->R)) + 1;
    return K1;
}

/* K1 is the first node violates the AVL property, K2 is its right node
   violation is caused by insert a node into the K2‘s left sub-tree
   (K1)                                  (K2)
      \             RR-rotate            /        (K2)       ---------------->     (K1) (new)
                (new)
*/
Node* rotateRR(Node* root) {
    Node* K1 = root;
    Node* K2 = root->R;
    Node* k2_lsub = K2->L;
    K2->L = K1;
    K1->R = k2_lsub;

    K1->height = max(height(K1->L), height(K1->R)) + 1;
    K2->height = max(height(K2->L), height(K2->R)) + 1;

    return K2;
}

/*
    first do LL rotate on K3, then do RR rotate on K1
    (K1)          (K1)                   (K2)
      \              \                   /        (K3) ------>   (K2)   -------->  (K1) (K3)
      /                    (K2)               (K3)
*/
Node* rotateRL(Node* root) {
    Node* K1 = root;
    Node* K2 = root->R->L;
    Node* K3 = root->R;

    K1->R = rotateLL(K3);
    return rotateRR(K1);
}

/*
    first do RR rotate on K1, then do LL rotate on K3
     (K3)              (K3)           (K2)
      /                /              /      (K1)   ------>   (K2)  ------>  (K1) (K3)
      \              /
      (K2)         (K1)
*/
Node* rotateLR(Node* root) {
    Node* K1 = root->L;
    Node* K2 = root->L->R;
    Node* K3 = root;

    K3->L = rotateRR(K1);
    return rotateLL(K3);
}

Node* insert(Node* root, int value) {
    if (root == NULL) {
        return new Node(value);
    }
    if (value < root->data) {
        root->L = insert(root->L, value);
        // do AVL property check
        if (height(root->L) - height(root->R) == 2) {
            if (value < root->L->data) {
                // LL case, single rotation
                root = rotateLL(root);
            } else if (value > root->L->data) {
                // LR case, double rotation
                root = rotateLR(root);
            }
        }
    } else if (value > root->data ){
        root->R = insert(root->R, value);
        // do AVL property check
        if (height(root->R) - height(root->L) == 2) {
            if (value > root->R->data) {
                // RR case, single rotation
                root = rotateRR(root);
            } else if (value < root->R->data) {
                // RL case, double rotation
                root = rotateRL(root);
            }
        }
    } else {
        // equal, do nothing
    }

    root->height= max(height(root->L), height(root->R)) + 1;
    return root;
}

int main() {
    Node* r = NULL;

    int N;
    scanf("%d", &N);
    for (int i=0; i<N; i++) {
        int d;
        scanf("%d", &d);
        r = insert(r, d);
    }
    if (r != NULL) {
        printf("%d", r->data);
    }
    return 0;
}

第一次自己写AVL树,参照照Data Structures and Alogrithm Analysis in C第二版中AVL树的代码

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

PAT 1066 Root of AVL Tree的相关文章

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

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

PTA (Advanced Level)1066 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

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

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

PAT (Advanced Level) 1066. Root of AVL Tree (25)

AVL树的旋转.居然1A了.... 了解旋转方式之后,数据较小可以当做模拟写. #include<cstdio> #include<cstring> #include<cmath> #include<vector> #include<map> #include<stack> #include<queue> #include<string> #include<algorithm> using name

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

我嗯,这道题比较讨厌,非常难,需要用到马尔科夫随便链.高斯混合复制模型.神经网络还需要用到本人的模拟退火算法进行优化搜索,为了搜索文章避免陷入局部优化,还得用到一些Tabu Search代码比较多写的有点乱七八糟,有的还点长,都贴到laji的网站如了.布料布衣价格上涨,扣子费用降低.这样导致过程很难算,有难度,但还是有点意思的,建议自省反省考虑下,嗯么. #include <iostream> #include <cstdio> #include <algorithm>