洛谷P5266
前言:
看大家都在用 STL/pb_ds 解决问题,可大家真的知道如何实现一个 map 吗?(你们肯定都会)
map 底层实现是平衡树。
本题可以使用 AVL 树手写 map 解决,相当于支持插入、删除、查询与求学生总数的操作的平衡树,原题的修改可以在插入时解决
注意:本题解的主要目的并非讲解平衡树的原理。 oi-wiki 上关于 AVL 树的讲解语焉不详,所以我决定写这篇题解,提供一种 AVL 树的易懂实现。
原理:
关于平衡树,可以左转 oi-wiki:link
AVL 树是平衡树的一种,对于每个节点 ,AVL 树维护字段 代表以 为节点的子树的高度。AVL 树的大部分操作与朴素 BST 相同,但在插入或删除时要检测当前节点的平衡性。我们定义平衡因子 为节点 左子树与右子树的高度之差。如果 ,则 不平衡。对于不平衡的状况,AVL 树通过左旋与右旋回复平衡。
代码:
#include <bits/stdc++.h>
using namespace std;
class tree {
private:
int how_many = 0;//共有多少元素
struct node {//定义树节点结构体
string name;//学生的名字
int score, h;//学生的分数,树高
node *left, *right;//左右儿子
node(string a, int val) {//构造函数,构造一个名字为a,分值为val的新节点
name = a;
score = val;
h = 1;
left = right = nullptr;
}
} *rt;
void updatehigh(node *&p) {//更新高度
p->h = max((p->left ? p->left->h : 0), (p->right ? p->right->h : 0)) + 1;
}
int getbalance(node *p) {//获取平衡因子
return (p->left ? p->left->h : 0) - (p->right ? p->right->h : 0);
}
void leftrotate(node *&p) {//左旋
if (!p || !p->right) {
return;
}
node *r = p->right, *t2 = r->left;
p->right = t2;
r->left = p;
updatehigh(p);
updatehigh(r);
p = r;
}
void rightrotate(node *&p) {//右旋
if (!p || !p->left) {
return;
}
node *l = p->left, *t2 = l->right;
p->left = t2;
l->right = p;
updatehigh(p);
updatehigh(l);
p = l;
}
void rebalance(node *&p) {//维护平衡
if (!p) {
return;
} else {
updatehigh(p);
if (getbalance(p) > 1) {
if (getbalance(p->left) < 0) {
leftrotate(p->left);
}
rightrotate(p);
} else if (getbalance(p) < -1) {
if (getbalance(p->right) > 0) {
rightrotate(p->right);
}
leftrotate(p);
}
}
}
void insert(string s, int val, node *&p) {
if (p == nullptr) {
cout << "OK" << endl;//在成功插入时输出“OK”
how_many++;//更新节点总数
p = new node(s, val);//将p指向新的节点
} else if (s < p->name) {
insert(s, val, p->left);
} else if (s > p->name) {
insert(s, val, p->right);
} else {
cout << "OK" << endl;
p->score = val;
}
rebalance(p);//维持节点p的平衡
}
void del(string s, node *&p) {
if (!p) {
cout << "Not found" << endl;//没有找到,输出
return;
} else if (s < p->name) {
del(s, p->left);
} else if (s > p->name) {
del(s, p->right);
} else {
if (!p->left) {
cout << "Deleted successfully" << endl;//成功删除
node *tmp = p;
p = p->right;
delete tmp;
how_many--;//更新节点总数
} else if (!p->right) {
cout << "Deleted successfully" << endl;
node *tmp = p;
p = p->left;
delete tmp;
how_many--;
} else {
node *tmp = p->right;
while (tmp->left) {
tmp = tmp->left;
}
p->name = tmp->name;
p->score = tmp->score;
del(tmp->name, p->right);//用p的直接后继与p交换,然后删除p
}
}
if (p) {//维护节点p的平衡
rebalance(p);
}
}
int find(string s, node *p) {
if (!p) {
return 2147483647;//在找不到时返回特殊值方便输出
} else if (s < p->name) {
return find(s, p->left);
} else if (s > p->name) {
return find(s, p->right);
} else {
return p->score;
}
}
public://为了让函数调用更方便
int size() {
return how_many;
}
void insert(string name, int val) {
insert(name, val, rt);
}
int find(string name) {
return find(name, rt);
}
void remove(string name) {
del(name, rt);
}
} avl;
int n;
int main() {
cin >> n;
for (int i = 0; i < n; ++i) {
int op;
cin >> op;
if (op == 1) {
string s;
int a;
cin >> s >> a;
avl.insert(s, a);
} else if (op == 2) {
string s;
cin >> s;
int ans = avl.find(s);
if (ans == 2147483647) {
cout << "Not found" << endl;
} else {
cout << ans << endl;
}
} else if (op == 3) {
string s;
cin >> s;
avl.remove(s);
} else {
cout << avl.size() << endl;
}
}
return 0;
}以及,本题还可以用另一种更广为人知的平衡树 Splay 解决。代码如下:
#include <bits/stdc++.h>
#include <utility>
using namespace std;
class Splay {
public:
struct nodeNotfound : exception {
};
private:
size_t number_of_nodes{};
struct node {
string name;
int score;
node *father{}, *left{}, *right{};
node(string name, int score) : name(std::move(name)), score(score) {}
} *root{};
void leftRotate(node *&p) {
node *r = p->right, *t2 = r->left;
r->left = p;
r->father = p->father;
if (r->father) {
if (p->father->left == p) {
r->father->left = r;
} else {
r->father->right = r;
}
}
p->father = r;
p->right = t2;
if (t2) {
t2->father = p;
}
p = r;
}
void rightRotate(node *&p) {
node *l = p->left, *t2 = l->right;
l->right = p;
l->father = p->father;
if (l->father) {
if (p->father->left == p) {
l->father->left = l;
} else {
l->father->right = l;
}
}
p->father = l;
p->left = t2;
if (t2) {
t2->father = p;
}
p = l;
}
void splay(node *&p) {
while (p && p->father) {
if (p->father == root) {
if (p == root->left) {
rightRotate(root);
} else {
leftRotate(root);
}
} else {
node *f = p->father, *g = f->father;
if (p == f->left) {
if (f == g->left) {
rightRotate(g);
// cerr << (root->left->score) << "\n";
rightRotate(f);
} else {
rightRotate(f);
leftRotate(g);
}
} else {
if (f == g->right) {
leftRotate(g);
leftRotate(f);
} else {
leftRotate(f);
rightRotate(g);
}
}
}
// print();
// cout << "\n\n\n";
}
while (root && root->father) {//if without this, root will be wrong
root = root->father;
}
}
void insert(const string &name, int score, node *&p) {
if (!p) {//if root == nullptr
number_of_nodes++;
p = new node(name, score);
return;
} else {
if (name == p->name) {
p->score = score;
splay(p);
} else if (name < p->name) {
if (!p->left) {
number_of_nodes++;
p->left = new node(name, score);
p->left->father = p;
splay(p->left);
} else {
insert(name, score, p->left);
}
} else {
if (!p->right) {
number_of_nodes++;
p->right = new node(name, score);
p->right->father = p;
splay(p->right);
} else {
insert(name, score, p->right);
}
}
}
}
void print(node *p) {
if (!p) {
return;
} else {
cout << p->score << " ";
if (p->father) {
cout << p->father->score << " ";
} else {
cout << "nullptr ";
}
if (p->left) {
cout << p->left->score << " ";
} else {
cout << "nullptr ";
}
if (p->right) {
cout << p->right->score << "\n";
} else {
cout << "nullptr\n";
}
print(p->left);
print(p->right);
}
}
void find(const string &s, node *p) {
if (!p) {
throw nodeNotfound();
} else {
if (s == p->name) {
splay(p);
} else if (s < p->name) {
find(s, p->left);
} else {
find(s, p->right);
}
}
}
public:
void erase(const string &s) {
try {
find(s, root);
} catch (nodeNotfound &err) {
throw nodeNotfound();
}
number_of_nodes--;
node *l = root->left, *tmp1 = l, *r = root->right, *tmp2 = r;
delete root;
root = nullptr;
if (!tmp1 && !tmp2) {
return;
} else if (!tmp1) {
tmp2->father = nullptr;
root = tmp2;
} else if (!tmp2) {
tmp1->father = nullptr;
root = tmp1;
} else {
tmp1->father = tmp2->father = nullptr;
while (tmp1->right) {
tmp1 = tmp1->right;
}
while (tmp2->left) {
tmp2 = tmp2->left;
}
root = l;
splay(tmp1);
root = r;
splay(tmp2);
tmp1->right = tmp2;
tmp2->father = tmp1;
root = tmp1;
}
}
void print() {
print(root);
}
void insert(const string &s, int x) {
insert(s, x, root);
}
int find(const string &s) {
try {
find(s, root);
} catch (nodeNotfound &err) {
throw nodeNotfound();
}
return root->score;
}
size_t size() const {
return number_of_nodes;
}
} student;
int n;
int main() {
ios_base::sync_with_stdio(false);
cin.tie(nullptr);/*
student.root = new Splay::node("1", 1);
student.root->right = new Splay::node("2", 2);
student.root->right->father = student.root;
student.root->right->right = new Splay::node("3", 3);
student.root->right->right->father = student.root->right;
student.print();
student.splay(student.root->right->right);
// student.leftRotate(student.root);
// student.leftRotate(student.root);
student.print();*/
/*student.insert("1", 1, student.root);
student.insert("2", 2, student.root);
student.insert("3", 3, student.root);
student.insert("4", 4, student.root);
student.insert("5", 5, student.root);
student.print();
student.rightRotate(student.root->left);
student.rightRotate(student.root->left);
student.rightRotate(student.root);
student.splay(student.root->left->left->left);
student.erase("2");
cout << student.find("4");
student.print();*/
cin >> n;
for (int i = 0; i < n; ++i) {
int op;
cin >> op;
if (op == 1) {
string s;
int x;
cin >> s >> x;
student.insert(s, x);
cout << "OK\n";
} else if (op == 2) {
string s;
cin >> s;
int ans = 0;
try {
ans = student.find(s);
} catch (Splay::nodeNotfound &err) {
cout << "Not found\n";
continue;
}
cout << ans << "\n";
} else if (op == 3) {
string s;
cin >> s;
try {
student.erase(s);
} catch (Splay::nodeNotfound &err) {
cout << "Not found\n";
continue;
}
cout << "Deleted successfully\n";
} else {
cout << student.size() << "\n";
}
}
return 0;
}Splay 是一种自平衡的二叉搜索树,它并不显式地维护平衡,而是不断通过 splay 操作将访问的节点旋转到根,改善树的形态、保证时间复杂度。
Splay 的 splay 操作使它可以方便地将给定节点提到任意子树的根。从而高效地实现分裂/合并以及其他高级操作(如区间翻转等。
但是,这些操作可能需要进行多次查找及 splay,可能使常数变大。例如我给出的代码中的 erase 操作,使用了类似 fhq-treap 的分裂+合并方法删除,需要进行三次查找及三次 splay,常数较大。