跳到正文

剪贴板

剪贴板 s553l03d

原文
剪贴板信息

剪贴板信息

收录
更新

AVL 实现

C++
#ifndef AVL_H
#define AVL_H

#include <vector>
#include <sstream>
#include <functional>

template <class T>
class AVLTreeNode {
public:
    T val;
    AVLTreeNode<T> *left, *right;
    long cnt, size, high;

    AVLTreeNode() :
    val(T()), left(nullptr), right(nullptr), cnt(1), size(1), high(1) {}

    AVLTreeNode(const T& v) :
    val(v), left(nullptr), right(nullptr), cnt(1), size(1), high(1) {}

    AVLTreeNode<T>* pushup() {
        size = cnt + (left ? left->size : 0) + (right ? right->size : 0);
        high = std::max(left ? left->high : 0, right ? right->high : 0) + 1;
        return this;
    }
};

template <class T, class Cmp = std::less<T>>
class AVLTree {
protected:
    using node = AVLTreeNode<T>;
    Cmp cmp = Cmp();

    node *root;

    static node* get_min(node* cur) {
        node *x = cur;
        while (x && x->left) x = x->left;
        return x;
    }
    static node* get_max(node* cur) {
        node *x = cur;
        while (x && x->right) x = x->right;
        return x;
    }

    static node* left_rotate(node* p) {
        node *q = p->left;
        p->left = q->right, q->right = p, p->pushup();
        return q->pushup();
    }
    static node* right_rotate(node* p) {
        node *q = p->right;
        p->right = q->left, q->left = p, p->pushup();
        return q->pushup();
    }
    static node* left_right_rotate(node* p) {
        p->left = right_rotate(p->left);
        return left_rotate(p);
    }
    static node* right_left_rotate(node* p) {
        p->right = left_rotate(p->right);
        return right_rotate(p);
    }
    static long get_high(node* p) {return p ? p->high : 0;}

    void insert(node*& cur, const T& val) {
        if (!cur) {
            cur = new node(val);
            return;
        }
        if (val == cur->val) {
            cur->cnt++, cur->pushup();
            return;
        }
        if (cmp(val, cur->val)) {
            insert(cur->left, val), cur->pushup();
            if (get_high(cur->left) - get_high(cur->right) >= 2) {
                cur = cmp(val, cur->left->val) ?
                left_rotate(cur) : left_right_rotate(cur);
            }
        } else {
            insert(cur->right, val), cur->pushup();
            if (get_high(cur->right) - get_high(cur->left) >= 2) {
                cur = cmp(val, cur->right->val) ?
                right_left_rotate(cur) : right_rotate(cur);
            }
        }
        cur->pushup();
    }

    bool remove_node(node*& cur) {
        if (!cur) return false;
        if (cur->cnt > 1) {
            cur->cnt--, cur->pushup();
            return true;
        }
        if (cur->left && cur->right) {
            node* replace = this->get_min(cur->right);
            cur->cnt = replace->cnt, cur->val = replace->val;
            replace->cnt = 1;
            remove(cur->right, replace->val), cur->pushup();
            if (get_high(cur->left) - get_high(cur->right) >= 2) {
                cur = (get_high(cur->left->left) >= get_high(cur->left->right)) ?
                left_rotate(cur) : left_right_rotate(cur);
            }
        } else {
            cur = cur->left ? cur->left : cur->right;
        }
        if (cur) cur->pushup();
        return true;
    }
    bool remove(node*& cur, const T& val) {
        if (!cur) return false;
        if (val == cur->val) return remove_node(cur);
        bool res;
        if (cmp(val, cur->val)) {
            res = remove(cur->left, val), cur->pushup();
            if (get_high(cur->right) - get_high(cur->left) >= 2) {
                cur = get_high(cur->right->right) >= get_high(cur->right->left) ?
                right_rotate(cur) : right_left_rotate(cur);
            }
        } else {
            res = remove(cur->right, val), cur->pushup();
            if (get_high(cur->left) - get_high(cur->right) >= 2) {
                cur = get_high(cur->left->left) >= get_high(cur->left->right) ?
                left_rotate(cur) : left_right_rotate(cur);
            }
        }
        if (cur) cur->pushup();
        return res;
    }

    int rank(node* cur, const T& val) const {
        if (!cur) return 1;
        int left_size = cur->left ? cur->left->size : 0;
        if (val == cur->val) return left_size + 1;
        if (cmp(val, cur->val)) return rank(cur->left, val);
        return rank(cur->right, val) + left_size + cur->cnt;
    }
    T kth(node* cur, int k) const {
        //cout << k << endl;
        if (!cur) return T();
        int left_size = cur->left ? cur->left->size : 0;
        if (left_size >= k) return kth(cur->left, k);
        if (left_size < k - cur->cnt) return kth(cur->right, k - left_size - cur->cnt);
        return cur->val;
    }

    node* find(const T& val) const {
        node* cur = root;
        while (cur && val != cur->val) {
            cur = cmp(val, cur->val) ? cur->left : cur->right;
        }
        return cur;
    }

public:
    AVLTree() : root(nullptr) {}
    AVLTree(std::vector<T>& arr) : root(nullptr) {
        for (const T& v : arr) insert(v);
    }
    template <typename it>
    AVLTree(const it& begin, const it& end) {
        for (auto i = begin; i != end; i++) insert(*i);
    }

    int size() const {return root ? root->size : 0;}
    bool empty() const {return !root;}

    std::vector<T> inorder() const {
        if (!root) return {};
        std::vector<T> res; res.reserve(size());

        std::function<void(const node*)> dfs = [&](const node* x) -> void {
            if (!x) return;
            dfs(x->left);
            for (int i = 1; i <= x->cnt; i++) res.emplace_back(x->val);
            dfs(x->right);
        };
        dfs(root);
        return res;
    }
    std::vector<T> preorder() const {
        if (!root) return {};
        std::vector<T> res; res.reserve(size());

        std::function<void(const node*)> dfs = [&](const node* x) -> void {
            if (!x) return;
            for (int i = 1; i <= x->cnt; i++) res.emplace_back(x->val);
            dfs(x->left);
            dfs(x->right);
        };
        dfs(root);
        return res;
    }
    std::vector<T> postorder() const {
        if (!root) return {};
        std::vector<T> res; res.reserve(size());

        std::function<void(const node*)> dfs = [&](const node* x) -> void {
            if (!x) return;
            dfs(x->left);
            dfs(x->right);
            for (int i = 1; i <= x->cnt; i++) res.emplace_back(x->val);
        };
        dfs(root);
        return res;
    }

    std::string prettify(int tabs = 4, char fill = ' ') const {
        if (!root) return "";
        std::stringstream ss;
        ss << root->val << '(' << root->cnt << ")\n";

        std::string fill1, fill2;
        for (int i = 1; i < tabs; i++) fill1.push_back(fill);
        for (int i = 1; i <= tabs; i++) fill2.push_back(fill);

        std::function<void(const node*, int, std::vector<int>)> dfs =
        [&](const node* x, int depth, std::vector<int> sites) -> void {
            int void_num = 0;
            for (node* child : {x->left, x->right}) {
                if (!child) continue;
                int n = depth - void_num - (int) sites.size();
                std::vector<std::string> str_list;
                str_list.reserve(n << 1);
                for (int i = 1; i <= n; i++) str_list.push_back("│" + fill1);
                for (int site : sites) {
                    str_list.insert(str_list.begin() + site, fill2);
                }
                if (x->right && child != x->right) {
                    str_list.emplace_back("\u251c\u2500\u2500 ");
                } else {
                    str_list.emplace_back("\u2514\u2500\u2500 ");
                    void_num++, sites.push_back(depth);
                }

                std::string out;
                for (const std::string& s : str_list) out += s;
                ss << out << child->val << '(' << child->cnt << ")\n";

                dfs(child, depth + 1, sites);
                if (child == x->right) void_num--, sites.pop_back();
            }
        };
        dfs(root, 0, {});
        return ss.str();
    }

    T minimum() {
        if (!root) throw -1;
        return get_min(root)->val;
    }
    T maximum() {
        if (!root) throw -1;
        return get_max(root)->val;
    }

    void insert(const T& val) {insert(root, val);}
    bool remove(const T& val) {return remove(root, val);}

    int rank(const T& val) const {return rank(root, val) - 1;}

    T kth(int k) const {return kth(root, k + 1);}
    T operator[] (int k) const {return kth(k);}

    int count(const T& val) const {
        node* x = find(root, val);
        return x ? x->cnt : 0;
    }

    T lower_bound(const T& val) const {
        node *cur = root;
        T res = T();
        while (cur) {
            if (cmp(cur->val, val)) res = cur->val, cur = cur->right;
            else cur = cur->left;
        }
        return res;
    }
    T upper_bound(const T& val) const {
        node *cur = root;
        T res = T();
        while (cur) {
            if (cmp(val, cur->val)) res = cur->val, cur = cur->left;
            else cur = cur->right;
        }
        return res;
    }
};

#endif //AVL_H