C语言编写红黑树构建算法,中序遍历各节点,输出颜色和值
时间: 2024-02-05 21:45:38 浏览: 76
红黑树的c语言代码实现
好的,下面是C语言编写红黑树构建算法,以及中序遍历各节点并输出颜色和值的代码:
```c
#include <stdio.h>
#include <stdlib.h>
// 定义红黑树的颜色
#define RED 0
#define BLACK 1
// 定义红黑树节点
typedef struct Node {
int value; // 节点值
int color; // 节点颜色
struct Node *left; // 左子节点
struct Node *right; // 右子节点
struct Node *parent;// 父节点
} Node;
// 定义红黑树
typedef struct {
Node *root; // 根节点
} RedBlackTree;
// 定义函数:中序遍历红黑树
void inorderTraversal(Node *node) {
if (node == NULL) {
return;
}
inorderTraversal(node->left);
printf("Value: %d, Color: %s\n", node->value, node->color == RED ? "RED" : "BLACK");
inorderTraversal(node->right);
}
// 定义函数:左旋
void leftRotate(RedBlackTree *tree, Node *node) {
Node *right = node->right;
node->right = right->left;
if (right->left != NULL) {
right->left->parent = node;
}
right->parent = node->parent;
if (node->parent == NULL) {
tree->root = right;
} else if (node == node->parent->left) {
node->parent->left = right;
} else {
node->parent->right = right;
}
right->left = node;
node->parent = right;
}
// 定义函数:右旋
void rightRotate(RedBlackTree *tree, Node *node) {
Node *left = node->left;
node->left = left->right;
if (left->right != NULL) {
left->right->parent = node;
}
left->parent = node->parent;
if (node->parent == NULL) {
tree->root = left;
} else if (node == node->parent->right) {
node->parent->right = left;
} else {
node->parent->left = left;
}
left->right = node;
node->parent = left;
}
// 定义函数:插入节点
void insertNode(RedBlackTree *tree, Node *node) {
Node *y = NULL;
Node *x = tree->root;
while (x != NULL) {
y = x;
if (node->value < x->value) {
x = x->left;
} else {
x = x->right;
}
}
node->parent = y;
if (y == NULL) {
tree->root = node;
} else if (node->value < y->value) {
y->left = node;
} else {
y->right = node;
}
node->left = NULL;
node->right = NULL;
node->color = RED;
while (node != tree->root && node->parent->color == RED) {
if (node->parent == node->parent->parent->left) {
Node *uncle = node->parent->parent->right;
if (uncle != NULL && 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(tree, node);
}
node->parent->color = BLACK;
node->parent->parent->color = RED;
rightRotate(tree, node->parent->parent);
}
} else {
Node *uncle = node->parent->parent->left;
if (uncle != NULL && 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(tree, node);
}
node->parent->color = BLACK;
node->parent->parent->color = RED;
leftRotate(tree, node->parent->parent);
}
}
}
tree->root->color = BLACK;
}
int main() {
RedBlackTree *tree = (RedBlackTree *)malloc(sizeof(RedBlackTree));
tree->root = NULL;
insertNode(tree, &(Node){10, BLACK, NULL, NULL, NULL});
insertNode(tree, &(Node){20, BLACK, NULL, NULL, NULL});
insertNode(tree, &(Node){30, BLACK, NULL, NULL, NULL});
insertNode(tree, &(Node){40, BLACK, NULL, NULL, NULL});
insertNode(tree, &(Node){50, BLACK, NULL, NULL, NULL});
inorderTraversal(tree->root);
return 0;
}
```
以上代码中,我们使用了 `typedef` 定义了红黑树节点 `Node` 和红黑树 `RedBlackTree`,然后定义了中序遍历红黑树的函数 `inorderTraversal`,以及左旋和右旋的函数 `leftRotate` 和 `rightRotate`,最后定义了插入节点的函数 `insertNode`。
在 `main` 函数中,我们创建了一个红黑树,并插入了五个节点。最后调用 `inorderTraversal` 函数中序遍历红黑树,输出各节点的颜色和值。
阅读全文