平衡树
前言
在平衡树之前,我们已经学习过 BST(二叉查找树)。BST 的缺点在于对于可以构造的数据,它不能很好地保持一棵二叉树的形态,并有几率退化成一条链,时间复杂度从平均
初始化
treap的初始化方式与BST类似,都是事先插入一个权值为
void newnode(int val){
tr[++cnttr].val=val;
return;
}
void inittreap(){
newnode(-0x7f7f7f7f);
newnode(0x7f7f7f7f);
trroot=1;tr[1].r=2;
return;
}
旋转
treap 中使用“旋转”改变形态并保持BST性质,它又分为“左旋”和“右旋”。旋转的代码如下:
void trzig(int &now){
int lc=tr[now].l;
tr[now].l=tr[lc].r,tr[lc].r=now,now=lc;
return;
}
void trzag(int &now){
int rc=tr[now].r;
tr[now].r=tr[rc].l,tr[rc].l=now,now=rc;
return;
}
旋转的示例如下图。
随机化
现在我们思考怎样才能通过旋转操作让 treap 平衡?换句话说,怎样的旋转操作是值得执行的?可以发现,在随机数据下普通的 BST 就是趋于平衡的。所以我们只需要给每个节点赋予随机的一个值,把随机建立在堆性质上。这样,treap 对于查找、插入、求前驱和后继、删除节点的平均时间复杂度都是
#include<iostream>
#include<cstdlib>
#include<cstring>
#include<ctime>
#define N 100005
using namespace std;
int n;
int cnttr,trroot;
struct treap{
int l,r,val,random,cnt,subtrs;
//l,r:左右子节点的下标
//val:节点上的数值,用于查找
//random:一个随机权值,可以使结构平衡
//cnt:同样数值的节点个数(存为一个点)
//subtrs:以本节点为根的子树中所有节点的 .cnt 之和
treap(){}
}tr[N];
void newnode(int val){
tr[++cnttr].val=val;
tr[cnttr].random=rand();
tr[cnttr].cnt=tr[cnttr].subtrs=1;
return;
}
void upsubtrs(int now){
tr[now].subtrs=tr[tr[now].l].subtrs+tr[tr[now].r].subtrs+tr[now].cnt;
return;
}
void inittreap(){
newnode(-0x7f7f7f7f),newnode(0x7f7f7f7f);
trroot=1;tr[1].r=2;
upsubtrs(trroot);
return;
}
//初始化treap
void trzig(int &now){
int lc=tr[now].l;
tr[now].l=tr[lc].r,tr[lc].r=now,now=lc;
upsubtrs(tr[now].r),upsubtrs(now);
return;
}
//左旋
void trzag(int &now){
int rc=tr[now].r;
tr[now].r=tr[rc].l,tr[rc].l=now,now=rc;
upsubtrs(tr[now].l),upsubtrs(now);
return;
}
//右旋
void trinsert(int &now,int val){
if(now==0){
newnode(val);
now=cnttr;
return;
}
if(tr[now].val==val){
tr[now].cnt++;upsubtrs(now);
return;
}
if(val<tr[now].val){
trinsert(tr[now].l,val);
if(tr[now].random<tr[tr[now].l].random)trzig(now);
}
else{
trinsert(tr[now].r,val);
if(tr[now].random<tr[tr[now].r].random)trzag(now);
}
upsubtrs(now);
return;
}
//走到对应节点,或发现无对应节点并新增节点
void trremove(int &now,int val){
if(now==0)return;
if(tr[now].val==val){
if(tr[now].cnt>1)
tr[now].cnt--,upsubtrs(now);
else if(tr[now].l||tr[now].r){
if(tr[now].r==0||tr[tr[now].l].random>tr[tr[now].r].random)
trzig(now),trremove(tr[now].r,val);
else trzag(now),trremove(tr[now].l,val);
upsubtrs(now);
}
else now=0;
return;
}
val<tr[now].val?trremove(tr[now].l,val):trremove(tr[now].r,val);
upsubtrs(now);
return;
}
//旋转直到成为叶子节点,然后删除
//上:维护 下:查询
int trpred(int now,int val,int best){
if(!tr[now].val)return best;
if(tr[now].val<val)return trpred(tr[now].r,val,tr[now].val);
else return trpred(tr[now].l,val,best);
}
int trsucc(int now,int val,int best){
if(!tr[now].val)return best;
if(tr[now].val>val)return trsucc(tr[now].l,val,tr[now].val);
else return trsucc(tr[now].r,val,best);
}
int getrank(int now,int val){
if(now==0)return 1;
if(val==tr[now].val)return tr[tr[now].l].subtrs+1;
if(val<tr[now].val)return getrank(tr[now].l,val);
return getrank(tr[now].r,val)+tr[tr[now].l].subtrs+tr[now].cnt;
}
int getval(int now,int rank){
if(now==0)return 0x7f7f7f7f;
if(tr[tr[now].l].subtrs>=rank)return getval(tr[now].l,rank);
if(tr[tr[now].l].subtrs+tr[now].cnt>=rank)return tr[now].val;
return getval(tr[now].r,rank-tr[tr[now].l].subtrs-tr[now].cnt);
}
int main(){
inittreap();
scanf("%d",&n);
while(n--){
int op,x;scanf("%d%d",&op,&x);
if(op==1)trinsert(trroot,x);
if(op==2)trremove(trroot,x);
if(op==3)printf("%d\n",getrank(trroot,x)-1);
if(op==4)printf("%d\n",getval(trroot,x+1));
if(op==5)printf("%d\n",trpred(trroot,x,0));
if(op==6)printf("%d\n",trsucc(trroot,x,0));
}
return 0;
}
无旋treap
除了基于旋转的 treap,还有基于分裂和合并的无旋 treap。我们介绍一种基础的无旋treap:fhq-treap。我们以 P3391 的实现为例:
合并
对于合并,对节点的随机权值进行比较,然后合并左(右)子树,注意下传标记。
int _Merge(int __x,int __y){
if(!__x||!__y)return __x+__y;
if(_treap[__x].random>_treap[__y].random){
_DownLazyTag(__x);
_treap[__x].r=_Merge(_treap[__x].r,__y);
_UpdateSize(__x);
return __x;
} else {
_DownLazyTag(__y);
_treap[__y].l=_Merge(__x,_treap[__y].l);
_UpdateSize(__y);
return __y;
}
return 0;
}
分裂
分裂有两种,一种按大小,一种按权值,两者有很高的相似度。
void _SplitByVal(int __now,int __val,int &__x,int &__y){
if(!__now){__x=__y=0;return;}
if(_treap[__now].val<=__val){
__x=__now;
_SplitByVal(_treap[__now].r,__val,_treap[__now].r,__y);
} else {
__y=__now;
_SplitByVal(_treap[__now].l,__val,__x,_treap[__now].l);
}
_UpdateSize(__now);
return;
}
void _SplitBySize(int __now,int __size,int &__x,int &__y){
if(!__now){__x=__y=0;return;}
_DownLazyTag(__now);
if(_treap[_treap[__now].l].size+1<=__size){
__x=__now;
_SplitBySize(_treap[__now].r,__size-_treap[_treap[__now].l].size-1,_treap[__now].r,__y);
} else {
__y=__now;
_SplitBySize(_treap[__now].l,__size,__x,_treap[__now].l);
}
_UpdateSize(__now);
return;
}
插入和删除
插入和删除很好理解,就是把要插入或删除的位置与其他位置分裂开来(成为三棵树),执行后再合并回去。
void _InsertVal(_Tp __val){
int __x,__y;
_SplitByVal(_trt,__val-1,__x,__y);
_trt=_Merge(_Merge(__x,_AddNewNode(__val)),__y);
return;
}
void _RemoveVal(_Tp __val){
int __x,__y,__z;
_SplitByVal(_trt,__val,__x,__z);
_SplitByVal(__x,__val-1,__x,__y);
if(__y){
__stac.push(__y);
__y=_Merge(_treap[__y].l,_treap[__y].r);
}
_trt=_Merge(_Merge(__x,__y),__z);
return;
}
翻转
对于翻转操作,只需要加上一个异或标记即可。
这是一般平衡树所做不到的。
void _Reverse(int __l,int __r){
int __x,__y,__z;
_SplitBySize(_trt,__l-1,__x,__y);
_SplitBySize(__y,__r-__l+1,__y,__z);
_treap[__y].lazy=_treap[__y].lazy^1;
_trt=_Merge(_Merge(__x,__y),__z);
return;
}
输出时只需要对 treap 中序遍历即可。完整代码
其它题目:
P1533 可怜的狗狗(一般平衡树)
P2343 宝石管理系统(一般平衡树)
P2073 送花(文艺平衡树)
P3380 【模板】二逼平衡树(树套树)