首页 > 代码库 > Treap标准模板
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=http://www.mamicode.com/key;"第%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; }
Treap标准模板
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。