Treap标准模板

这是Treap的模板程序,支持Left/Right Rotate,Find the maxnum/minnum,Find the predecessor/successor of a node,Add/Delete nodes 等绝大多数功能(不包含类似于”查找排名第k的元素”这样奇怪的东西的代码)

#include<bits/stdc++.h>
#include<windows.h>
#define maxn 1000001
#define Random(x) (rand()%x)
#define ALLOW
#define query_pred_succ
#define query_delete

using namespace std;

typedef struct{
    int leftnode,rightnode,data,fix;        //data为数值,fix为修正值
    bool symbol;    //记录当前节点是否是空节点,0为空,1为非空 
}node;

class treap
{
    public:
        node p[maxn];
        int size,root;
        treap()
        {
            srand(time(0));
            size=0;
            root=0;
        }
        void Treap_Left_Rotate(int &x)
        {
            int y=p[x].rightnode;
            p[x].rightnode=p[y].leftnode;
            p[y].leftnode=x;
            x=y;
        }
        void Treap_Right_Rotate(int &x)
        {
            int y=p[x].leftnode;
            p[x].leftnode=p[y].rightnode;
            p[y].rightnode=x;
            x=y;
        }
        void Treap_insert(int &k,int key)
        {
            if (k==0)
            {
                k=++size;
                p[k].leftnode=p[k].rightnode=0;
                p[k].data=key;
                p[k].fix=rand();
            } else
            if (key<p[k].data)
            {
                Treap_insert(p[k].leftnode,key);
                if (p[p[k].leftnode].fix>p[k].fix)
                    Treap_Right_Rotate(k);
            }
            else
            {
                Treap_insert(p[k].rightnode,key);
                if (p[p[k].rightnode].fix>p[k].fix)
                    Treap_Left_Rotate(k);
            }
        }
        void Treap_delete(int &k,int key)
        {
            if (k==0) return;
            if (key==p[k].data)
            {
                if (p[k].leftnode==0 && p[k].rightnode==0) k=0;
                    else if (p[k].leftnode==0 && p[k].rightnode!=0) k=p[k].rightnode;
                        else if (p[k].leftnode!=0 && p[k].rightnode==0) k=p[k].leftnode;
                            else
                            if (p[p[k].leftnode].fix<p[p[k].rightnode].fix)
                            {
                                Treap_Left_Rotate(k);
                                Treap_delete(p[k].leftnode,key);
                            }
                            else
                            {
                                Treap_Right_Rotate(k);
                                Treap_delete(p[k].rightnode,key);
                            }
            } else
            if (key<p[k].data) Treap_delete(p[k].leftnode,key);
                else Treap_delete(p[k].rightnode,key);
        }
        void in_order_print(int k)
        {
            p[k].symbol=1;
            if (p[k].leftnode!=0)
                in_order_print(p[k].leftnode);
            printf("第%d个节点 : 值:%d   修正值:%d  左孩子:%d  右孩子:%d\n",k,p[k].data,p[k].fix,p[k].leftnode,p[k].rightnode);
            if (p[k].rightnode!=0)
                in_order_print(p[k].rightnode);
        }
        int find_max(int k)
        {
            if (p[k].rightnode!=0)
                return find_max(p[k].rightnode);
            else return p[k].data;
        }
        int find_min(int k)
        {
            if (p[k].leftnode!=0)
                return find_min(p[k].leftnode);
            else return p[k].data;
        }
        int Treap_pred(int k,int key,int optimal)
        {
            if (!p[k].symbol) return optimal;
            if (p[k].data<=key) return Treap_pred(p[k].rightnode,key,k);
                else return Treap_pred(p[k].leftnode,key,optimal);
        }
        int Treap_succ(int k,int key,int optimal)
        {
            if (!p[k].symbol) return optimal;
            if (p[k].data>=key) return Treap_succ(p[k].leftnode,key,k);
                else return Treap_succ(p[k].rightnode,key,optimal);
        }
};

treap T;
int main()
{
    int n,m;
    #ifdef ALLOW
        MessageBox(NULL,"This program is the standard Treap code\nAbility:Left/Right Rotate,Find the maxnum/minnum,Find the predecessor/successor of a node,Add/Delete nodes\n","Tips",MB_OK);
    #endif 
    printf("Input the total nodes number:\n");
    scanf("%d",&n);
    for (int i=1;i<=n;i++)
    {
        int tmp;
        printf("No.%d : You want to insert:",i);
        scanf("%d",&tmp);
        T.Treap_insert(T.root,tmp);
    }

    printf("After inserting,the Treap is:\n");
    T.in_order_print(T.root);
    printf("MAXNUM:     %d\n",T.find_max(T.root));
    printf("MINNUM:     %d\n",T.find_min(T.root));
    #ifdef query_pred_succ
        printf("How many nodes do you want to look for its predecessor/successor?\n");
        scanf("%d",&m);
        if (m>0)
        {
            printf("Input format for looking for the predecessor:P num\n");
            printf("Input format for looking for the successor  :S num\n");
            char cmd;
            int tmp;
            int a=1;
            while (a<=n)
            {
                cin>>cmd>>tmp;
                if (cmd=='P') printf("%d\n",T.Treap_pred(T.root,tmp,0));
                    else if (cmd=='S') printf("%d\n",T.Treap_succ(T.root,tmp,0));
                        else MessageBox(NULL,"Unknown Command Type\nPlease Input Again.","Error",MB_OK),a--;
                a++;
            }
        }
    #endif
    #ifdef query_delete
        printf("How many nodes would you like to delete?(The number you input must be below%d)\n",n);
        scanf("%d",&m);
        if (m>n) 
        {
            MessageBox(NULL,"The number you inputed just now is too big!!!","Error",MB_OK);
            return -1;
        }
        for (int i=1;i<=m;i++)
        {
            int tmp;
            printf("No.%d : You want to delete the node with value",i);
            scanf("%d",&tmp);
            T.Treap_delete(T.root,tmp);
            T.in_order_print(T.root);
        }
    #endif
    return 0;
}

  

posted @ 2017-01-23 21:50  FFjet  阅读(226)  评论(0编辑  收藏  举报