各种平衡树Treap/SBT/Avl/Splay tree

看了这么久的平衡树,是时候做个总结了。

以poj 3481为例,敲了四份代码,分别是Treap ,Size Balance Tree,Avl Tree,splay tree。

唯独少了红黑树T_T。。。

总的来说每种平衡树各有各的优点吧:

Treap写起来简单上手也快如果熟练的话不到十分种可以敲完。

SBT速度快名不虚传。。。

Avl树高度平衡吧,不过实际的效果不尽如人意,可能是我实现的姿势不对吧/(ㄒoㄒ)/~~

splay tree各方面比较均衡,特别的伸展树在维护序列方面相对其它几种树优势明显。

可持久化的Treap除外(因为没比较过故不好妄下结论)。

总之,只要理解了旋转操作和建树的思想,每种树实现起来都很方便。。。

好了少扯淡了代码如下:

Treap:

#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#define Max_N 110000
#define size(_) ((_)==NULL ? 0 : (_)->size)
typedef struct _trp{
	int client, key, size, fix;
	struct _trp *ch[2];
}treap, *Treap;
treap stack[Max_N];
int sz = 0;
int run(){
	static int x = 1840828537;
	x += (x << 2) | 1;
	return x;
}
void update(Treap x){
	if (x == NULL) return;
	x->size = size(x->ch[0]) + size(x->ch[1]) + 1;
}
void rotate(Treap *x, int d){
	Treap k = (*x)->ch[!d];
	(*x)->ch[!d] = k->ch[d];
	k->ch[d] = *x;
	k->size = (*x)->size;
	update(*x);
	*x = k;
}
void insert(Treap *x, int client, int key){
	if (*x == NULL){
		*x = &stack[sz++];
		(*x)->ch[0] = (*x)->ch[1] = NULL;
		(*x)->key = key, (*x)->size = 1, (*x)->client = client, (*x)->fix = run();
	} else {
		int d = key > (*x)->key;
		insert(&((*x)->ch[d]), client, key);
		update(*x);
		if ((*x)->ch[d]->fix < (*x)->fix) rotate(x, !d);
	}
}
void _delete(Treap *x, int key){
	if (*x == NULL) return;
	if ((*x)->key == key){
		if (!(*x)->ch[0] || !(*x)->ch[1]){
			*x = (*x)->ch[0] ? (*x)->ch[0] : (*x)->ch[1];
		} else {
			int d = (*x)->ch[0]->fix < (*x)->ch[1]->fix;
			rotate(x, d);
			_delete(&((*x)->ch[d]), key);
		}
	} else {
		_delete(&((*x)->ch[key>(*x)->key]), key);
	}
	if (*x != NULL) update(*x);
}
Treap find_kth(Treap x, int k){
	int t = 0;
	for (; x != NULL;){
		t = size(x->ch[0]);
		if (k == t + 1) break;
		else if (k <= t) x = x->ch[0];
		else k -= t + 1, x = x->ch[1];
	}
	return x;
}
int main(){
#ifdef LOCAL
	freopen("in.txt", "r", stdin);
	freopen("out.txt", "w+", stdout);
#endif
	int n, a, b;
	Treap root = NULL, ret = NULL;
	while (~scanf("%d", &n) && n){
		ret = NULL;
		if (2 == n || 3 == n){
			if (2 == n && root) ret = find_kth(root, root->size);
			else if (3 == n && root) ret = find_kth(root, 1);
			if (!ret || !root) printf("0\n");
			else printf("%d\n", ret->client), _delete(&root, ret->key);
		} else {
			scanf("%d %d", &a, &b);
			insert(&root, a, b);
		}

	}
	return 0;
}

SBT:

#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#define Max_N 110000
#define size(_) ((_)==NULL ? 0 : (_)->size)
typedef struct _sbt{
	int client, key, size;
	struct _sbt *ch[2];
}SBTNode, *SBT;
SBTNode stack[Max_N];
int sz = 0;
void rotate(SBT *x, int d){
	SBT k = (*x)->ch[!d];
	(*x)->ch[!d] = k->ch[d];
	k->ch[d] = *x;
	k->size = (*x)->size;
	(*x)->size = size((*x)->ch[0]) + size((*x)->ch[1]) + 1;
	*x = k;
}
void Maintain(SBT *x, int d){
	if ((*x)->ch[d] == NULL) return;
	if (size((*x)->ch[d]->ch[d]) > size((*x)->ch[!d])) rotate(x, !d);
	else if (size((*x)->ch[d]->ch[!d]) > size((*x)->ch[!d]))
		rotate(&((*x)->ch[d]), d), rotate(x, !d);
	else return;
	Maintain(&((*x)->ch[d]), 0);
	Maintain(&((*x)->ch[!d]), 1);
	Maintain(x, 0), Maintain(x, 1);
}
void insert(SBT *x, int client, int key){
	if (*x == NULL){
		*x = &stack[sz++];
		(*x)->ch[0] = (*x)->ch[1] = NULL;
		(*x)->key = key, (*x)->size = 1, (*x)->client = client;
	} else {
		(*x)->size++;
		insert(&((*x)->ch[key > (*x)->key]), client, key);
		Maintain(x, key >= (*x)->key);
	}
}
void _delete(SBT *x, int key){
	if (*x == NULL) return;
	(*x)->size--;
	if ((*x)->key == key){
		if (!(*x)->ch[0] || !(*x)->ch[1]){
			*x = (*x)->ch[0] ? (*x)->ch[0] : (*x)->ch[1];
		} else {
			SBT ret = (*x)->ch[1];
			for (; ret->ch[0] != NULL; ret = ret->ch[0]);
			_delete(&((*x)->ch[1]), (*x)->key = ret->key);
		}
	} else {
		_delete(&((*x)->ch[key > (*x)->key]), key);
	}
}
SBT find_kth(SBT x, int k){
	int t = 0;
	for (; x != NULL;){
		t = size(x->ch[0]);
		if (k == t + 1) break;
		else if (k <= t) x = x->ch[0];
		else k -= t + 1, x = x->ch[1];
	}
	return x;
}
int main(){
#ifdef LOCAL
	freopen("in.txt", "r", stdin);
	freopen("out.txt", "w+", stdout);
#endif
	int n, a, b;
	SBT root = NULL, ret = NULL;
	while (~scanf("%d", &n) && n){
		ret = NULL;
		if (2 == n || 3 == n){
			if (2 == n && root) ret = find_kth(root, root->size);
			else if (3 == n && root) ret = find_kth(root, 1);
			if (!ret || !root) printf("0\n");
			else printf("%d\n", ret->client), _delete(&root, ret->key);
		} else {
			scanf("%d %d", &a, &b);
			insert(&root, a, b);
		}

	}
	return 0;
}

Avl:

#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#define Max_N 200000
#define _max(a,b) ((a)>(b)?(a):(b))
#define size(_) ((_)==NULL ? 0 : (_)->size)
#define Height(_) ((_)==NULL ? -1 : (_)->Height)
typedef struct _avl{
	int client, key, size, Height;
	struct _avl *ch[2];
}AvlTree, *Avl;
AvlTree stack[Max_N];
int sz = 0;
void update(Avl x){
	if (x == NULL) return;
	x->size = size(x->ch[0]) + size(x->ch[1]) + 1;
	x->Height = _max(Height(x->ch[0]), Height(x->ch[1])) + 1;
}
void rotate(Avl *x, int d){
	Avl k = (*x)->ch[!d];
	(*x)->ch[!d] = k->ch[d];
	k->ch[d] = *x;
	update(*x);
	update(k);
	*x = k;
}
void Maintain(Avl *x, int d){
	if (Height((*x)->ch[d]) - Height((*x)->ch[!d]) == 2){
		if (Height((*x)->ch[d]->ch[d]) - Height((*x)->ch[d]->ch[!d]) == 1) rotate(x, !d);
		else if (Height((*x)->ch[d]->ch[d]) - Height((*x)->ch[d]->ch[!d]) == -1){
			rotate(&((*x)->ch[d]), d), rotate(x, !d);
		}
	}
}
void insert(Avl *x, int client, int key){
	if (*x == NULL){
		*x = &stack[sz++];
		(*x)->ch[0] = (*x)->ch[1] = NULL;
		(*x)->key = key, (*x)->Height = 0, (*x)->size = 1, (*x)->client = client;
	} else {
		int d = key > (*x)->key;
		insert(&((*x)->ch[d]), client, key);
		update(*x);
		Maintain(x, d);
	}
}
void _delete(Avl *x, int key){
	if (*x == NULL) return;
	if ((*x)->key == key){
		if (!(*x)->ch[0] || !(*x)->ch[1]){
			*x = (*x)->ch[0] ? (*x)->ch[0] : (*x)->ch[1];
		} else {
			Avl ret = (*x)->ch[1];
			for (; ret->ch[0] != NULL; ret = ret->ch[0]);
			_delete(&((*x)->ch[1]), (*x)->key = ret->key);
		}
	} else {
		_delete(&((*x)->ch[key > (*x)->key]), key);
	}
	if (*x != NULL){
		update(*x);
		Maintain(x, 0), Maintain(x, 1);
	}
}
Avl find_kth(Avl x, int k){
	int t = 0;
	for (; x != NULL;){
		t = size(x->ch[0]);
		if (k == t + 1) break;
		else if (k <= t) x = x->ch[0];
		else k -= t + 1, x = x->ch[1];
	}
	return x;
}
int main(){
#ifdef LOCAL
	freopen("in.txt", "r", stdin);
	freopen("out.txt", "w+", stdout);
#endif
	int n, a, b;
	Avl root = NULL, ret = NULL;
	while (~scanf("%d", &n) && n){
		ret = NULL;
		if (2 == n || 3 == n){
			if (2 == n && root) ret = find_kth(root, root->size);
			else if (3 == n && root) ret = find_kth(root, 1);
			if (!ret || !root) printf("0\n");
			else printf("%d\n", ret->client), _delete(&root, ret->key);
		} else {
			scanf("%d %d", &a, &b);
			insert(&root, a, b);
		}

	}
	return 0;
}

splay tree:

#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#define Max_N 200000
#define size(_) ((_)==null ? 0 : (_)->size)
typedef struct _spt{
	int client, key, size;
	struct _spt *pre, *ch[2];
}splay;
splay *null, *root, stack[Max_N];
int sz = 0;
splay *_calloc(int client, int key){
	splay *p = &stack[sz++];
	p->pre = p->ch[0] = p->ch[1] = null;
	p->size = 1, p->key = key, p->client = client;
	return p;
}
void push_up(splay *x){
	if (x == null) return;
	x->size = size(x->ch[0]) + size(x->ch[1]) + 1;
}
void rotate(splay *x, int d){
	splay *y = x->pre;
	y->ch[!d] = x->ch[d];
	if (x->ch[d] != null) x->ch[d]->pre = y;
	x->pre = y->pre;
	if (y->pre != null) y->pre->ch[y->pre->ch[0] != y] = x;
	x->ch[d] = y;
	y->pre = x;
	push_up(y);
	if (y == root) root = x;
}
void splay_splay(splay *x, splay *f){
	for (; x->pre != f;){
		if (x->pre->pre == f){
			rotate(x, x->pre->ch[0] == x);
		} else {
			splay *y = x->pre, *z = y->pre;
			if (z->ch[0] == y){
				if (y->ch[0] == x) rotate(y, 1), rotate(x, 1);
				else rotate(x, 0), rotate(x, 1);
			} else {
				if (y->ch[1] == x)
					rotate(y, 0), rotate(x, 0);
				else rotate(x, 1), rotate(x, 0);
			}
		}
	}
	push_up(x);
}
void insert(int client, int key){
	splay *fp = null, *p = root;
	if (root == null){
		root = _calloc(client, key);
		return;
	}
	for (; p != null;){
		fp = p;
		if (key > p->key) p = p->ch[1];
		else p = p->ch[0];
	}
	p = _calloc(client, key);
	if (fp->key > key) fp->ch[0] = p;
	else fp->ch[1] = p;
	p->pre = fp;
	splay_splay(p, null);
	push_up(p);
}
void _delete(int key){
	splay *p = root, *rt = null;
	while (p != null && p->key != key) p = p->ch[key > p->key];
	if (p == null) return;
	splay_splay(p, null);
	rt = root->ch[0];
	if (rt == null){
		rt = root->ch[1];
	}
	else {
		splay *tmp = rt->ch[1];
		while (tmp != null && tmp->ch[1] != null) tmp = tmp->ch[1];
		if (tmp != null) splay_splay(tmp, root);
		rt = root->ch[0];
		rt->ch[1] = root->ch[1];
		root->ch[1]->pre = rt;
	}
	root = rt;
	root->pre = null;
	if (root != null) push_up(root);
}
void initialize(){
	null = _calloc(-1, -1);
	null->size = 0;
	root = null;
}
splay *find_kth(splay *x, int k){
	int t = 0;
	for (; x != null;){
		t = size(x->ch[0]);
		if (k == t + 1) break;
		else if (k <= t) x = x->ch[0];
		else k -= t + 1, x = x->ch[1];
	}
	if (x == null){
		return null;
	} else {
		splay_splay(x, null);
		return root;
	}
}
int main(){
#ifdef LOCAL
	freopen("in.txt", "r", stdin);
	freopen("out.txt", "w+", stdout);
#endif
	int n, a, b;
	initialize();
	splay *ret = NULL;
	while (~scanf("%d", &n) && n){
		ret = NULL;
		if (2 == n || 3 == n){
			if (2 == n && root) ret = find_kth(root, root->size);
			else if (3 == n && root) ret = find_kth(root, 1);
			if (ret == null || root == null) printf("0\n");
			else printf("%d\n", ret->client), _delete(ret->key);
		} else {
			scanf("%d %d", &a, &b);
			insert(a, b);
		}

	}
	return 0;
}

最后上对比结果:从上到下依次是,splay tree, treap,sbt,avl。

其实蒟蒻只会用c语言不会c++,但提交代码的时候选了c++和gcc两种o(╯□╰)o。。。

时间: 2024-08-09 14:46:25

各种平衡树Treap/SBT/Avl/Splay tree的相关文章

luoguP3369[模板]普通平衡树(Treap/SBT) 题解

链接一下题目:luoguP3369[模板]普通平衡树(Treap/SBT) #include<iostream> #include<cstdlib> #include<cstdio> #include<cmath> #include<cstring> #include<iomanip> #include<algorithm> #include<ctime> #include<queue> #incl

P3369 【模板】普通平衡树(Treap/SBT)

二次联通门 : P3369 [模板]普通平衡树(Treap/SBT) /* luogu P3369 [模板]普通平衡树(Treap/SBT) splay 模板 支持插入x数 删除x数(若有多个相同的数,因只删除一个) 查询x数的排名(若有多个相同的数,因输出最小的排名) 查询排名为x的数 求x的前驱(前驱定义为小于x,且最大的数) 求x的后继(后继定义为大于x,且最小的数) */ #include <cstdio> #define Max 200005 void read (int &

平衡树初阶——AVL平衡二叉查找树+三大平衡树(Treap + Splay + SBT)模板【超详解】

平衡树初阶——AVL平衡二叉查找树 一.什么是二叉树 1. 什么是树. 计算机科学里面的树本质是一个树状图.树首先是一个有向无环图,由根节点指向子结点.但是不严格的说,我们也研究无向树.所谓无向树就是将有向树的所有边看成无向边形成的树状图.树是一种递归的数据结构,所以我们研究树也是按照递归的方式去研究的. 2.什么是二叉树. 我们给出二叉树的递归定义如下: (1)空树是一个二叉树. (2)单个节点是一个二叉树. (3)如果一棵树中,以它的左右子节点为根形成的子树都是二叉树,那么这棵树本身也是二叉

数组splay ------ luogu P3369 【模板】普通平衡树(Treap/SBT)

二次联通门 : luogu P3369 [模板]普通平衡树(Treap/SBT) #include <cstdio> #define Max 100005 #define Inline __attri\ bute__( ( optimize( "-O2" ) ) ) Inline void read (int &now) { now = 0; register char word = getchar (); bool temp = false; while (wor

AC日记——【模板】普通平衡树(Treap/SBT) 洛谷 P3369

[模板]普通平衡树(Treap/SBT) 思路: 劳资敲了一个多星期: 劳资终于a了: 劳资一直不a是因为一个小错误: 劳资最后看的模板: 劳资现在很愤怒: 劳资不想谈思路!!! 来,上代码: #include <cstdio> using namespace std; #define maxn 1000005 struct SplayTreeNodeType { int w,key,opi,size,ch[2]; }; struct SplayTreeNodeType tree[maxn];

替罪羊树 ------ luogu P3369 【模板】普通平衡树(Treap/SBT)

二次联通门 : luogu P3369 [模板]普通平衡树(Treap/SBT) 闲的没事,把各种平衡树都写写 比较比较... 下面是替罪羊树 #include <cstdio> #include <vector> #define Max_ 100010 #define Inline __attri\ bute__( ( optimize( "-O2" ) ) ) Inline void read (int &now) { register char w

红黑树 ------ luogu P3369 【模板】普通平衡树(Treap/SBT)

二次联通门 : luogu P3369 [模板]普通平衡树(Treap/SBT) 近几天闲来无事...就把各种平衡树都写了一下... 下面是红黑树(Red Black Tree) #include <cstdio> #define Max 100001 #define Red true #define Black false #define Inline __attri\ bute__( ( optimize( "-O2" ) ) ) Inline void read (i

BST, AVL Tree 和 Splay Tree 的实现

一.定义. 1.1 BST 二叉搜索树,也称有序二叉树,排序二叉树,是指一棵空树或者具有下列性质的二叉树: ① 若任意节点的左子树不空,则左子树上所有结点的值均小于它的根结点的值: ② 若任意节点的右子树不空,则右子树上所有结点的值均大于它的根结点的值: ③ 任意节点的左.右子树也分别为二叉查找树. ④ 没有键值相等的节点. 1.2 AVL Tree 平衡二叉树定义(AVL):它或者是一颗空树,或者具有以下性质的二叉树:它的左子树和右子树的深度之差(平衡因子)的绝对值不超过1,且它的左子树和右子

bzoj 3223/tyvj 1729 文艺平衡树 splay tree

原题链接:http://www.tyvj.cn/p/1729 这道题以前用c语言写的splay tree水过了.. 现在接触了c++重写一遍... 只涉及区间翻转,由于没有删除操作故不带垃圾回收,具体如下: 1 #include<cstdio> 2 #include<cstdlib> 3 #include<iostream> 4 #include<algorithm> 5 const int MAX_N = 100010; 6 struct Node{ 7