• BST


    #include<cstdio>
    #include<cstdlib>
    using namespace std;
    struct treapNode {
        int val,ra;
        treapNode *left,*right;
    };
    
    void rightRoate(treapNode *&root) {
        treapNode *right = root->right;
        root->right = right->left;
        right->left = root;
        root = right;
    }
    
    void leftRoate(treapNode *&root) {
        treapNode *left = root->left;
        root->left = left->right;
        left->right = root;
        root = left;
    }
    
    void insert(treapNode *&root,int val) {
        if(!root) {
            root = new treapNode;
            root->val = val;
            root->ra = rand()*rand();
            root->left = root->right = NULL;
            return;
        }
        if(val <= root->val) {
            insert(root->left,val);
            if(root->ra > root->left->ra) {
                leftRoate(root);
            }
        }
        else {
            insert(root->right,val);
            if(root->ra > root->right->ra) {
                rightRoate(root);
            }
        }
    }
    
    treapNode** find(treapNode *&root,int val) {
        if(!root) {
            return NULL;
        }
        if(val == root->val) {
            return &root;
        } else if(val < root->val) {
            return find(root->left,val);
        } else {
            return find(root->right,val);
        }
    }
    
    void erase(treapNode *&root) {
        if(!root->left) {
            treapNode* tmp = root;
            root = root->right;
            delete tmp;
            return;
        } 
        if(!root->right) {
            treapNode *tmp = root;
            root = root->left;
            delete tmp;
            return;
        }
        if(root->left->ra > root->right->ra) {
            rightRoate(root);
            erase(root->left);
        } else {
            leftRoate(root);
            erase(root->right);
        }
    }
    
    
    void destroyTreap(treapNode *&root,bool &first) {
        if(!root) {
            return;
        }
        destroyTreap(root->left,first);
        if(!first) printf(" ");
        printf("%d",root->val);
        first = false;
        destroyTreap(root->right,first);
        //root->left = root->right = NULL;
        //delete root;
    }
    
    int main() {
        int n;
        treapNode*root;
        while(scanf("%d",&n)==1) {
            root = NULL;
            for(int i=0,x;i<n;i++) {
                scanf("%d",&x);
                insert(root,x);
            }
            bool first = true;
            destroyTreap(root,first);
            printf("
    ");
            for(int x;scanf("%d",&x) == 1;) {
                treapNode **tmp = find(root,x);
                if(!tmp) {
                    printf("not exist
    ");
                    continue;
                }
                printf("find %d
    ",(*tmp)->val);
                erase(*tmp);
                destroyTreap(root,first);
                printf("
    ");
            }
        }
    }

     详细讲解:

    树堆 - wiki

  • 相关阅读:
    自动化测试-appium常用元素
    自动化测试-微信小程序
    自动化测试-环境搭建appium for windows
    安全测试-docker搭建sonar完成代码质量检测
    工具安装-pycharm使用已配置的虚拟环境
    安全测试-sonarscanner扫描代码
    工具安装-java集成到maven
    iOS 提升代码的安全性,可以做哪些措施???
    iOS 绘制一个表盘时钟,秒针效果可以“扫秒/游走”
    iOS 关于BTC 一些知识点
  • 原文地址:https://www.cnblogs.com/cdyboke/p/7795301.html
Copyright © 2020-2023  润新知