Splay的基本操作(插入/删除,查询)

概述

  • 这是一棵二叉查找树
  • 让频繁访问的节点尽量靠近根
    • 将查询,插入等操作的点"旋转"至根
  • 树的高度均摊为\(log_n\)

变量

int root, tot; // root为当前树根(与0相连), tot是最大的编号
struct Snode
{
int ch[2], fa, val, cnt, size;
/*
ch[0], ch[1]分别为左右儿子
fa是父亲节点, val是权值
cnt是这个权值的个数,size是子树(含自己)的总元素个数
*/
} T[MAXN];

rotate

例图:

graph TD;
Z --> Y; Z --> D;
Y --> *X*; Y --> C;
*X* --> A; *X* --> B;

z --> *x*; z --> d;
*x* --> a; *x* --> y;
y --> b; y --> c;

  • 旋转\(X\) : 即把\(X\)放到父亲\(Y\)的位置,并且调整相关的\(X,Y,Z\)与儿子间的关系, 使之仍然满足二叉查找树的性质

其余三种\(X, Y, Z\)的关系的情况:

graph TD;
Z --> D; Z --> Y;
Y --> X; Y --> C;
X --> A; X --> B;

z --> d; z --> x;
x --> a; x --> y;
y --> b; y --> c;

graph TD;
Z --> Y; Z --> D;
Y --> C; Y --> X;
X --> A; X --> B;

z --> x; z --> d;
x --> y; x --> b;
y --> c; y --> a;

graph TD;
Z --> D; Z --> Y;
Y --> C; Y --> X;
X --> A; X --> B;

z --> d; z --> x;
x --> y; x --> b;
y --> c; y --> a;

void rotate(int x)
{
int y = T[x].fa, z = T[y].fa;
int zy = (T[z].ch[1] == y);
int yx = (T[y].ch[1] == x);
T[z].ch[zy] = x; T[x].fa = z;
T[y].ch[yx] = T[x].ch[yx ^ 1]; T[T[x].ch[yx ^ 1]].fa = y;
T[x].ch[yx ^ 1] = y; T[y].fa = x;
update(y); update(x);
}

splay

  • 如果 "\(X\)和父亲\(Y\)的关系" 和 "\(Y\)和父亲\(Z\)的关系" 相等, 需要先rotate(y)rotate(x)​
void splay(int x, int tar) // x --> tar
{
tar = T[tar].fa; // x --> tar's father's son
while (T[x].fa != tar)
{
int y = T[x].fa, z = T[y].fa;
if (z != tar)
(T[z].ch[1] == y) ^ (T[y].ch[1] == x) ? rotate(x) : rotate(y);
rotate(x);
}
if (tar == 0) root = x;
}

插入

利用二叉查找树的性质,找到这个权值应该放的位置

给从根下来的路径上size都+1

如果之前没这个值,那么新建一个节点

int newnode(int v, int fa)
{
int u = ++ tot;
if (fa) T[fa].ch[v > T[fa].val] = u;
T[u].fa = fa;
T[u].val = v;
T[u].ch[0] = T[u].ch[1] = 0;
T[u].cnt = T[u].size = 1;
return tot;
}

否则直接计数+1

void insert(int v)
{
int now = root, fa = 0;
if (root == 0) return (void) ( root = newnode(v, 0) );
while (1)
{
T[now].size ++;
if (T[now].val == v) return (void) ( T[now].cnt ++, splay(now, root) );
if (!T[now].ch[v > T[now].val])
{
T[now].ch[v > T[now].val] = newnode(v, now);
return (void) ( splay(T[now].ch[v > T[now].val], root) );
}
now = T[now].ch[v > T[now].val];
}
}

注意插入完后,将其splay到根

查找权值\(v\)的节点编号

利用二叉查找树的性质

int find(int v)
{
int now = root;
if (!now) return 0;
while (1)
{
if (T[now].val == v) { splay(now, root); return now; }
if (!T[now].ch[v > T[now].val]) return 0;
now = T[now].ch[v > T[now].val];
}
}

查找完后,将其splay到根

删除

  • 首先找到权值对应的点, splay到根

    int now = find(x);
    if (!now) return ;
  • 如果这个点有多个, 计数-1即可

    if (T[now].cnt > 1) return (void)(T[now].cnt --, T[now].size --);
  • 如果只剩这一个点, 清空即可

    if (!T[now].ch[0] && !T[now].ch[1]) return (void)(root = 0);
  • 如果有一个儿子, 将儿子变成根, 清空

    if (!T[now].ch[0]) return (void)(root = T[now].ch[1], T[root].fa = 0);
    if (!T[now].ch[1]) return (void)(root = T[now].ch[0], T[root].fa = 0);
  • 如果有两个儿子, 则将左子树中最大的变成根, 右子树变成现在的右儿子, 清空

    int left = T[root].ch[0];
    while (T[left].ch[1]) left = T[left].ch[1];
    splay(left, root);
    T[left].ch[1] = T[now].ch[1]; T[T[now].ch[1]].fa = root;
    clear(now);
    update(root);

查找权值\(v\)的排名

可以将这个值旋转到根,排名即左子树大小+1

int rank(int v)
{
int now = find(v);
if (!now) return 0;
return T[T[root].ch[0]].size + 1;
}

或者二叉查找树做(查完splay)

int rank(int v)
{
int ans = 0, now = root;
while (1)
{
if (T[now].val == v)
{
ans += T[T[now].ch[0]].size + 1;
splay(now, root);
return ans;
}
if (now == 0) return 0;
if (T[now].val > v) now = T[now].ch[0];
else if (T[now].val < v)
{
ans += T[T[now].ch[0]].size + T[now].cnt;
now = T[now].ch[1];
}
}
}

查找排名的权值

还是二叉查找树, 查完splay

int arank(int x)
{
if (x == 0) return 0;
int now = root;
while (1)
{
int used = T[now].cnt + T[T[now].ch[0]].size;
if (used >= x && x > T[T[now].ch[0]].size) break;
if (x > used) x -= used, now = T[now].ch[1];
else now = T[now].ch[0];
}
splay(now, root);
return T[now].val;
}

前驱/后继

二叉查找树做

int pre(int v)
{
int res = -INF, now = root;
while (now)
{
if (v > T[now].val) res = max(res, T[now].val), now = T[now].ch[1];
else now = T[now].ch[0];
}
return res;
}
int nex(int v)
{
int res = INF, now = root;
while (now)
{
if (v < T[now].val) res = min(res, T[now].val), now = T[now].ch[0];
else now = T[now].ch[1];
}
return res;
}

或者先插入, splay到根, 然后找左边最大的/右边最小的, 最后删除

Splay的基本操作(插入/删除,查询)的更多相关文章

  1. 洛谷 P2042 [NOI2005]维护数列-Splay(插入 删除 修改 翻转 求和 最大的子序列)

    因为要讲座,随便写一下,等讲完有时间好好写一篇splay的博客. 先直接上题目然后贴代码,具体讲解都写代码里了. 参考的博客等的链接都贴代码里了,有空再好好写. P2042 [NOI2005]维护数列 ...

  2. Hibernate插入、查询、删除操作 HQL

    Hibernate的所有的操作都是通过Session完成的. 基本步骤如下: 1:通过配置文件得到SessionFactory: SessionFactory sessionFactory=new C ...

  3. Hibernate框架的基本搭建(一个小的java project的测试向数据库中插入和查询数据的功能)

    Hibernate介绍:Hibernate是一种“对象-关系型数据映射组件”,它使用映射文件将对象(object)与关系型数据(Relational)相关联,在Hibernate中映射文件通常以&qu ...

  4. python Trie树和双数组TRIE树的实现. 拥有3个功能:插入,删除,给前缀智能找到所有能匹配的单词

    #coding=utf- #字典嵌套牛逼,别人写的,这样每一层非常多的东西,搜索就快了,树高26.所以整体搜索一个不关多大的单词表 #还是O(). ''' Python 字典 setdefault() ...

  5. [LeetCode] Insert Delete GetRandom O(1) - Duplicates allowed 常数时间内插入删除和获得随机数 - 允许重复

    Design a data structure that supports all following operations in average O(1) time. Note: Duplicate ...

  6. [LeetCode] Insert Delete GetRandom O(1) 常数时间内插入删除和获得随机数

    Design a data structure that supports all following operations in average O(1) time. insert(val): In ...

  7. Trie树的创建、插入、查询的实现

    原文:http://blog.chinaunix.net/xmlrpc.php?r=blog/article&uid=28977986&id=3807947 1.什么是Trie树 Tr ...

  8. java序列化对象 插入、查询、更新到数据库

    java序列化对象 插入.查询.更新到数据库 : 实现代码例如以下: import java.io.ByteArrayInputStream; import java.io.ByteArrayOutp ...

  9. EF框架操作postgresql,实现WKT类型坐标的插入,查询,以及判断是否相交

    1.组件配置 首先,要下载.NET for Postgresql的驱动,npgsql,EF6,以及EntityFramework6.Npgsql,版本号 3.1.1.0. 由于是mvc项目,所以,把相 ...

随机推荐

  1. [转帖]linux基础知识大纲

    linux基础知识大纲 https://blog.csdn.net/CSDN___LYY/article/details/80810403 1.Linux操作系统概述Linux操作系统的发展过程.创始 ...

  2. [转帖]mDNS原理的简单理解

    mDNS原理的简单理解 https://binkery.com/archives/318.html 发现还有avahi-daemon mdns 设置ip地址 等等事项 网络部分 自己学习的还是不够多 ...

  3. 【linux】CentOS 查看系统时间,修改时区

    ===============CentOS 7.6================ 1.查看系统时间 date 查看当前系统时间以及时区结果是: Mon Jul 8 09:23:31 UTC 2019 ...

  4. Java 函数式编程--流操作

    GitHub Page: http://blog.cloudli.top/posts/Java-函数式编程-流操作/ 外部迭代到内部迭代 在使用集合类时,通用的方式是在使用 for 循环集合上进行迭代 ...

  5. C# static readonly 修饰符初始化变量

    同事问了一个问题,readonly和static啥区别? 我就写了个demo运行了下: /*** * 验证初始化次数:static只初始化一次,无论多少用户访问,在内存中只有一份 * readonly ...

  6. NetCoreApi框架搭建(二、Nlog使用配置)

    本文只配置了简单文件存储 1.添加nuget包 2.添加日志配置文件nlog.config 这里配置了三个target区分不同的日志,具体配置需要自己研究,推荐链接https://www.cnblog ...

  7. VBA笔记

    1.VBA数据类型 数据类型 存储空间大小 范围 Byte 1个字节 0-255 Boolean 2个字节 true或false Integer 2个字节 -32768-32767 Long 4个字节 ...

  8. 大规模定制模式之于MES的三点思考

    大规模定制(Mass Custermization) ,其目标是大规模生产定制化产品,并且在效率.质量(一致性)等指标方面与大规模批量生产等齐. 这是一种理想或者追求,其提出的背景是目前越发普遍的多品 ...

  9. logger(三)log4j2简介及其实现原理

    一.log4j2简介 log4j2是log4j 1.x和logback的改进版,据说采用了一些新技术(无锁异步.等等),使得日志的吞吐量.性能比log4j 1.x提高10倍,并解决了一些死锁的bug, ...

  10. Mysql-修改用户连接数据库IP地址和用户名

    将用户连接数据库(5.7.14-7)的IP地址从 10.10.5.16   修改为 10.11.4.197 Mysql> rename user 'username'@'10.10.5.16' ...