Treap(模板)
2024-08-31 00:56:33
人生第一次平衡树,Treap板子
#include<iostream>
#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<ctime> using namespace std; struct Treenode
{
int size;
int fix;
int weight;
int key;
Treenode *left;
Treenode *right;
}; class Treaptree
{
private:
Treenode *null;
public:
Treenode *root;
Treaptree()
{
null=new Treenode;
null->key=;
null->weight=;
null->size=;
null->left=null;
null->right=null;
null->fix=;
root=null;
} void Treap_left(Treenode *now)
{
Treenode *tmp=now->right;
now->right=tmp->left;
tmp->left=now;
tmp->size=now->size;
now->size=now->left->size+now->right->size+now->weight;
now=tmp;
} void Treap_right(Treenode *now)
{
Treenode *tmp=now->left;
now->left=tmp->right;
tmp->right=now;
tmp->size=now->size;
now->size=now->left->size+now->right->size+now->weight;
now=tmp;
} void insert(Treenode *&now,int key)
{
if(now==null)
{
now=new Treenode;
now->key=key;
now->size=;
now->weight=;
now->fix=rand();
now->left=null;
now->right=null;
}
else if(key==now->key)
{
now->weight++;
}
else if(key<now->key)
{
insert(now->left,key);
if(now->left->fix<now->fix) Treap_right(now);
}
else if(key>now->key)
{
insert(now->right,key);
if(now->left->fix<now->fix) Treap_left(now);
}
now->size=now->left->size+now->right->size+now->weight;
} bool find(Treenode *now,int key)
{
if(now==null)return false;
if(key<now->key)return find(now->left,key);
else if(key>now->key)return find(now->right,key);
else return true;
} void Delete(Treenode *&now,int key)
{
if(now==null)return;
if(key<now->key)Delete(now->left,key);
else if(key>now->key)Delete(now->right,key);
else
{
if(now->weight>)now->weight--;
else
{
if(now->left==null&&now->right==null)
{
delete now;
now=null;
}
else
{
if(now->left->fix<now->right->fix)Treap_left(now);
else Treap_right(now);
Delete(now,key);
}
now->size=now->left->size+now->right->size+now->weight;
}
}
}
}; int main()
{ return ;
}
心若向阳,无言悲伤
最新文章
- Winform的";透明";
- 支持Android iOS,firefox(其它未测)的图片上传客户端预览、缩放、裁切。
- Android VideoView播放视频
- Puppet
- 用Objective-C的foundation框架解决表达式求值问题
- poj 1860 (Bellman_Ford判断正环)
- Android AndroidManifest 清单文件以及权限详解!【转】
- 构建安全的Xml Web Service系列之SSL篇
- Javascript基础知识小测试(一)
- spring定时器的使用
- Go - concurrency
- NPOI插入图片到excel指定单元格
- HTTP Basic和Digest认证介绍与计算
- MIME 参考手册
- [例子] nginx负载均衡搭建及测试
- Tensoflow API笔记(N) 设备指定
- Go的json解析:Marshal与Unmarshal
- shell 获取随机字符串
- 【轻松前端之旅】元素,标记,属性,<;html>;标签
- ERROR tool.ImportTool: Encountered IOException running import job: java.io.IOException: Cannot run program ";hive";: error=2, No such file or directory