给出红黑树的C++代码实现
时间: 2023-10-17 09:25:27 浏览: 93
下面是一个用 C 实现的红黑树:
```c
#include <stdio.h>
#include <stdlib.h>
typedef enum Color {
RED, BLACK
} Color;
typedef struct Node {
int value;
Color color;
struct Node *left;
struct Node *right;
struct Node *parent;
} Node;
Node *root = NULL;
Node *createNode(int value, Color color) {
Node *newNode = (Node *) malloc(sizeof(Node));
newNode->value = value;
newNode->color = color;
newNode->left = NULL;
newNode->right = NULL;
newNode->parent = NULL;
return newNode;
}
Node *findSibling(Node *node) {
Node *parent = node->parent;
if (parent == NULL) {
return NULL;
}
if (parent->left == node) {
return parent->right;
} else {
return parent->left;
}
}
Color getNodeColor(Node *node) {
if (node == NULL) {
return BLACK;
} else {
return node->color;
}
}
Node *leftRotate(Node *node) {
Node *pivot = node->right;
node->right = pivot->left;
if (pivot->left != NULL) {
pivot->left->parent = node;
}
pivot->parent = node->parent;
if (node->parent == NULL) {
root = pivot;
} else if (node == node->parent->left) {
node->parent->left = pivot;
} else {
node->parent->right = pivot;
}
pivot->left = node;
node->parent = pivot;
return pivot;
}
Node *rightRotate(Node *node) {
Node *pivot = node->left;
node->left = pivot->right;
if (pivot->right != NULL) {
pivot->right->parent = node;
}
pivot->parent = node->parent;
if (node->parent == NULL) {
root = pivot;
} else if (node == node->parent->left) {
node->parent->left = pivot;
} else {
node->parent->right = pivot;
}
pivot->right = node;
node->parent = pivot;
return pivot;
}
void fixInsert(Node *node) {
while (node->parent != NULL && node->parent->color == RED) {
Node *parent = node->parent;
Node *grandparent = parent->parent;
Node *uncle = findSibling(parent);
if (getNodeColor(uncle) == RED) {
parent->color = BLACK;
uncle->color = BLACK;
grandparent->color = RED;
node = grandparent;
}
else {
if (parent == grandparent->left) {
if (node == parent->right) {
node = leftRotate(parent);
parent = node->parent;
}
rightRotate(grandparent);
}
else {
if (node == parent->left) {
node = rightRotate(parent);
parent = node->parent;
}
leftRotate(grandparent);
}
parent->color = BLACK;
grandparent->color = RED;
}
}
root->color = BLACK;
}
void insertNode(int value) {
Node *newNode = createNode(value, RED);
if (root == NULL) {
root = newNode;
} else {
Node *current = root;
Node *parent = NULL;
while (current != NULL) {
parent = current;
if (value < current->value) {
current = current->left;
} else {
current = current->right;
}
}
newNode->parent = parent;
if (value < parent->value) {
parent->left = newNode;
} else {
parent->right = newNode;
}
fixInsert(newNode);
}
}
void printTree(Node *node, int space) {
if (node == NULL) {
return;
}
space += 10;
printTree(node->right, space);
printf("\n");
for (int i = 10; i < space; i++) {
printf(" ");
}
printf("%d (%s)\n", node->value, node->color == RED ? "RED" : "BLACK");
printTree(node->left, space);
}
int main() {
insertNode(10);
insertNode(20);
insertNode(30);
insertNode(40);
insertNode(50);
insertNode(60);
insertNode(70);
printTree(root, 0);
return 0;
}
```
这个简单的红黑树实现包含了插入节点和打印红黑树的功能。它基于二叉搜索树的规则插入节点,并通过旋转和重新着色来保持红黑树的平衡。在插入节点后,通过 fixInsert 函数来调整红黑树。打印红黑树的功能使用递归方式实现。你可以运行这个代码来检查红黑树的输出。
阅读全文