AVL:
#include <bits/stdc++.h>
using namespace std;
struct Node {
int key;
int son[2], hei;
} node[12345678];
int total;
struct AVL {
int root;
void update(int i) {
node[i].hei = max(node[node[i].son[0]].hei, node[node[i].son[1]].hei);
}
void rotate(int &i, int c) {
int j = node[i].son[c];
node[i].son[c] = node[j].son[c ^ 1];
node[j].son[c ^ 1] = i;
update(i), update(j);
i = j;
}
void __insert(int &i, int k) {
if (!i) {
i = k;
return;
}
int c = node[i].key < node[k].key;
__insert(node[i].son[c], k);
if (node[node[i].son[c]].hei - node[node[i].son[c ^ 1]].hei >= 2) {
rotate(i, c);
}
}
void insert(int val) {
int k = ++ total;
node[k].key = val;
node[k].hei = 1;
__insert(root, k);
}
} T;
int main() {
mt19937 Rand(1);
for (int i = 1; i <= 1e7; i ++) {
int x = Rand() >> 1;
T.insert(x);
}
printf("clock=%d\n", clock());
return 0;
}
Splay
#include <bits/stdc++.h>
using namespace std;
mt19937 Rand(1);
bitset<1 << 24> s;
int main() {
for (int i = 1; i <= 1e7; i ++) {
int x = Rand() >> 8;
s[x] = 1;
}
printf("clock=%d\n", clock());
return 0;
}
Treap
#include <bits/stdc++.h>
using namespace std;
mt19937 Rand(552);
struct Node {
int key;
int son[2], up;
} node[12345678];
int total;
struct Treap {
int root;
void rotate(int &i, int c) {
int j = node[i].son[c];
node[i].son[c] = node[j].son[c ^ 1];
node[j].son[c ^ 1] = i;
i = j;
}
void __insert(int &i, int k) {
if (!i) {
i = k;
return;
}
int c = node[i].key < node[k].key;
__insert(node[i].son[c], k);
if (node[node[i].son[c]].up < node[i].up) {
rotate(i, c);
}
}
void insert(int val) {
int k = ++ total;
node[k].key = val;
node[k].up = Rand();
__insert(root, k);
}
} T;
int main() {
for (int i = 1; i <= 1e7; i ++) {
int x = Rand() >> 1;
T.insert(x);
}
printf("clock=%d\n", clock());
return 0;
}
起因是明天我要做英语演讲,就找董王求了一份office。下载过程太长了,董王无聊,就开始写AVL,写完了又开始写Treap,因为Splay要存father所以懒得写了,就写了一个"S"play
标签:亲笔,node,insert,int,代码,son,Nodgd,key,hei From: https://www.cnblogs.com/zhuzc/p/18081127