帮我生成一段splay平衡树代码
时间: 2023-07-21 19:09:43 浏览: 52
当然可以,以下是一个简单的 Splay 平衡树实现代码(C++):
```c++
#include <iostream>
using namespace std;
struct Node {
int val;
Node *left, *right, *p;
Node(int v) : val(v), left(nullptr), right(nullptr), p(nullptr) {}
};
class SplayTree {
private:
Node *root;
void left_rotate(Node *x) {
Node *y = x->right;
x->right = y->left;
if (y->left != nullptr) {
y->left->p = x;
}
y->p = x->p;
if (x->p == nullptr) {
root = y;
} else if (x == x->p->left) {
x->p->left = y;
} else {
x->p->right = y;
}
y->left = x;
x->p = y;
}
void right_rotate(Node *x) {
Node *y = x->left;
x->left = y->right;
if (y->right != nullptr) {
y->right->p = x;
}
y->p = x->p;
if (x->p == nullptr) {
root = y;
} else if (x == x->p->left) {
x->p->left = y;
} else {
x->p->right = y;
}
y->right = x;
x->p = y;
}
void splay(Node *x) {
while (x->p != nullptr) {
if (x->p->p == nullptr) {
if (x == x->p->left) {
right_rotate(x->p);
} else {
left_rotate(x->p);
}
} else if (x == x->p->left && x->p == x->p->p->left) {
right_rotate(x->p->p);
right_rotate(x->p);
} else if (x == x->p->right && x->p == x->p->p->right) {
left_rotate(x->p->p);
left_rotate(x->p);
} else if (x == x->p->right && x->p == x->p->p->left) {
left_rotate(x->p);
right_rotate(x->p);
} else {
right_rotate(x->p);
left_rotate(x->p);
}
}
}
public:
SplayTree() : root(nullptr) {}
Node* search(int v) {
Node *x = root;
while (x != nullptr && x->val != v) {
if (v < x->val) {
x = x->left;
} else {
x = x->right;
}
}
if (x != nullptr) {
splay(x);
}
return x;
}
void insert(int v) {
Node *z = new Node(v);
Node *y = nullptr;
Node *x = root;
while (x != nullptr) {
y = x;
if (z->val < x->val) {
x = x->left;
} else {
x = x->right;
}
}
z->p = y;
if (y == nullptr) {
root = z;
} else if (z->val < y->val) {
y->left = z;
} else {
y->right = z;
}
splay(z);
}
void remove(int v) {
Node *z = search(v);
if (z == nullptr) {
return;
}
if (z->left == nullptr) {
transplant(z, z->right);
} else if (z->right == nullptr) {
transplant(z, z->left);
} else {
Node *y = minimum(z->right);
if (y->p != z) {
transplant(y, y->right);
y->right = z->right;
y->right->p = y;
}
transplant(z, y);
y->left = z->left;
y->left->p = y;
}
delete z;
}
Node* minimum(Node *x) {
while (x->left != nullptr) {
x = x->left;
}
return x;
}
Node* maximum(Node *x) {
while (x->right != nullptr) {
x = x->right;
}
return x;
}
void inorder_walk() {
inorder_walk(root);
cout << endl;
}
private:
void transplant(Node *u, Node *v) {
if (u->p == nullptr) {
root = v;
} else if (u == u->p->left) {
u->p->left = v;
} else {
u->p->right = v;
}
if (v != nullptr) {
v->p = u->p;
}
}
void inorder_walk(Node *x) {
if (x != nullptr) {
inorder_walk(x->left);
cout << x->val << ' ';
inorder_walk(x->right);
}
}
};
int main() {
SplayTree tree;
tree.insert(5);
tree.insert(2);
tree.insert(8);
tree.insert(1);
tree.insert(3);
tree.insert(7);
tree.insert(9);
tree.inorder_walk(); // output: 1 2 3 5 7 8 9
tree.remove(5);
tree.inorder_walk(); // output: 1 2 3 7 8 9
tree.search(7);
tree.inorder_walk(); // output: 1 2 3 7 8 9
return 0;
}
```
以上代码实现了 Splay 平衡树的基本操作,包括插入、删除、查找、中序遍历等。如果你需要进一步了解 Splay 平衡树,可以参考相关资料。