zoukankan      html  css  js  c++  java
  • treap-名次树-树堆

    #include <cstring>
    #include <cstdio>
    #include <cstdlib>
    using namespace std;
    struct Node{
        Node *ch[2];
        int rank , value , size;
        const static int  mod = 100;
        Node(int x){
            value = x;
            rank = rand();
            ch[1] = ch[0] = NULL;
            size = 0;
        }
        bool operator < (int & rht) const{
            return value < rht;
        }
        bool operator == (int & rht) const{
            return value == rht;
        }
    };
    class Treap{
    private:
        static int insert(Node * &o,int  x);
        static void rotate(Node * &o,int d);
        static int remove(Node * &o, int x);
        static int find(Node * &o, int x);
    
        static void show(Node * o);
        static void destory(Node * &o);
    public:
        Node * root ;
        Treap(){
            root = NULL;
        }
        ~Treap(){
            Treap :: destory(root);
        }
        int insert(int x){
            return Treap :: insert(root,x);
        }
        int remove(int x){
            return Treap :: remove(root,x);
        }
        int find(int x){
            return Treap :: find(root,x);
        }
        void show(){
            Treap :: show(root);
        };
    };
    int Treap :: insert(Node * &o , int x){
        if(o == NULL){
            o = new Node(x);
            return 1; 
        }
        if(o -> value == x) {
            (o -> size) ++;
            return o -> size; 
        }
        int guide = ( *o < x  ) ? 1 : 0 ;
        Treap ::insert(o-> ch[guide], x);
        if(o->ch[guide] -> rank > o -> rank ) {
            Treap::rotate(o,guide^1); 
        }
    }
    int Treap :: remove(Node * &o , int x){
        if( o == NULL )  return 0;
        if(o -> value == x){
            ( o -> size ) -- ;
            if( o -> size != 0) return 1;
            // 需要删除
            Node * del = o;
            if(o->ch[0] && o->ch[1]){
                int tmp = o->ch[0] -> rank  <   o->ch[1] -> rank ?  0 : 1;
                Treap :: rotate( o , tmp);
                return Treap :: remove(o->ch[tmp],x);
            }else{
                // 即使没有儿子节点也无所谓,因为会吧NULL配上;
                if( o-> ch[0] ) o = o -> ch[0];
                else            o = o -> ch[1];
                delete del;
                return 1;
            }
        }
        int guide = ( *o < x )? 1 : 0 ;
        Treap :: remove(o->ch[guide], x);
    }
    int Treap :: find(Node * &o ,  int x){
        if( o == NULL ) return 0; 
        if( o -> value == x) 
            return o -> size;
        int guide = ( *o < x )? 1 : 0;
        return Treap :: find(o->ch[guide] , x);
    }
    void Treap :: rotate(Node * &o , int d){
        Node * tmp = o -> ch[d^1];
        o -> ch[d^1] = tmp -> ch[d];
        tmp -> ch[d] = o;
        o = tmp;
    }
    void Treap :: show(Node * o){
        if( o == NULL ) return;
        printf("(");
        show(o->ch[0]);
        printf("%d/%d",o -> value, o->rank); 
        show(o->ch[1]);
        printf(")");
    }
    void Treap :: destory(Node * & o){
        if( o == NULL) return;
        if( o-> ch[0]) Treap :: destory(o->ch[0]);
        if( o-> ch[1]) Treap :: destory(o->ch[1]);
        delete o;
        o = NULL;
    }
    int main(){}
    

      

  • 相关阅读:
    Java中的引用
    JVM参数调优
    GCRoots
    JVM体系结构
    死锁编码及定位分析
    线程池(Java中有哪些方法获取多线程)
    Synchronized和Lock的区别
    阻塞队列BlockingQueue
    CountDownLatch/CyclicBarrier/Semaphore
    浅谈二分
  • 原文地址:https://www.cnblogs.com/shuly/p/6339860.html
Copyright © 2011-2022 走看看