自己总结的一个平衡树Treap模版,自己测试了一下,感觉没什么错误,但总感觉有bug,请大家指教一下,下面是自己测试时写的测试用的代码,请无视
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <iostream>
#include <algorithm>
using namespace std;
typedef long long ll;
const int inf=0x3f3f3f3f;
const int maxn=100010;
struct Treap_Node{
Treap_Node *left,*right;
int value,fix;//fix为修正值,是随机产生的,保证二叉树不会退化的重要环节
};
Treap_Node *root;
void Treap_Print(Treap_Node *P){//从小到大输出
if(P){
Treap_Print(P->left);
printf("%d\n",P->value);
Treap_Print(P->right);
}
}
void Treap_Left_Rotate(Treap_Node *&a){//左旋之后仍不改变二叉树性质
Treap_Node *b=a->right;
a->right=b->left;
b->left=a;
a=b;
}
void Treap_Right_Rotate(Treap_Node *&a){//右旋之后仍不改变二叉树性质
Treap_Node *b=a->left;
a->left=b->right;
b->right=a;
a=b;
}
int Treap_Find(Treap_Node *P,int value){//查找有没有value这个数
if(!P) return 0;
if(P->value==value) return 1;
else if(value<P->value) return Treap_Find(P->left,value);
else return Treap_Find(P->right,value);
}
void Treap_Insert(Treap_Node *&P,int value){//插入一个数
if(!P){
P=new Treap_Node;
P->left=P->right=NULL;//左右儿子均为空
P->value=value;
P->fix=rand();
}
else if(value<=P->value){
Treap_Insert(P->left,value);
if(P->left->fix<P->fix)
Treap_Right_Rotate(P);
}
else{
Treap_Insert(P->right,value);
if(P->right->fix<P->fix)
Treap_Left_Rotate(P);
}
}
void Treap_Delete(Treap_Node *&P,int value){//删除一个数
if(value==P->value){
if(!P->right||!P->left){//只有一个节点直接删除
Treap_Node *t=P;
if(!P->right) P=P->left;
else P=P->right;
delete t;
}else{
if(P->left->fix<P->right->fix){//左儿子修正值较小,右旋
Treap_Right_Rotate(P);
Treap_Delete(P->right,value);
}else{//右儿子修正值较小,左旋
Treap_Left_Rotate(P);
Treap_Delete(P->left,value);
}
}
}else if(value<P->value) Treap_Delete(P->left,value);
else Treap_Delete(P->right,value);
}
int main(){
int m,a;
char ch;
while(scanf("%d",&m)!=-1){
root=NULL;
Treap_Insert(root,2);
Treap_Insert(root,3);
Treap_Insert(root,1);
Treap_Print(root);
while(m--){
getchar();
scanf("%c",&ch);
if(ch=='F'){
scanf("%d",&a);
int t=Treap_Find(root,a);
if(t) printf("Found\n");
else printf("Not Found\n");
}else if(ch=='I'){
scanf("%d",&a);
Treap_Insert(root,a);
}else if(ch=='P'){
Treap_Print(root);
}else if(ch=='D'){
scanf("%d",&a);
Treap_Delete(root,a);
}
}
}
return 0;
}
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <iostream>
#include <algorithm>
using namespace std;
typedef long long ll;
const int inf=0x3f3f3f3f;
const int maxn=100010;
struct treap_node{
treap_node *left,*right;
int val,fix,wgt,size;
treap_node(int val): val(val) {left=right=NULL; size=wgt=1; fix=rand(); }
int lsize(){
if (left)
return left->size;
else
return 0;
}
int rsize(){
if (right)
return right->size;
else
return 0;
}
void Maintain(){
size=wgt;
size+=lsize()+rsize();
}
};
treap_node *root;
int n,minn,size,i;
void tlr(treap_node *&a){
treap_node *b=a->right;
a->right=b->left;
b->left=a;
a->Maintain();
b->Maintain();
a=b;
}
void trr(treap_node *&a){
treap_node *b=a->left;
a->left=b->right;
b->right=a;
a->Maintain();
b->Maintain();
a=b;
}
void treap_insert(treap_node *&p,int value){
if (!p)
p=new treap_node(value);
else{
if (value==p->val)
p->wgt++;
if (value<p->val){
treap_insert(p->left,value);
if (p->left->fix<p->fix)
trr(p);
}
if (value>p->val){
treap_insert(p->right,value);
if (p->right->fix<p->fix)
tlr(p);
}
}
p->Maintain();
}
int Treap_kth(treap_node *&p,int k){
if (k<p->lsize()+1)
return Treap_kth(p->left,k);
else
if (k>p->lsize()+p->wgt)
return Treap_kth(p->right,k-p->lsize()-p->wgt);
else
return p->val;
}
void del(treap_node *&p,int d){
if (p->val==d){
if (p->wgt==1){
if (!p->left||!p->right){
if (!p->left)
p=p->right;
else
p=p->left;
}
else{
if (p->left->fix<p->right->fix){
trr(p);
del(p->right,d);
}
else{
tlr(p);
del(p->left,d);
}
}
}
else
p->wgt--;
}
else{
if (d<p->val)
del(p->left,d);
if (d>p->val)
del(p->right,d);
}
if (p!=NULL) p->Maintain();
}
int id[10000010];
int main(){
int a,b,d,c,sum=0;
while(scanf("%d",&a)!=-1){
if(a==0) break;
if(a==1){
scanf("%d%d",&b,&c);
id[c]=b;sum++;
treap_insert(root,c);
}
if(a==2){
if(sum==0) printf("0\n");
else{
int t=Treap_kth(root,sum);
printf("%d\n",id[t]);
sum--;
del(root,t);
}
}
if(a==3){
if(sum==0) printf("0\n");
else{
int t=Treap_kth(root,1);
printf("%d\n",id[t]);
sum--;
del(root,t);
}
}
// cout<<"rrrrrr"<<endl;
// tree.Treap_Print(tree.root);
// cout<<"rrrrrr"<<endl;
}
return 0;
}