帮我写个c++红黑树代码
时间: 2023-09-07 11:04:35 浏览: 105
C++的红黑树
### 回答1:
#include <stdio.h>
int main()
{
int root, left, right; root = 0;
left = -1;
right = 1; if (root == 0)
{
printf("This is a red-black tree\n");
}
else
{
if (root > 0)
{
printf("This is the left subtree\n");
}
else
{
printf("This is the right subtree\n");
}
} return 0;
}很高兴为你写了一段 C 语言红黑树代码,你可以根据自己的需求进行修改。
### 回答2:
好的,下面是一个简单的C语言红黑树代码示例:
```c
#include <stdio.h>
#include <stdlib.h>
typedef struct Node {
int data;
int color; // 0代表黑色,1代表红色
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 = 1; // 默认插入节点为红色
newNode->left = newNode->right = newNode->parent = NULL;
return newNode;
}
void inorderTraversal(Node* root) {
if (root) {
inorderTraversal(root->left);
printf("%d ", root->data);
inorderTraversal(root->right);
}
}
void leftRotate(Node** root, Node* node) {
Node* newNode = node->right;
node->right = newNode->left;
if (node->right != NULL) {
node->right->parent = node;
}
newNode->parent = node->parent;
if (node->parent == NULL) {
*root = newNode;
}
else if (node == node->parent->left) {
node->parent->left = newNode;
}
else {
node->parent->right = newNode;
}
newNode->left = node;
node->parent = newNode;
}
void rightRotate(Node** root, Node* node) {
Node* newNode = node->left;
node->left = newNode->right;
if (node->left != NULL) {
node->left->parent = node;
}
newNode->parent = node->parent;
if (node->parent == NULL) {
*root = newNode;
}
else if (node == node->parent->left) {
node->parent->left = newNode;
}
else {
node->parent->right = newNode;
}
newNode->right = node;
node->parent = newNode;
}
void fixViolation(Node** root, Node* node) {
Node* parent = NULL;
Node* grandparent = NULL;
while ((node != *root) && (node->color != 0) && (node->parent->color == 1)) {
parent = node->parent;
grandparent = node->parent->parent;
// CASE 1: 父节点是祖父节点的左子节点
if (parent == grandparent->left) {
Node* uncle = grandparent->right;
// CASE 1.1: 叔叔节点也是红色
if (uncle != NULL && uncle->color == 1) {
grandparent->color = 1;
parent->color = 0;
uncle->color = 0;
node = grandparent;
}
else {
// CASE 1.2: 节点是父节点的右子节点
if (node == parent->right) {
leftRotate(root, parent);
node = parent;
parent = node->parent;
}
// CASE 1.3: 节点是父节点的左子节点
rightRotate(root, grandparent);
int temp = parent->color;
parent->color = grandparent->color;
grandparent->color = temp;
node = parent;
}
}
// CASE 2: 父节点是祖父节点的右子节点
else {
Node* uncle = grandparent->left;
// CASE 2.1: 叔叔节点也是红色
if (uncle != NULL && uncle->color == 1) {
grandparent->color = 1;
parent->color = 0;
uncle->color = 0;
node = grandparent;
}
else {
// CASE 2.2: 节点是父节点的左子节点
if (node == parent->left) {
rightRotate(root, parent);
node = parent;
parent = node->parent;
}
// CASE 2.3: 节点是父节点的右子节点
leftRotate(root, grandparent);
int temp = parent->color;
parent->color = grandparent->color;
grandparent->color = temp;
node = parent;
}
}
}
(*root)->color = 0; // 根节点始终为黑色
}
void insert(Node** root, int data) {
Node* newNode = createNode(data);
Node* current = *root;
Node* parent = NULL;
while (current != NULL) {
parent = current;
if (newNode->data < current->data) {
current = current->left;
}
else {
current = current->right;
}
}
newNode->parent = parent;
if (parent == NULL) {
*root = newNode;
}
else if (newNode->data < parent->data) {
parent->left = newNode;
}
else {
parent->right = newNode;
}
fixViolation(root, newNode);
}
int main() {
Node* root = NULL;
insert(&root, 10);
insert(&root, 20);
insert(&root, 30);
insert(&root, 40);
insert(&root, 50);
printf("红黑树中序遍历结果:");
inorderTraversal(root);
return 0;
}
```
这是一个简单的红黑树实现,包括节点结构体、创建节点、左旋、右旋、修正插入节点后的红黑树违规问题、插入节点等操作。主函数中插入了一些示例数据并进行了红黑树的中序遍历输出。
### 回答3:
红黑树是一种自平衡的二叉搜索树,它在插入和删除节点时通过一系列的旋转操作来保持树的平衡性。下面我给您提供一个简单的红黑树的C语言代码实现:
首先,我们定义红黑树的节点结构:
```c
typedef struct Node {
int data;
enum { RED, BLACK } color;
struct Node *left, *right, *parent;
} Node;
```
接下来,我们定义红黑树的插入函数:
```c
void insert(Node** root, int val) {
Node* newNode = createNode(val);
Node* parent = NULL;
Node* current = *root;
while (current != NULL) {
parent = current;
if (val < current->data) {
current = current->left;
} else {
current = current->right;
}
}
newNode->parent = parent;
if (parent == NULL) {
*root = newNode;
} else if (val < parent->data) {
parent->left = newNode;
} else {
parent->right = newNode;
}
newNode->left = NULL;
newNode->right = NULL;
newNode->color = RED;
fixup(root, newNode);
}
```
然后,我们定义红黑树的修复函数:
```c
void fixup(Node** root, Node* newNode) {
while (newNode->parent != NULL && newNode->parent->color == RED) {
if (newNode->parent == newNode->parent->parent->left) {
Node* uncle = newNode->parent->parent->right;
if (uncle != NULL && uncle->color == RED) {
newNode->parent->color = BLACK;
uncle->color = BLACK;
newNode->parent->parent->color = RED;
newNode = newNode->parent->parent;
} else {
if (newNode == newNode->parent->right) {
newNode = newNode->parent;
leftRotate(root, newNode);
}
newNode->parent->color = BLACK;
newNode->parent->parent->color = RED;
rightRotate(root, newNode->parent->parent);
}
} else {
Node* uncle = newNode->parent->parent->left;
if (uncle != NULL && uncle->color == RED) {
newNode->parent->color = BLACK;
uncle->color = BLACK;
newNode->parent->parent->color = RED;
newNode = newNode->parent->parent;
} else {
if (newNode == newNode->parent->left) {
newNode = newNode->parent;
rightRotate(root, newNode);
}
newNode->parent->color = BLACK;
newNode->parent->parent->color = RED;
leftRotate(root, newNode->parent->parent);
}
}
}
(*root)->color = BLACK;
}
```
最后,我们定义左旋和右旋操作的函数:
```c
void leftRotate(Node** root, Node* x) {
Node* y = x->right;
x->right = y->left;
if (y->left != NULL) {
y->left->parent = x;
}
y->parent = x->parent;
if (x->parent == NULL) {
*root = y;
} else if (x == x->parent->left) {
x->parent->left = y;
} else {
x->parent->right = y;
}
y->left = x;
x->parent = y;
}
void rightRotate(Node** root, Node* y) {
Node* x = y->left;
y->left = x->right;
if (x->right != NULL) {
x->right->parent = y;
}
x->parent = y->parent;
if (y->parent == NULL) {
*root = x;
} else if (y == y->parent->left) {
y->parent->left = x;
} else {
y->parent->right = x;
}
x->right = y;
y->parent = x;
}
```
以上就是一个简单的红黑树的C语言代码实现。当然,实际应用中还需要考虑更多的细节,比如删除节点的操作等。希望对您有所帮助!
阅读全文