静态数组Treap(用随机数维护的二叉搜索树)
来源:互联网 发布:淘口令算淘宝客吗 编辑:程序博客网 时间:2024/05/02 04:16
#include<stdio.h> #include<stdlib.h> struct node{ int x,w,h,r,l; }; struct node a[100001]; int e,root=1; void rotate_left(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 rotate_right(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 insert(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) rotate_left(p,a[p].h); else rotate_right(p,a[p].h); } if(a[p].h==0)root=p; }else insert(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) rotate_left(p,a[p].h); else rotate_right(p,a[p].h); } if(a[p].h==0)root=p; }else insert(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; scanf("%d",&n); scanf("%d",&k); srand(3221); a[1].x=k;e=1; a[1].w=rand(); for(i=2;i<=n;i++){ scanf("%d",&k); insert(root,k); } out(root); return 0; }
0 0
- 静态数组Treap(用随机数维护的二叉搜索树)
- 静态数组实现的二叉搜索树
- 随机化的二叉搜索树总结(treap,随机输入)
- Treap--简单的平衡二叉搜索树
- 随机二叉搜索树 Treap
- 九度 题目1009:二叉搜索树 (静态数组构建二叉树\二叉树的数组实现)
- 动态平衡二叉搜索树的简易实现,Treap 树
- 静态数组实现TREAP
- JD 1009:二叉搜索树(静态数组建树)
- 二叉搜索树(篇1)判断数组是不是二叉搜索树后序遍历的结果
- 随机自平衡二叉搜索树Treap模版
- 二叉搜索树的数组实现
- 二叉搜索树-数组的实现方式
- treap 排序二叉树的模板
- HDU 3791 二叉搜索树(用数组保存树)
- 二叉搜索树(树状数组)
- 数组建立二叉搜索树
- BST 二叉搜索树 (动态建树与静态建树)
- 语义分析的一些方法(二)
- leetcode 338 Counting Bits C++
- OpenCV2.4.13环境配置问题
- javascript基础题目
- Ubuntu16.04启动器位置切换
- 静态数组Treap(用随机数维护的二叉搜索树)
- 语义分析的一些方法(三)
- POJ 2189 枚举
- just try
- LA2678 Subsequence 时间优化
- 8大排序之-------堆排序与时间复杂度
- 【SQL Performance】历史SQL监控(Historical SQL Monitoring ) 功能(12c)
- 字符串分隔
- Spark Streaming的Exactly-One的事务处理