TREAP平衡树
来源:互联网 发布:win10网络图标灰色 编辑:程序博客网 时间:2024/06/05 20:25
#include<cstdio>#include<stdlib.h>using namespace std;struct node{int x,w,h,r,l;};struct node a[10001];int e;void rl(int v,int u){int t=a[u].h;a[u].r=a[v].l;a[a[v].l].h=u; a[u].h=v;a[v].l=u; a[v].h=t; if(a[t].l==u)a[t].l=v; else a[t].r=v; }void rr(int v,int u){ int t=a[u].h; a[u].l=a[v].r;a[a[v].r].h=u; a[u].h=v;a[v].r=u; a[v].h=t; if(a[t].l==u)a[t].l=v; else a[t].r=v; } void ins(int v,int u){ int p; if(!v)return; if(u>a[v].x){ if(!a[v].r){ a[++e].x=u; a[e].w=rand(); a[e].h=v; a[v].r=e; p=e; while(!a[p].h && a[p].w>a[a[p].h].w){ if(p==a[a[p].h].r) rl(p,a[p].h); else rr(p,a[p].h); } }else ins(a[v].r,u); }else{ if(!a[v].l){ a[++e].x=u; a[e].w=rand(); a[e].h=v; a[v].l=e; p=e; while(!a[p].h && a[p].w>a[a[p].h].w){ if(p==a[a[p].h].r) rl(p,a[p].h); else rr(p,a[p].h); } }else ins(a[v].l,u); } } void out(int s){ if(!s)return; out(a[s].l); printf("%d ",a[s].x); out(a[s].r); } int main(){ int i,j,k,m,n,root=1; scanf("%d",&n); scanf("%d",&k); srand(2141); a[1].x=k;e=1; a[1].w=rand(); for(i=2;i<=n;i++){ scanf("%d",&k); ins(root,k); } out(1); return 0; }
1 0
- TREAP平衡树代码
- Treap平衡树
- Treap平衡树
- Treap平衡树
- Treap平衡树
- TREAP平衡树
- 平衡树Treap模版
- 【平衡树】Treap
- 平衡树Treap
- 平衡树之treap
- 普通平衡树 treap
- 【学习】Treap平衡树
- 数据结构 平衡树treap
- 平衡树之Treap
- Treap-普通平衡树
- 平衡树之treap
- 浅谈 平衡树-treap
- [平衡树模板]Treap
- LeetCode数据库练习题MySql
- C++之pair与make_pair
- uva 10020 Minimal coverage
- ubuntu14.04配置samba服务器
- 关于WebService技术的应用开发
- TREAP平衡树
- UML图——用例图
- LeetCode290 pattern string 解题报告
- codeforces 617E XOR and Favorite Number (莫队)
- 【sql深入】left join、right join & inner join的比较
- 【高斯消元】【图论】[Wc2011][HYSBZ/BZOJ2155]Xor
- Linux下套接字详解(五)----基于fork多进程的TCP套接字(阻塞/同步/并发)
- UML——包图
- 自己动手开发一个 Web 服务器(三)