Treap樹堆實現簡單Set C++實現

Treap樹堆實現簡單Set C++實現

Treap樹實現

Treap樹實現

實現代碼

/*
author : eclipse
email  : [email protected]
time   : Sat May 23 10:51:30 2020
*/
#include<bits/stdc++.h>
using namespace std;

struct Node {
    int data;
    Node *left;
    Node *right;
    int priority;
    Node(int value, int level) : data(value), left(NULL), right(NULL), priority(level) {}
};

class Treap {
private:
    Node *root;
    void leftRotate(Node* &p);
    void rightRotate(Node* &p);
    void insert(Node* &p, int value);
    void remove(Node* &p, int value);
public:
    Treap();
    void insert(int value);
    void remove(int x);
    bool search(int x);
    Node* treapRoot();
};

Treap::Treap() {
    root = NULL;
}

void Treap::leftRotate(Node* &p) {
    Node *k = p->right;
    p->right = k->left;
    k->left = p;
    p = k;
}

void Treap::rightRotate(Node* &p) {
    Node *k = p->left;
    p->left = k->right;
    k->right = p;
    p = k;
}

void Treap::insert(int value) {
    insert(root, value);
}

void Treap::insert(Node* &p, int value) {
    if (p == NULL) {
        p = new Node(value, rand());
    } else {

        if (value == p->data) {
            return;
        } else if (value < p->data) {
            insert(p->left, value);
        } else {
            insert(p->right, value);
        }

        if(p->left && p->left->priority > p->priority) {
            rightRotate(p);
        } else if(p->right && p->right->priority < p->priority) {
            leftRotate(p);
        }
    }
}

void Treap::remove(int value) {
    remove(root, value);
}

void Treap::remove(Node* &p, int value) {
    if (!p) {
        return;
    }
    if (p->data == value) {
        if (p->left == NULL) {
            p = p->right;
        } else if (p->right == NULL) {
            p = p->left;
        } else {
            if (p->left->priority > p->right->priority) {
                rightRotate(p);
                remove(p->right, value);
            } else if (p->left->priority < p->right->priority) {
                leftRotate(p);
                remove(p->left, value);
            }
        }
    } else {
        if (value < p->data) {
            remove(p->left, value);
        } else {
            remove(p->right, value);
        }
    }
}

bool Treap::search(int value) {
    Node *p = root;
    while (p) {
        if (p->data == value) {
            return true;
        } else {
            p = p->data < value ? p->right : p->left;
        }
    }
    return false;
}

Node* Treap::treapRoot() {
    return root;
}

class Set {
private:
    Treap treap;
public:
    Set() {};
    void insert(int value);
    void erase(int value);
    bool contain(int value);
    bool empty();
};

void Set::insert(int value) {
    treap.insert(value);
}

void Set::erase(int value) {
    treap.remove(value);
}

bool Set::contain(int value) {
    return treap.search(value);
}

bool Set::empty() {
    return treap.treapRoot() == NULL;
}

int main(int argc, char const *argv[]) {
    Set set;
    int N;
    scanf("%d", &N);
    int value;
    for (int i = 0; i < N; i++) {
        scanf("%d", &value);
        set.insert(value);
    }
    for (int i = 0; i < N; i++) {
        scanf("%d", &value);
        printf("%s ", set.contain(value) ? "TRUE" : "FALSE");
    }
    printf("\n");
    for (int i = 0; i < N; i++) {
        scanf("%d", &value);
        set.erase(value);
    }
    printf("%s\n", set.empty() ? "EMPTY" : "NOT EMPTY");
    return 0;
}

算法思路

  • 主流的STL::set<T>都是用一種動態平衡的BST紅黑樹實現的,這裏利用Treap樹簡單地實set<int>
  • 依賴Treap樹堆的增刪查操作即可實現,將其封裝在Set類中

輸入數據

10
20 19 28 34 123 8900 21334 4345 234 567
20 19 28 34 123 8900 21334 4345 234 567
20 19 28 34 123 8900 21334 4345 234 567

輸出數據

TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE
EMPTY

最後

  • 由於博主水平有限,不免有疏漏之處,歡迎讀者隨時批評指正,以免造成不必要的誤解!
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章