这是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标准模板的更多相关文章

  1. STL标准模板库(简介)

    标准模板库(STL,Standard Template Library)是C++标准库的重要组成部分,包含了诸多在计算机科学领域里所常见的基本数据结构和基本算法,为广大C++程序员提供了一个可扩展的应 ...

  2. treap树模板

    ///treap树模板 typedef struct Node ///节点的结构体 { Node *l,*r; int val,pri; ///节点的值和优先级 int sz; ///节点子树的节点数 ...

  3. 【转】C++标准库和标准模板库

    C++强大的功能来源于其丰富的类库及库函数资源.C++标准库的内容总共在50个标准头文件中定义.在C++开发中,要尽可能地利用标准库完成.这样做的直接好处包括:(1)成本:已经作为标准提供,何苦再花费 ...

  4. STL标准模板库介绍

    1. STL介绍 标准模板库STL是当今每个从事C++编程的人需要掌握的技术,所有很有必要总结下 本文将介绍STL并探讨它的三个主要概念:容器.迭代器.算法. STL的最大特点就是: 数据结构和算法的 ...

  5. BZOJ 3224 TYVJ 1728 普通平衡树 [Treap树模板]

    3224: Tyvj 1728 普通平衡树 Time Limit: 10 Sec  Memory Limit: 128 MB Submit: 7390  Solved: 3122 [Submit][S ...

  6. 【c++】标准模板库STL入门简介与常见用法

    一.STL简介 1.什么是STL STL(Standard Template Library)标准模板库,主要由容器.迭代器.算法.函数对象.内存分配器和适配器六大部分组成.STL已是标准C++的一部 ...

  7. C++之路起航——标准模板库(vector)

    vector(动态数组或向量):动态分配内存空间的线性储存结构. 需要包括头文件<vector> 定义: vector<数据类型> 变量名: Eg: vector<int ...

  8. C++——string类和标准模板库

    一.string类 1.构造函数 string实际上是basic_string<char>的一个typedef,同时省略了与内存管理相关的参数.size_type是一个依赖于实现的整型,是 ...

  9. STL 简介,标准模板库

    这篇文章是关于C++语言的一个新的扩展--标准模板库的(Standard Template Library),也叫STL.  当我第一次打算写一篇关于STL的文章的时候,我不得不承认我当时低估了这个话 ...

随机推荐

  1. 取代iframe,实现页面中引入别的页面

    <li> <a href="#addChannel">添加通道</a> </li> window.onload = rightCha ...

  2. 【js】了解前端缓存,收获不止于此!

    了解前端缓存,收获不止于此! 这次我们来讲一下关于前端缓存的问题.感谢赵欢同学提供doc素材. 首先,开局我画了一张图,你会对文章有一个大局了解. 今天讲的是前端缓存. 前端缓存有3大种:如图,分为H ...

  3. Got error -1 from storage engine

    有个小朋友修复从库,但是start slave 后,报错信息如下 Could not execute Write_rows event on table hsfdssdb.mf_textannounc ...

  4. Windows 下安装 Memcached

    Windows 下安装 Memcached 官网上并未提供 Memcached 的 Windows 平台安装包,我们可以使用以下链接来下载,你需要根据自己的系统平台及需要的版本号点击对应的链接下载即可 ...

  5. MySQL表结构更新规范

    以下以新增字段为例,修改.删除.新增表的操作类似: 步骤: 1.PDM(中文名称为产品数据管理(Product Data Management))增加字段,并注明新增字段的注释 2.数据库编写sql语 ...

  6. 16: mint-ui移动端

    1.1 mint-ui安装与介绍  官网:http://mint-ui.github.io/docs/#/zh-cn2/loadmore 1.安装与引用 // 安装Vue 2.0 npm instal ...

  7. word模板导出的几种方式:第二种:C#通过模板导出Word(文字,表格,图片) 占位符替换

    原文出处:https://www.cnblogs.com/ilefei/p/3508463.html 一:模板的创建   (注意文件后缀只能是.docx或.doct) 在需要位置 插入-文档部件-域, ...

  8. Maven的基本概念

    一.Maven的基本概念' Maven(翻译为"专家","内行")是跨平台的项目管理工具.主要服务于基于Java平台的项目构建,依赖管理和项目信息管理. 1.1 ...

  9. ubuntu学习笔记

    Linux操作系统 locale –a查看支持语言 ls查看目录 ls .l / 查看根目录 apt-get –h 安装软件看帮助信息 sudo apt-get inatall packge 安装包 ...

  10. 论文笔记:AdaScale: Towards real-time video object detection using adaptive scalingAdaScale

    AdaScale: Towards real-time video object detection using adaptive scaling 2019-02-18 16:14:17 Paper: ...