[PAT解题报告] Root of AVL Tree

不喜欢这种题——模拟AVL树,要不断地做旋转以维护树地平衡,返回最终根节点。
AVL树及旋转比较复杂,具体可以参看
http://blog.chinaunix.net/uid-25324849-id-2182877.html
除了旋转就没别的东西了。。。。。

代码:

#include <cstdio>
#include <cstring>
#include <string>

using namespace std;

struct node {
int val, height;
node *left, *right;
};

node all[22];
int total;

int height(node *a) {
    return a?a->height:(-1);
}

node *LL(node *k2) {
node *k1 = k2->left;
    k2->left = k1->right;
    k1->right = k2;
    k2->height = max(height(k2->left), height(k2->right)) + 1;
    k1->height = max(height(k1->left), height(k1->right)) + 1;
    return k1;
}


node *RR(node *k1) {
node *k2 = k1->right;
    k1->right = k2->left;
    k2->left = k1;
    k1->height = max(height(k1->left), height(k1->right)) + 1;
    k2->height = max(height(k2->left), height(k2->right)) + 1;
    return k2;
}

node *LR(node *k3) {
    k3->left = RR(k3->left);
    return LL(k3);
}

node *RL(node *k1) {
    k1->right = LL(k1->right);
    return RR(k1);
}

node *insert(node *root,int x) {
    if (root == 0) {
        root = &all[total++];
        root->left = root->right = 0;
        root->val = x;
        root->height = 0;
    }
    else if (x < root->val) {
        root->left = insert(root->left, x);
        if (height(root->left) - height(root->right) == 2) {
            root = (x < root->left->val)?LL(root):LR(root);
        }
    }
    else {
        root->right = insert(root->right, x);
        if (height(root->right) - height(root->left) == 2) {
            root = (x > root->right->val)?RR(root):RL(root);
        }
    }
    root->height = max(height(root->left), height(root->right)) + 1;
    return root;
}

int main() {
int n;
node *root = 0;
    for (scanf("%d",&n);n;--n) {
        int x;
        scanf("%d",&x);    
        root = insert(root, x);
    }
    printf("%d\n",root->val);
    return 0;
}


原题链接: http://www.patest.cn/contests/pat-a-practise/1066

全部评论
看到LL,RR旋转,我就懵逼了。连写代码的想法都没有了
点赞 回复
分享
发布于 2016-03-15 23:31
#include<bits/stdc++.h> using namespace std; struct Node{ int val; Node* left=NULL; Node *right = NULL; }; //最终分解为向左向右两种旋转 Node * LL(Node *root){ Node *head = root->left; root->left = head->right; head->right = root; return head; } Node * RR(Node *root){ Node *head = root->right; root->right = head->left; head->left = root; return head; } Node * LR(Node *root){ root->left = RR(root->left); return LL(root); } Node * RL(Node *root){ root->right = LL(root->right); return RR(root); } int getDepth(Node *root,int d){ if (!root)return d; return max(getDepth(root->left, d + 1), getDepth(root->right, d + 1)); } Node* InsertAVL(Node* root, Node* n){ if (!root){ root = n; }else if (n->val<root->val){ root->left = InsertAVL(root->left, n); if (getDepth(root->left,1) - getDepth(root->right,1) == 2) { if (n->val < root->left->val) root = LL(root); else root = LR(root); } }else if (n->val>root->val){ root->right = InsertAVL(root->right, n); if (getDepth(root->left, 1) - getDepth(root->right, 1) == -2) { if (n->val > root->right->val) root = RR(root); else root = RL(root); } } return root; } int main(){ int N; cin >> N; Node *root = new Node; cin >> root->val; for (int i = 1; i < N;i++){ Node *n = new Node; cin >> n->val; root=InsertAVL(root, n); } cout << root->val; return 0; }
点赞 回复
分享
发布于 2017-07-13 14:40
联想
校招火热招聘中
官网直投

相关推荐

点赞 收藏 评论
分享
牛客网
牛客企业服务