跳到正文

个人记录

平衡树

popo0815约 6 分钟
原文
文章信息与版本

文章信息

收录
2025年12月12日
更新
2025年12月12日
篇幅
约 6 分钟

版本

  1. v1当前

平衡树

  • 基于二叉搜索树的 BST BST 性质,但极端情况即一条链的情况会被卡成 sbsb
  • 故产生了平衡树维护,顾名思义,即树的左右子树大小平衡,期望树高 log⁡n\log n
  • 回顾有 TreapTreap , SplaySplay ,无旋树,红黑树,替罪羊树 ............

Treap

  • 既满足 BST BST 的性质有满足堆 HeapHeap 的性质
  • 给节点随机权值,在堆的性质下,能达到期望高度 log⁡n\log n
  • 笛卡尔树就是一种 TreapTreap

Splay

  • 与Treap一样有左旋,右旋操作,但其精髓是双旋,如图

  • 即一次双旋能使树两边趋于平衡,则将一个节点旋到根,可以使该路径上的节点深度减小一半,其他点至多增加 22

  • 因此只要对于访问频率高的点进行双旋到根,那么每次修改查询的点即为访问频率高的点

  • 感性理解

C++
namespace Splay
{
	int val[N],pos[N],fa[N],son[N][2],siz[N],root;

	inline bool which(int x)
	{
		return son[fa[x]][1]==x;
	}
	inline void update(int x)
	{
		siz[x]=siz[son[x][0]]+siz[son[x][1]]+1;
	}
	void build(int l,int r,int f)
	{
		int x=l+r>>1;
		val[x]=a[x],pos[a[x]]=x;
		if(f)
		{
			if(f>r) son[f][0]=x;
			else son[f][1]=x;
			fa[x]=f;
		}
		if(l<=x-1) build(l,x-1,x);
		if(x+1<=r) build(x+1,r,x);
		update(x);
	}
	inline void rotate(int x)
	{
		int d=which(x),f=fa[x];
		int ff=fa[f],y=son[x][d^1];
		fa[x]=ff,fa[y]=f,fa[f]=x;
		son[ff][son[ff][1]==f]=x,son[x][d^1]=f,son[f][d]=y;
		update(f),update(x);
	}
	inline void splay(int x,int p)
	{
		for(int f;fa[x]!=p;rotate(x))
			if(fa[f=fa[x]]!=p)
				rotate(which(x)^which(f)?x:f);
		if(!p) root=x;
	}
	void print(int x)
	{
		if(!x) return;
		print(son[x][0]);
		printf("%d ",val[x]);
		print(son[x][1]);
	}
}using namespace Splay;

C++
inline void ins(int x)
{
	if(!root)//根为空
	{
		tot++;
		son[tot][0]=son[tot][1]=f[tot]=0;
		root=tot;
		siz[tot]=cnt[tot]++;
		val[tot]=x;
		return;
	}
	int now=root,fa;
	while(1)
	{
		if(x==val[now])//找到了
		{
			cnt[now]++;
			update(now);
			update(fa);
			splay(now);
			break;
		}
		fa=now;
		now=son[now][val[now]<x];
		if(!now)
		{
			tot++;
			son[tot][0]=son[tot][1]=0;
			siz[tot]=cnt[tot]=1;
			f[tot]=fa,val[tot]=x;
			son[fa][val[fa]<x]=tot;

			update(fa);
			splay(tot);
			break;
		}
	}
}

inline int find_num(int x)
{
	int now=root;
	while(1)
	{
		if(son[now][0]&&x<=siz[son[now][0]])
			now=son[now][0];
		else
		{
			int temp=siz[son[now][0]]+cnt[now];
//			int temp=(son[now][0]?siz[son[now][0]]:0)+cnt[now];
			if(x<=temp) return val[now];
			x-=temp,now=son[now][1];
		}
	}
}
inline int find_rank(int x)
{
	int now=root,ans=0;
	while(1)
	{
		if(x<val[now])
		{
			if(!son[now][0]) return ans+1;
			now=son[now][0];
		}

		else
		{
			ans+=siz[son[now][0]];
//			ans+=(son[now][0]?siz[son[now][0]]:0);
			if(x==val[now]) return splay(now),ans+1;
			ans+=cnt[now],now=son[now][1];
		}
	}
}
inline int find_pre()
{
	int now=son[root][0];
	while(son[now][1]) now=son[now][1];
	return now;
}
inline int find_suf()
{
	int now=son[root][1];
	while(son[now][0]) now=son[now][0];
	return now;
}
inline void del(int x)
{
	find_rank(x);
	if(cnt[root]>1)
	{
		cnt[root]--;
		update(root);
		return ;
	}
	if(!son[root][0]&&!son[root][1]) return clear(root),root=0;

	if(!son[root][0])
	{
		int old=root;
		root=son[root][1],f[root]=0;
		clear(old);
		return;
	}
	else if(!son[root][1])
	{
		int old=root;
		root=son[root][0],f[root]=0;
		clear(old);
		return;
	}
	int left=find_pre(),old=root;
	splay(left);//一定异构
	son[root][1]=son[old][1],f[son[old][1]]=root;
	clear(old),update(root);
}
原文