SBT(Size Balance Tree), 即一种通过子树大小(size)保持平衡的BST

SBT的基本性质是:每个节点的size大小必须大于等于其兄弟的儿子的size大小:

当我们插入或者删除一个节点之后,SBT的性质会有所改变,此时需要函数maintain(mt)来维持平衡

mt(T)用于修复以T为根的子树的SBT 调用mt(T)的前提是T的子树都已经是SBT了

{由于左右对称,这里只讨论关于上图第一个不等式不成立的例子}

情形1:size[A] > size[R]

此时只需继续mt(A)与mt(L)就行

情形2:size[B] > size[R]

此时继续mt(L)与mt(B)

综上,Maintain代码如下:

inline void update(node* r) { r->sz = r->lc->sz + r->rc->sz + 1; }

void rotate(node* &r, bool f) {
node *t = r->ch[f];
r->ch[f] = t->ch[!f];
t->ch[!f] = r;
t->sz = r->sz;
update(r);
r = t;
} void mt(node* &r, bool f) { //利用左右对称带上参数f同时减去不必要的检查
if(r == NILL) return; //NILL 为空指针
if(r->ch[f]->ch[f]->sz > r->ch[!f]->sz)
rotate(r, f);
else if(r->ch[f]->ch[!f]->sz > r->ch[!f]->sz)
rotate(r->ch[f], !f), rotate(r, f);
else return;
mt(r->ch[f], f);
mt(r, f);
}

Analysis of Height

F[H]:高度为H最大结点个数,有定理:

F[H] = Fibonacci[H+2]-1

∴N个结点的SBT的最坏深度最大满足(F[H]<=N)的H,因此:

根据各种分析之后可得:Maintain的单次操作为O(1) SBT的其他操作时间复杂度都为为log(n)

所以SBT被称为目前最快的二叉平衡树!贴上模板题的代码(普通平衡树):

#include <cstdio>
#include <algorithm>
using namespace std;
#define lc ch[0]
#define rc ch[1] const int MAXN = 500000;
const int INF = 0x3f3f3f3f; struct node {
node* ch[2];
int sz, v;
node(){}
}SBT[MAXN+10], *NILL=new node, *root=NILL, *tot=SBT; int getint() {
int ret = 0; bool f = 0; char ch;
while((ch=getchar()) < '0' || ch > '9')if(ch == '-') f = !f;
while(ch >= '0' && ch <= '9') ret = ret * 10 + ch - '0', ch = getchar();
return f ? -ret : ret;
} void init() {
NILL->lc = NILL;
NILL->rc = NILL;
NILL->sz = 0;
}
inline void update(node* r) { r->sz = r->lc->sz + r->rc->sz + 1; }
node* newnode() {
tot->lc = tot->rc = NILL;
tot->sz = 1;
return tot++;
} void rotate(node* &r, bool f) {
node *t = r->ch[f];
r->ch[f] = t->ch[!f];
t->ch[!f] = r;
t->sz = r->sz;
update(r);
r = t;
} void mt(node* &r, bool f) {
if(r == NILL) return;
if(r->ch[f]->ch[f]->sz > r->ch[!f]->sz)
rotate(r, f);
else if(r->ch[f]->ch[!f]->sz > r->ch[!f]->sz)
rotate(r->ch[f], !f), rotate(r, f);
else return;
mt(r->ch[f], f);
mt(r, f);
} void insert(node* &r, int v) {
if(r == NILL) {
r = newnode();
r->v = v;
return;
}
r->sz++;
bool k = v > r->v;
insert(r->ch[k], v);
mt(r, k);
} int del(node* &r, int x) {
int ret;
r->sz--;
if(r->v == x || (r->lc == NILL && x < r->v) || (r->rc == NILL && x > r->v)) {
ret = r->v;
if(r->lc == NILL || r->rc == NILL)
r = r->lc==NILL ? r->rc : r->lc;
else r->v = del(r->lc, x);
}
else ret = del(r->ch[x>=r->v], x);
return ret;
} int sel(int val) {
int ret = 1;
node* p = root;
while(p != NILL) {
if(val <= p->v)
p = p->lc;
else {
ret += p->lc->sz + 1;
p = p-> rc;
}
}
return ret;
} int rk(int x)
{
node* p = root;
while(p != NILL){
if(x == p->lc->sz + 1)
return p->v;
if(x <= p->lc->sz)
p = p->lc;
else {
x -= p->lc->sz + 1;
p = p->rc;
}
}
return INF;
} int query(int v, bool f)
{
node* p = root;
int ret = f ? INF : -INF;
while(p != NILL) {
if(p->v != v && (f == (p->v > v) && f == (ret > p->v)))
ret = p->v;
if(v == p->v)
p = p->ch[f];
else p = p->ch[v > p->v];
}
return ret;
} int main () {
init();
int kase = getint();
while(kase--) {
int opt = getint(), x = getint();
switch(opt) {
case 1:insert(root, x); break;
case 2:del(root, x); break;
case 3:printf("%d\n", sel(x)); break;
case 4:printf("%d\n", rk(x)); break;
case 5:printf("%d\n", query(x, 0)); break;
case 6:printf("%d\n", query(x, 1)); break;
}
}
}

但可能还是没有avl快

最新文章

  1. 图片下载缓存防止OOM
  2. 组合数取模Lucas定理及快速幂取模
  3. Tomcat启动服务报错:Unknown version string [3.1]. Default version will be used.
  4. SQL注入原理
  5. jsp:forward response.sendRedirect
  6. ZOJ 3930 Dice Notation
  7. 生成JSON数据--Gson(谷歌)方法
  8. spring boot 快速生成demo工程 官网生成
  9. h5 canvas 图片上传操作
  10. Vue.js 2.x笔记:安装与起步(1)
  11. vscode断点调试简单的服务端文件
  12. 【论文阅读】Wing Loss for Robust Facial Landmark Localisation with Convolutional Neural Networks
  13. python--面向过程编程与面向对象编程
  14. SQL视图命名规则:一般以V_xxx_xxxxxx
  15. spring2.0:The server time zone value &#39;&#195;–&#195;&#194;&#185;&#195;&#186;&#194;&#177;&#195;&#170;&#195;—&#194;&#188;&#195;Š&#194;&#177;&#194;&#188;&#195;&#164;&#39; is unrecognized or represents more than one time zone. You must configure either th
  16. React-Native到0.44版本后Navigator 不能用的问题
  17. Python的安装以及编译器的安装
  18. A1079. Total Sales of Supply Chain
  19. 配置nginx实现windows/iis应用负载均衡
  20. linux shell 中文件编码查看及转换方法

热门文章

  1. Linux组管理、用户管理、查看用户信息、usermod、which、切换用户、修改文件具体权限
  2. quartz2.3.0(三)cron定义调度周期
  3. git clone一个仓库下的单个文件【记录】
  4. Windows服务创建及发布
  5. Centos7部署开源聊天软件rocket.chat
  6. 获取SpringMVC中所有RequestMapping映射URL信息
  7. vmware的三种网络模式讲解
  8. StatusStrip控件的使用(转:http://blog.sina.com.cn/s/blog_4f18c3ec0100fguf.html)
  9. Java中使用HttpPost上传文件以及HttpGet进行API请求(包含HttpPost上传文件)
  10. python day7: time,datetime,sys,pickle,json模块