运行C++写一个红黑树
时间: 2023-11-30 21:51:52 浏览: 93
抱歉,我是语言模型AI,不支持运行代码,但我可以给你提供一个基本的红黑树的实现。
```c
#include<stdio.h>
#include<stdlib.h>
#include<stdbool.h>
enum Color {RED, BLACK};
typedef struct Node {
int data;
enum Color color;
struct Node* left;
struct Node* right;
struct Node* parent;
} Node;
Node* createNode(int data) {
Node* newNode = (Node*)malloc(sizeof(Node));
newNode->data = data;
newNode->color = RED;
newNode->left = NULL;
newNode->right = NULL;
newNode->parent = NULL;
return newNode;
}
void rotateLeft(Node** root, Node* node) {
Node* right = node->right;
node->right = right->left;
if (node->right != NULL)
node->right->parent = node;
right->parent = node->parent;
if (node->parent == NULL)
*root = right;
else if (node == node->parent->left)
node->parent->left = right;
else
node->parent->right = right;
right->left = node;
node->parent = right;
}
void rotateRight(Node** root, Node* node) {
Node* left = node->left;
node->left = left->right;
if (node->left != NULL)
node->left->parent = node;
left->parent = node->parent;
if (node->parent == NULL)
*root = left;
else if (node == node->parent->left)
node->parent->left = left;
else
node->parent->right = left;
left->right = node;
node->parent = left;
}
void fixViolation(Node** root, Node* node) {
Node* parent = NULL;
Node* grandparent = NULL;
while ((node != *root) && (node->color != BLACK) && (node->parent->color == RED)) {
parent = node->parent;
grandparent = node->parent->parent;
if (parent == grandparent->left) {
Node* uncle = grandparent->right;
if ((uncle != NULL) && (uncle->color == RED)) {
grandparent->color = RED;
parent->color = BLACK;
uncle->color = BLACK;
node = grandparent;
} else {
if (node == parent->right) {
rotateLeft(root, parent);
node = parent;
parent = node->parent;
}
rotateRight(root, grandparent);
bool temp = parent->color;
parent->color = grandparent->color;
grandparent->color = temp;
node = parent;
}
} else {
Node* uncle = grandparent->left;
if ((uncle != NULL) && (uncle->color == RED)) {
grandparent->color = RED;
parent->color = BLACK;
uncle->color = BLACK;
node = grandparent;
} else {
if (node == parent->left) {
rotateRight(root, parent);
node = parent;
parent = node->parent;
}
rotateLeft(root, grandparent);
bool temp = parent->color;
parent->color = grandparent->color;
grandparent->color = temp;
node = parent;
}
}
}
(*root)->color = BLACK;
}
void insert(Node** root, int data) {
Node* newNode = createNode(data);
Node* current = *root;
Node* parent = NULL;
while (current != NULL) {
parent = current;
if (data < current->data)
current = current->left;
else
current = current->right;
}
newNode->parent = parent;
if (parent == NULL)
*root = newNode;
else if (data < parent->data)
parent->left = newNode;
else
parent->right = newNode;
fixViolation(root, newNode);
}
void inorder(Node* root) {
if (root == NULL)
return;
inorder(root->left);
printf("%d ", root->data);
inorder(root->right);
}
int main() {
Node* root = NULL;
int n, data;
printf("Enter number of nodes: ");
scanf("%d", &n);
for (int i = 0; i < n; i++) {
printf("Enter node data: ");
scanf("%d", &data);
insert(&root, data);
printf("Inorder traversal: ");
inorder(root);
printf("\n");
}
return 0;
}
```
这个实现中包含了红黑树的基本操作,如左旋、右旋和修复违规等。您可以将其保存为一个`.c`文件并使用C编译器进行编译和运行。
阅读全文
相关推荐
![rar](https://img-home.csdnimg.cn/images/20241231044955.png)
![7z](https://img-home.csdnimg.cn/images/20241231044736.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![pdf](https://img-home.csdnimg.cn/images/20241231044930.png)
![cpp](https://img-home.csdnimg.cn/images/20250102104920.png)
![application/x-rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![-](https://img-home.csdnimg.cn/images/20241231045053.png)
![-](https://img-home.csdnimg.cn/images/20241231044930.png)
![-](https://img-home.csdnimg.cn/images/20241231044955.png)
![-](https://img-home.csdnimg.cn/images/20241231045053.png)
![-](https://img-home.csdnimg.cn/images/20241231044955.png)
![-](https://img-home.csdnimg.cn/images/20241226111658.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)