PAT----A1066 Root of AVL Tree (25point(s))

A1066 Root of AVL Tree (25point(s))

题意

手写AVL,插入n个节点后输出根节点的key。

思路

手写AVL,一开始不会写,因为AVL早就忘了,什么LL、RR、LR、RL都不记得了。。。。参考了一下别人的博客终于把AVL写了出来(只有insert方法)。

总结

知识盲区,AVL要会写。
先贴一下AVL:

struct Node {
    int v{};
    Node *l = nullptr;
    Node *r = nullptr;
    int h{1};

    Node(int v) : v(v) {}
};

class AVLTree {
public:
    void insert(int v) {
        root = insert(root, v);
    }

    Node *root = nullptr;
private:
    Node *insert(Node *node, int v) {
        if (node == nullptr) return new Node(v);
        if (v < node->v) {
            node->l = insert(node->l, v);
        } else
            node->r = insert(node->r, v);
        updateHeight(node);
        return balance(node);
    }

    Node *balance(Node *node) {
        int fac = getBalanceFac(node);
        // LL
        if (fac > 1 && getBalanceFac(node->l) >= 0)
            return rightRotate(node);
        // RR
        if (fac < -1 && getBalanceFac(node->r) <= 0)
            return leftRotate(node);
        // LR
        if (fac > 1 && getBalanceFac(node->l) < 0) {
            node->l = leftRotate(node->l);
            return rightRotate(node);
        }
        // RL
        if (fac < -1 && getBalanceFac(node->r) > 0) {
            node->r = rightRotate(node->r);
            return leftRotate(node);
        }
        return node; // do not need balance
    }

    int getHeight(Node *node) {
        return node == nullptr ? 0 : node->h;
    }

    void updateHeight(Node *node) {
        node->h = max(getHeight(node->l), getHeight(node->r)) + 1;
    }

    int getBalanceFac(Node *node) {
        return node == nullptr ? 0 : getHeight(node->l) - getHeight(node->r);
    }

    Node *rightRotate(Node *node) {
        Node *pt = node->l;
        node->l = pt->r;
        pt->r = node;
        updateHeight(node);
        updateHeight(pt);
        return pt;
    }

    Node *leftRotate(Node *node) {
        Node *pt = node->r;
        node->r = pt->l;
        pt->l = node;
        updateHeight(node);
        updateHeight(pt);
        return pt;
    }
};

Sample Input1:

5
88 70 61 96 120

Sample Output1:

70

Sample Input2:

7
88 70 61 96 120 90 65

Sample Output2:

88
#include "bits/stdc++.h"
using namespace std;

struct Node {
    int v{};
    Node *l = nullptr;
    Node *r = nullptr;
    int h{1};

    Node(int v) : v(v) {}
};

class AVLTree {
public:
    void insert(int v) {
        root = insert(root, v);
    }

    Node *root = nullptr;
private:
    Node *insert(Node *node, int v) {
        if (node == nullptr) return new Node(v);
        if (v < node->v) {
            node->l = insert(node->l, v);
        } else
            node->r = insert(node->r, v);
        updateHeight(node);
        return balance(node);
    }

    Node *balance(Node *node) {
        int fac = getBalanceFac(node);
        // LL
        if (fac > 1 && getBalanceFac(node->l) >= 0)
            return rightRotate(node);
        // RR
        if (fac < -1 && getBalanceFac(node->r) <= 0)
            return leftRotate(node);
        // LR
        if (fac > 1 && getBalanceFac(node->l) < 0) {
            node->l = leftRotate(node->l);
            return rightRotate(node);
        }
        // RL
        if (fac < -1 && getBalanceFac(node->r) > 0) {
            node->r = rightRotate(node->r);
            return leftRotate(node);
        }
        return node; // do not need balance
    }

    int getHeight(Node *node) {
        return node == nullptr ? 0 : node->h;
    }

    void updateHeight(Node *node) {
        node->h = max(getHeight(node->l), getHeight(node->r)) + 1;
    }

    int getBalanceFac(Node *node) {
        return node == nullptr ? 0 : getHeight(node->l) - getHeight(node->r);
    }

    Node *rightRotate(Node *node) {
        Node *pt = node->l;
        node->l = pt->r;
        pt->r = node;
        updateHeight(node);
        updateHeight(pt);
        return pt;
    }

    Node *leftRotate(Node *node) {
        Node *pt = node->r;
        node->r = pt->l;
        pt->l = node;
        updateHeight(node);
        updateHeight(pt);
        return pt;
    }
};

int main() {
//    freopen("input.txt","r",stdin);
    int n;
    cin >> n;
    AVLTree tree;
    for (int i = 0; i < n; ++i) {
        int t;
        scanf("%d", &t);
        tree.insert(t);
    }
    cout << tree.root->v << endl;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值