又复习了一遍splay板子

void right_rotate(int x)
{
int y=fa[x];
int z=fa[y];

fa[rightson[x]]=y;
if (rightson[x])
  {
  	leftson[y]=rightson[x];
  }
fa[x]=z;

if (z){
if (leftson[z]==y) leftson[z]=x;
else rightson[z]=x;
}
rightson[x]=y;
fa[y]=x;

}

void left_rotate(int x){
int y=fa[x];
int z=fa[y];
fa[leftson[x]]=x;
if (leftson[x]){
rightson[y]=leftson[x];
}
fa[x]=z;
if (z)
{
if (leftson[z]==y) leftson[z]=x;
else rightson[z]=x;
}
fa[y]=x;
leftson[x]=y;
}

void splay(int x,int ancestry)
{

while (ancestry!=fa[x]){
int y=fa[x];
int z=fa[y];
if (z==ancestry)
{
if (leftson[y]==x) right_rotate(x);
else if (rightson[y]==x) left_rotate(x);
}
else {
if (leftson[z]==y&&rightson[y]==x)
left_rotate(x),right_rotate(x);
else if (rightson[z]==y&&leftson[y]==x)
right_rotate(x),left_rotate(x);
else if (leftson[z]==y&&leftson[y]==x)
{
right_rotate(x);right_rotate(x);
}
else {left_rotate(x);left_rotate(x);}
}

}
if (ancestry==0) root=x;
}

猜你喜欢

转载自blog.csdn.net/qq_36172410/article/details/89500744