作者:ik82jht | 来源:互联网 | 2023-10-10 13:16
P3369 【模板】普通平衡树
//avl数组版
#include
using namespace std;
const int maxn=100010;
struct avlnode{
int val;
int size;
int cnt;
int height;
int ls;
int rs;
}avl[maxn];
int root,tot;
int height(int rt)
{
return avl[rt].height;
}
void pushup(int rt)
{
avl[rt].size=avl[avl[rt].ls].size+avl[avl[rt].rs].size+avl[rt].cnt;
}
void LeftRotation(int &rt)
{
int k;
k=avl[rt].rs;
avl[rt].rs=avl[k].ls;
avl[k].ls=rt;
avl[rt].,ls"< preorder(avl[rt].ls);
preorder(avl[rt].rs);
}
}
void inorder(int rt)
{
if (rt!=0)
{
inorder(avl[rt].ls);
for (int i=1;i<=avl[rt].cnt;++i) cout< inorder(avl[rt].rs);
}
}
void insert(int &rt,int v)
{
if (rt==0)
{
rt=Newavl(v);
}
else if (avl[rt].val>v)
{
insert(avl[rt].ls,v);
if (avl[avl[rt].ls].height-avl[avl[rt].rs].height==2)
{
int& k=avl[rt].ls;
if (avl[avl[k].ls].height>avl[avl[k].rs].height)
{
RightRotation(rt);
}
else
{
LeftRotation(k);
RightRotation(rt);
}
}
}
else
{
insert(avl[rt].rs,v);
if (avl[avl[rt].ls].height-avl[avl[rt].rs].height==-2)
{
int& k=avl[rt].rs;
if (avl[avl[k].ls].height {
LeftRotation(rt);
}
else
{
RightRotation(k);
LeftRotation(rt);
}
}
}
pushup(rt);
avl[rt].height=max(avl[avl[rt].ls].height,avl[avl[rt].rs].height)+1;
}
int Maximum(int rt)
{
if (rt==0)
{
return rt;
}
while(avl[rt].rs!=0)
{
rt=avl[rt].rs;
}
return rt;
}
int Minimum(int rt)
{
if (rt==0)
{
return rt;
}
while(avl[rt].ls!=0)
{
rt=avl[rt].ls;
}
return rt;
}
void dele(int &rt,int v)
{
if (rt==0)
{
return ;
}
if (v {
dele(avl[rt].ls,v);
avl[rt].height=max(height(avl[rt].ls),height(avl[rt].rs))+1;
if (height(avl[rt].ls)-height(avl[rt].rs)==-2)
{
int &k=avl[rt].rs;
if (height(avl[k].ls)>height(avl[k].rs))
{
RightRotation(k);
LeftRotation(rt);
}
else
{
LeftRotation(rt);
}
}
}
else if(v>avl[rt].val)
{
dele(avl[rt].rs,v);
avl[rt].height=max(height(avl[rt].ls),height(avl[rt].rs))+1;
if (height(avl[rt].ls)-height(avl[rt].rs)==2)
{
int &k=avl[rt].ls;
if (height(avl[k].rs)>height(avl[k].ls))
{
LeftRotation(k);
RightRotation(rt);
}
else
{
RightRotation(rt);
}
}
}
else
{
if (avl[rt].ls&&avl[rt].rs)
{
if (height(avl[rt].ls)>height(avl[rt].rs))
{
int k=Maximum(avl[rt].ls);
avl[rt].val=avl[k].val;
dele(avl[rt].ls,avl[k].val);
}
else
{
int k=Minimum(avl[rt].rs);
avl[rt].val=avl[k].val;
dele(avl[rt].rs,avl[k].val);
}
}
else
{
rt=avl[rt].ls+avl[rt].rs;
}
}
pushup(rt);
}
int kth(int rt,int k)
{
if (avl[avl[rt].ls].size+avl[rt].cnt==k) return avl[rt].val;
else if(k<=avl[avl[rt].ls].size) return kth(avl[rt].ls,k);
else return kth(avl[rt].rs,k-avl[avl[rt].ls].size-avl[rt].cnt);
}
int Rank(int rt,int val)
{
if (rt==0) return 1;
if (avl[rt].val>=val) return Rank(avl[rt].ls,val);
else return avl[avl[rt].ls].size+avl[rt].cnt+Rank(avl[rt].rs,val);
}
int Pre(int rt,int val)
{
return kth(rt,Rank(rt,val)-1);
}
int suc(int rt,int val)
{
return kth(rt,Rank(rt,val+1));
}
int main()
{
int n;
cin>>n;
for (int i=1;i<=n;++i)
{
int opt,v;
cin>>opt;
if (opt==1)
{
cin>>v;
insert(root,v);
}
else if(opt==2)
{
cin>>v;
dele(root,v);
}
else if(opt==3)
{
cin>>v;
cout< }
else if (opt==4)
{
cin>>v;
cout< }
else if(opt==5)
{
cin>>v;
cout<>v;
cout< }
else if(opt==7)
{
inorder(root);
cout< }
else if(opt==8)
{
preorder(root);
}
}
/*
inorder(root);
cout< preorder(root);
*/
}
/*
in 1 rightrotation
9
1 3
1 4
1 2
1 1
7
8
2 4
7
8
in 2 leftrotation
9
1 4
1 2
1 5
1 7
7
8
2 2
7
8
in 3
9
1 4
1 5
1 7
1 2
7
8
2 2
7
8
in 4
10
1 10
1 5
1 12
1 4
1 6
7
8
2 5
7
8
in 5
10
1 10
1 5
1 12
1 4
1 6
7
8
2 12
7
8
*/