用C语言写一个红黑树的前中后序排列
时间: 2023-06-13 09:04:54 浏览: 53
红黑树是一种自平衡二叉查找树,它的前序、中序以及后序遍历与普通的二叉查找树遍历方式相同。下面是使用C语言实现红黑树的前序、中序以及后序遍历的代码。
```c
#include <stdio.h>
#include <stdlib.h>
typedef struct rb_node {
int key;
int color;
struct rb_node *left, *right, *parent;
} rb_node;
void left_rotate(rb_node **root, rb_node *x) {
rb_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 right_rotate(rb_node **root, rb_node *y) {
rb_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;
}
void rb_insert_fixup(rb_node **root, rb_node *z) {
while (z->parent != NULL && z->parent->color == 1) {
if (z->parent == z->parent->parent->left) {
rb_node *y = z->parent->parent->right;
if (y != NULL && y->color == 1) {
z->parent->color = 0;
y->color = 0;
z->parent->parent->color = 1;
z = z->parent->parent;
} else {
if (z == z->parent->right) {
z = z->parent;
left_rotate(root, z);
}
z->parent->color = 0;
z->parent->parent->color = 1;
right_rotate(root, z->parent->parent);
}
} else {
rb_node *y = z->parent->parent->left;
if (y != NULL && y->color == 1) {
z->parent->color = 0;
y->color = 0;
z->parent->parent->color = 1;
z = z->parent->parent;
} else {
if (z == z->parent->left) {
z = z->parent;
right_rotate(root, z);
}
z->parent->color = 0;
z->parent->parent->color = 1;
left_rotate(root, z->parent->parent);
}
}
}
(*root)->color = 0;
}
void rb_insert(rb_node **root, int key) {
rb_node *z = (rb_node*) malloc(sizeof(rb_node));
z->key = key;
z->color = 1;
z->left = NULL;
z->right = NULL;
z->parent = NULL;
rb_node *y = NULL;
rb_node *x = *root;
while (x != NULL) {
y = x;
if (z->key < x->key) x = x->left;
else x = x->right;
}
z->parent = y;
if (y == NULL) *root = z;
else if (z->key < y->key) y->left = z;
else y->right = z;
rb_insert_fixup(root, z);
}
void inorder(rb_node *root) {
if (root != NULL) {
inorder(root->left);
printf("%d ", root->key);
inorder(root->right);
}
}
void preorder(rb_node *root) {
if (root != NULL) {
printf("%d ", root->key);
preorder(root->left);
preorder(root->right);
}
}
void postorder(rb_node *root) {
if (root != NULL) {
postorder(root->left);
postorder(root->right);
printf("%d ", root->key);
}
}
int main() {
rb_node *root = NULL;
rb_insert(&root, 10);
rb_insert(&root, 20);
rb_insert(&root, 30);
rb_insert(&root, 100);
rb_insert(&root, 90);
rb_insert(&root, 40);
rb_insert(&root, 50);
rb_insert(&root, 60);
printf("Inorder Traversal: ");
inorder(root);
printf("\n");
printf("Preorder Traversal: ");
preorder(root);
printf("\n");
printf("Postorder Traversal: ");
postorder(root);
printf("\n");
return 0;
}
```
以上代码包含了红黑树的插入操作和三种不同的遍历方式的实现。在代码中,`rb_insert()` 函数用于插入新节点,`inorder()` 函数、`preorder()` 函数和 `postorder()` 函数分别用于实现中序、前序和后序遍历。