1066. Root of AVL Tree

題目鏈接:http://pat.zju.edu.cn/contests/pat-a-practise/1066

考察AVL樹的操作,大家從理論上都懂得過程,但實現起來。。。不知道怎麼下手啊

/*
 * 參考:http://biaobiaoqi.me/blog/2013/10/08/pat-1065-pat-1068/
 * 
 * 考察AVL樹的建立,調整操作,樹的高度的更新。
 * 需特別注意旋轉操作、及如何確定何種操作。
 *
 *
 */
#include <stdio.h>

struct Node
{
    Node *left;
    Node *right;
    int value;
    int height;

    // 構造
    Node(int v)
    {
        value = v;
        height = 0; // 葉節點層次爲0
        left = right = NULL; // 不可忘記指針初始化
    }
};

int max(int x, int y) { return x > y ?  x:y ; }

int get_height(Node *root) 
{
    return root == NULL ? -1 : root->height; //  NULL所在層次爲-1
}

Node * LL(Node *root)
{
    Node * t = root->left;
    root->left = t->right; //t的右孩子替代t,成爲根節點的左孩子
    t->right = root;

    t->height = max(get_height(t->left), get_height(t->right)) + 1;
    root->height = max(get_height(root->left), get_height(root->right)) + 1;
    return t; //返回根節點t
}

Node *RR(Node *root)
{
    Node * t = root->right;
    root->right = t->left; //t的左孩子替代t,成爲根節點的右孩子
    t->left = root;

    t->height = max(get_height(t->left), get_height(t->right)) + 1;
    root->height = max(get_height(root->left), get_height(root->right)) + 1;
    return t; //返回根節點t
}

Node *LR(Node *root)
{
    // 先對左子樹RR,再本身LL
    root->left = RR( root->left );
    return LL(root);
}

Node *RL(Node *root)
{
    // 先右子樹LL, 再左子樹RR
    root->right = LL( root->right);
    return RR(root);
}

Node *insert(Node *root, int v)
{
    if (root == NULL) {
        root = new Node(v);
    } 
    else
    {
        if(root->value > v)
        {
            // 插入左子樹
            root->left = insert(root->left, v);
            // 調整
            if ( (get_height(root->left) - get_height(root->right)) > 1)
            {
                if(get_height(root->left->left) > get_height(root->left->right))
                {
                    // LL
                    root = LL(root);
                }
                else
                {
                    // LR
                    root = LR(root);
                }
            }
        } 
        else if(root->value < v)
        {
            // 插入到右子樹
            root->right = insert(root->right, v);
            if( (get_height(root->right) - get_height(root->left)) > 1)
            {
                if( get_height(root->right->right) > get_height(root->right->left) )
                {
                    // RR
                    root = RR(root);
                }
                else
                {
                    root = RL(root);
                }
            }
        } // else {}題目聲明沒有相同值的節點
    }

    // 更新高度,遞歸插入,在子樹插入後,需要更新本節點高度
    root->height = max(get_height(root->left), get_height(root->right)) + 1;
    return root;
}

int main()
{
    int n;
    scanf("%d", &n);
    Node *root=NULL; // 建立樹根
    while(n-- > 0)
    {
        int t;
        scanf("%d", &t);
        root = insert(root, t);
    }

    printf("%d\n", root->value);
    return 0;
}


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章