使用C++编写一个可以运行的红黑树代码,一次性写不完可以分几次来回答
时间: 2024-02-18 16:02:26 浏览: 68
红黑树程序(用C++实现)
好的,下面我给您提供一个简单的 C++ 实现红黑树的代码,您可以根据需要进行修改和完善。
```c++
#include <iostream>
using namespace std;
// 节点颜色
enum Color {RED, BLACK};
// 红黑树节点结构体
struct Node {
int val; // 节点值
Color color; // 节点颜色
Node* left; // 左子节点
Node* right; // 右子节点
Node* parent; // 父节点
// 构造函数
Node(int v) : val(v), color(RED), left(nullptr), right(nullptr), parent(nullptr) {}
};
// 红黑树类
class RBTree {
public:
// 构造函数
RBTree() : root(nullptr) {}
// 插入节点
void insert(int v) {
Node* node = new Node(v);
insertNode(node);
fixupInsert(node); // 插入节点后修正红黑树
}
// 删除节点
void remove(int v) {
Node* node = search(v);
if (node == nullptr) return;
removeNode(node);
fixupRemove(node); // 删除节点后修正红黑树
}
// 查找节点
Node* search(int v) {
Node* node = root;
while (node != nullptr) {
if (v < node->val) node = node->left;
else if (v > node->val) node = node->right;
else return node;
}
return nullptr;
}
// 中序遍历红黑树
void inorderTraverse() {
inorderTraverse(root);
}
private:
Node* root; // 根节点
// 左旋
void leftRotate(Node* node) {
Node* r = node->right;
node->right = r->left;
if (r->left != nullptr) r->left->parent = node;
r->parent = node->parent;
if (node->parent == nullptr) root = r;
else if (node == node->parent->left) node->parent->left = r;
else node->parent->right = r;
r->left = node;
node->parent = r;
}
// 右旋
void rightRotate(Node* node) {
Node* l = node->left;
node->left = l->right;
if (l->right != nullptr) l->right->parent = node;
l->parent = node->parent;
if (node->parent == nullptr) root = l;
else if (node == node->parent->left) node->parent->left = l;
else node->parent->right = l;
l->right = node;
node->parent = l;
}
// 插入节点
void insertNode(Node* node) {
Node* parent = nullptr;
Node* cur = root;
while (cur != nullptr) {
parent = cur;
if (node->val < cur->val) cur = cur->left;
else cur = cur->right;
}
node->parent = parent;
if (parent == nullptr) root = node;
else if (node->val < parent->val) parent->left = node;
else parent->right = node;
}
// 删除节点
void removeNode(Node* node) {
Node* child = nullptr;
if (node->left != nullptr && node->right != nullptr) {
Node* succ = node->right;
while (succ->left != nullptr) succ = succ->left;
node->val = succ->val;
node = succ;
}
if (node->left != nullptr) child = node->left;
else child = node->right;
if (node->parent == nullptr) root = child;
else if (node == node->parent->left) node->parent->left = child;
else node->parent->right = child;
if (child != nullptr) child->parent = node->parent;
}
// 插入节点后修正红黑树
void fixupInsert(Node* node) {
while (node->parent != nullptr && node->parent->color == RED) {
if (node->parent == node->parent->parent->left) {
Node* uncle = node->parent->parent->right;
if (uncle != nullptr && uncle->color == RED) {
node->parent->color = BLACK;
uncle->color = BLACK;
node->parent->parent->color = RED;
node = node->parent->parent;
} else {
if (node == node->parent->right) {
node = node->parent;
leftRotate(node);
}
node->parent->color = BLACK;
node->parent->parent->color = RED;
rightRotate(node->parent->parent);
}
} else {
Node* uncle = node->parent->parent->left;
if (uncle != nullptr && uncle->color == RED) {
node->parent->color = BLACK;
uncle->color = BLACK;
node->parent->parent->color = RED;
node = node->parent->parent;
} else {
if (node == node->parent->left) {
node = node->parent;
rightRotate(node);
}
node->parent->color = BLACK;
node->parent->parent->color = RED;
leftRotate(node->parent->parent);
}
}
}
root->color = BLACK;
}
// 删除节点后修正红黑树
void fixupRemove(Node* node) {
while (node != root && node->color == BLACK) {
if (node == node->parent->left) {
Node* bro = node->parent->right;
if (bro->color == RED) {
bro->color = BLACK;
node->parent->color = RED;
leftRotate(node->parent);
bro = node->parent->right;
}
if (bro->left->color == BLACK && bro->right->color == BLACK) {
bro->color = RED;
node = node->parent;
} else {
if (bro->right->color == BLACK) {
bro->left->color = BLACK;
bro->color = RED;
rightRotate(bro);
bro = node->parent->right;
}
bro->color = node->parent->color;
node->parent->color = BLACK;
bro->right->color = BLACK;
leftRotate(node->parent);
node = root;
}
} else {
Node* bro = node->parent->left;
if (bro->color == RED) {
bro->color = BLACK;
node->parent->color = RED;
rightRotate(node->parent);
bro = node->parent->left;
}
if (bro->left->color == BLACK && bro->right->color == BLACK) {
bro->color = RED;
node = node->parent;
} else {
if (bro->left->color == BLACK) {
bro->right->color = BLACK;
bro->color = RED;
leftRotate(bro);
bro = node->parent->left;
}
bro->color = node->parent->color;
node->parent->color = BLACK;
bro->left->color = BLACK;
rightRotate(node->parent);
node = root;
}
}
}
node->color = BLACK;
}
// 中序遍历红黑树
void inorderTraverse(Node* node) {
if (node == nullptr) return;
inorderTraverse(node->left);
cout << node->val << " ";
inorderTraverse(node->right);
}
};
```
代码实现了红黑树的插入、删除、查找和中序遍历操作。在插入和删除节点后会对红黑树进行修正以保持红黑树的性质。您可以根据需要进行测试和修改。
阅读全文