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

PAT 1066 Root of AVL Tree

时间:2014-11-08 16:39:27      阅读:137      评论:0      收藏:0      [点我收藏+]

标签:style   blog   io   color   sp   for   div   on   log   

#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树的代码

PAT 1066 Root of AVL Tree

标签:style   blog   io   color   sp   for   div   on   log   

原文地址:http://www.cnblogs.com/lailailai/p/4083464.html

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