【数据结构】splay 树 学习笔记
前言
解锁新科技:
可以进一步解锁的科技:
-
{\color{red}\text{FHQ Treap}} -
{\color{red}\text{Link-Cut-Tree}} -
P6136 【模板】普通平衡树(数据加强版)
还是拿这个题当板子比较好。
如何搓一棵 Splay [1] [2]:
step 1:
class Splay{
};
step 2:
一棵 Splay 树由以下几部分构成:
int root,指向根。int tot,结点个数。Node,表示结点集。
一个结点 Node 由以下几部分构成:
fach[2]valcntsiz- 各种
tag。
为了方便,先用 C 风格写,最后可以改成 oop 的。
int fa[_], son[_][2], siz[_], cnt[_], val[_];
int root, tot;
step 3:
几个重要的函数:
void maintain(int x);int get(int x);void clear(int x);
void maintain(int x){
siz[x] = siz[son[x][0]] + siz[son[x][1]] + cnt[x];
}
int get(int x){
return son[fa[x]][1] == x;
}
void clear(int x){
fa[x] = son[x][0] = son[x][1] = siz[x] = cnt[x] = val[x] = 0;
}
step 4:
旋转某个结点至父节点,void rotate(int x);
void rotate(int x){
int f = fa[x], g = fa[f];
int chk = get(x);
son[f][chk] = son[x][chk ^ 1];
if(son[x][chk^1])
// 注意这里,一定要判断子节点非空,否则可能修改 0 号节点
fa[son[x][chk ^ 1]] = f;
son[x][chk ^ 1] = f;
fa[f] = x, fa[x] = g;
if(g)
son[g][f == son[g][1]] = x;
maintain(f), maintain(x);
// 先 f 再 x
}
step 5:
splay 操作,将 x 强制旋转至根。
一个 splay 操作为对 x 进行一系列 splay 步骤。
zig/zag操作,在当x.fa为根时进行。zig-zig/zag-zag操作,在当x,x.fa,x.fa.fa在同一条直线上时进行,先旋转x.fa,再旋转x。zig-zag/zag-zig操作,当不共线时进行,旋转两次x。
代码很短,但是是 Splay 树的核心,只有每次操作后都 Splay 一次才能保证复杂度正确。
void splay(int x){
for (int f = fa[x]; (f = fa[x]); rotate(x))
if(fa[f])
rotate(get(x) == get(f) ? f : x);
root = x;
}
具体证明可以去 OI-wiki
step 7:
insert 操作,插入元素。
- 如果树为空,插入根,退出。
- 如果当前节点权值等于
val,直接插入,splay到根。 - 否则按照
bst的性质向下找,发现空节点插入。
注意变量名代表的含义,不要搞混了(
void insert(int x){
if(!root){
root = ++tot;
siz[tot] = cnt[tot] = 1;
val[tot] = x;
return;
}
int k = root, f = 0;
for (;;){
if(x==val[k]){
cnt[k]++;
maintain(k), maintain(f);
splay(k);
return;
}
f = k, k = son[k][val[k] < x];
if(!k){
tot++;
fa[tot] = f;
siz[tot] = cnt[tot] = 1;
son[f][val[f] < x] = tot;
val[tot] = x;
maintain(f);
splay(tot);
return;
}
}
}
step 8:
查找 x 的 rank。
先插入 x,再 rank,最后删除。
若保证 x 一定在集合中可直接 rank。
int rnk(int vl){
int x = root, ans = 0;
for (;;){
if(vl<val[x])
x = son[x][0];
else{
ans += siz[son[x][0]];
if(vl==val[x]){
splay(x);
return ans + 1;
}
ans += cnt[x];
x = son[x][1];
}
}
}
step 9:
查询第 k 大的数,注意 k 不能大于 root.siz。
注意,不论是什么操作,最后都要 splay 一下。
int kth(int k) {
int x = root;
for (;;) {
if (son[x][0] && k <= siz[son[x][0]])
x = son[x][0];
else {
int tmp = siz[son[x][0]] + cnt[x];
if (k <= tmp) {
splay(x);
return val[x];
}
k -= tmp, x = son[x][1];
}
}
}
step 10:
查找 x 的前驱后继。
先插入 x,然后 x 在根的位置,从左子树一直向右走/右子树一直向左走,最后删除。
这个也要 splay 的,不然复杂度不正确。
int pre() {
int x = son[root][0];
while (son[x][1])
x = son[x][1];
splay(x);
return x;
}
int nxt() {
int x = son[root][1];
while (son[x][0])
x = son[x][0];
splay(x);
return x;
}
step 11:
删除值为 x 的结点,需保证 x 在集合内。
先旋转值为 x 的结点至根。
- 若根的
cnt大于1,cnt--,退出。 - 否则合并根的左右子树。
这个分类讨论一下就好,最后虽然看起来没有 splay 但其实在 rnk 的时候就已经 splay 过了。
void del(int x) {
rnk(x);
if (cnt[root] > 1) {
cnt[root]--;
maintain(root);
return;
}
if (!son[root][0] && !son[root][1]) {
clear(root), root = 0;
return;
}
if (!son[root][0]) {
int k = root;
root = son[root][1];
fa[root] = 0;
clear(k);
return;
}
if (!son[root][1]) {
int k = root;
root = son[root][0];
fa[root] = 0;
clear(k);
return;
}
int k = root, lf = pre();
splay(lf);
son[root][1] = son[k][1];
if (son[k][1])
fa[son[k][1]] = root;
clear(k);
maintain(root);
}
step 12:
有人觉得然后就没有了,其实,,
你还要调试
递归输出序列,调试帮手:
void print(int pos) {
if (!pos)
return;
print(son[pos][0]);
for (int i = 1; i <= cnt[pos]; i++)
printf("%d ", val[pos]);
print(son[pos][1]);
}
void print() {
print(root);
putchar('\n');
}
这种风格似乎码量很大,但也比 lrj 的指针好调 QwQ
这样就把基本操作讲完了~
P6136 code
完整版代码在这里:
code
ext:
P3391 【模板】文艺平衡树
参考资料:[3]
Splay 的强大之处不止在于她能够进行一系列的平衡树操作,还在于一个很重要的扩展应用:
打标记
能够打标记,就意味着 Splay 能够像线段树那样支持一些区间操作,尤其是...