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;
}
Treap标准模板的更多相关文章
- STL标准模板库(简介)
标准模板库(STL,Standard Template Library)是C++标准库的重要组成部分,包含了诸多在计算机科学领域里所常见的基本数据结构和基本算法,为广大C++程序员提供了一个可扩展的应 ...
- treap树模板
///treap树模板 typedef struct Node ///节点的结构体 { Node *l,*r; int val,pri; ///节点的值和优先级 int sz; ///节点子树的节点数 ...
- 【转】C++标准库和标准模板库
C++强大的功能来源于其丰富的类库及库函数资源.C++标准库的内容总共在50个标准头文件中定义.在C++开发中,要尽可能地利用标准库完成.这样做的直接好处包括:(1)成本:已经作为标准提供,何苦再花费 ...
- STL标准模板库介绍
1. STL介绍 标准模板库STL是当今每个从事C++编程的人需要掌握的技术,所有很有必要总结下 本文将介绍STL并探讨它的三个主要概念:容器.迭代器.算法. STL的最大特点就是: 数据结构和算法的 ...
- BZOJ 3224 TYVJ 1728 普通平衡树 [Treap树模板]
3224: Tyvj 1728 普通平衡树 Time Limit: 10 Sec Memory Limit: 128 MB Submit: 7390 Solved: 3122 [Submit][S ...
- 【c++】标准模板库STL入门简介与常见用法
一.STL简介 1.什么是STL STL(Standard Template Library)标准模板库,主要由容器.迭代器.算法.函数对象.内存分配器和适配器六大部分组成.STL已是标准C++的一部 ...
- C++之路起航——标准模板库(vector)
vector(动态数组或向量):动态分配内存空间的线性储存结构. 需要包括头文件<vector> 定义: vector<数据类型> 变量名: Eg: vector<int ...
- C++——string类和标准模板库
一.string类 1.构造函数 string实际上是basic_string<char>的一个typedef,同时省略了与内存管理相关的参数.size_type是一个依赖于实现的整型,是 ...
- STL 简介,标准模板库
这篇文章是关于C++语言的一个新的扩展--标准模板库的(Standard Template Library),也叫STL. 当我第一次打算写一篇关于STL的文章的时候,我不得不承认我当时低估了这个话 ...
随机推荐
- 选择排序java实现
package text.algorithm; /** * 选择排序 * O(n^2);空间复杂度O(1); */public class SelectionSort { public static ...
- zigbee 安全通信加密链接密钥
---恢复内容开始--- #define KEY_TYPE_TC_MASTER 0 // Trust Center Master Key信任中心主密钥#define KEY_TYPE_ ...
- 【记录】Linux环境安装mysql8.0
话说mysql8.0版本比5.7版本要快2倍以上,这么看宣传怎么能不装8.0呢,但是新版本和旧版本有不少不同导致若使用以前的一些安装方法会导致安到一半就由于各种找不到文件卡住. 尝试了不少次,只有使用 ...
- bui前端框架+yii整理
这个是做bui前端样式整合的时候记录的. 首先当然是要下载一个yii的源码,搭建起来. 第一步将bui的样式迁移到yii的样式目录中去 这里我在样式外面加了一个bui的文件夹,表示这个文件夹中存放的是 ...
- LeetCode Weekly Contest 121
上周因为感冒没有刷题,两个星期没有刷题,没手感了,思维也没有那么活跃了,只刷了一道,下个星期努力. 984. String Without AAA or BBB Given two integers ...
- linux常用英文单词记录
1.skip 跳过忽略 2.next 下一步3.hostname 主机名4.password 密码5.complete 完成6.network 网络7.conf config configuratio ...
- 王之泰《面向对象程序设计(java)》课程学习总结
第一部分:理论知识学习部分 总复习纲要 1. Java语言特点与开发环境配置(第1章.第2章) 2. Java基本程序结构(第3章) 3. Java面向对象程序结构(第4章.第5章.第6章) 4. 类 ...
- ArrayList迭代器源码分析
集合的遍历 Java集合框架中容器有很多种类,如下图中: 对于有索引的List集合可以通过for循环遍历集合: List<String> list = new ArrayList<& ...
- jQuery (含义 及 优缺点)
jQuery是一套开源的JavaScript函数库,它主要是简化了对DOM的操作.兼容CSS3,兼容各大主流浏览器. 二.jQuery的特点: 1:轻量级的JS函数库 jQuery的核心js文件几十K ...
- Go语言库之strconv包(转载自--http://blog.csdn.net/alvine008/article/details/51283189)
golang strconv.ParseInt 是将字符串转换为数字的函数 func ParseInt(s string, base int, bitSize int) (i int64, err e ...