我想要一份红黑树的c++实现代码
时间: 2023-04-06 21:04:51 浏览: 113
红黑树C++代码实现
4星 · 用户满意度95%
我可以为您提供一份红黑树的C实现代码,以下是代码:
```
//红黑树节点颜色
#define RED 0
#define BLACK 1
//红黑树节点结构体
typedef struct rb_node {
int key; //键值
int color; //颜色
struct rb_node *left; //左子节点
struct rb_node *right; //右子节点
struct rb_node *parent; //父节点
} rb_node;
//红黑树结构体
typedef struct rb_tree {
rb_node *root; //根节点
} rb_tree;
//初始化红黑树
void init_rb_tree(rb_tree *tree) {
tree->root = NULL;
}
//创建红黑树节点
rb_node *create_rb_node(int key) {
rb_node *node = (rb_node *)malloc(sizeof(rb_node));
node->key = key;
node->color = RED;
node->left = NULL;
node->right = NULL;
node->parent = NULL;
return node;
}
//左旋
void left_rotate(rb_tree *tree, rb_node *node) {
rb_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 right_rotate(rb_tree *tree, rb_node *node) {
rb_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->left) {
node->parent->left = left;
} else {
node->parent->right = left;
}
left->right = node;
node->parent = left;
}
//插入红黑树
void insert_rb_tree(rb_tree *tree, int key) {
rb_node *node = create_rb_node(key);
rb_node *parent = NULL;
rb_node *current = tree->root;
while (current != NULL) {
parent = current;
if (key < current->key) {
current = current->left;
} else {
current = current->right;
}
}
node->parent = parent;
if (parent == NULL) {
tree->root = node;
} else if (key < parent->key) {
parent->left = node;
} else {
parent->right = node;
}
while (node != tree->root && node->parent->color == RED) {
if (node->parent == node->parent->parent->left) {
rb_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;
left_rotate(tree, node);
}
node->parent->color = BLACK;
node->parent->parent->color = RED;
right_rotate(tree, node->parent->parent);
}
} else {
rb_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;
right_rotate(tree, node);
}
node->parent->color = BLACK;
node->parent->parent->color = RED;
left_rotate(tree, node->parent->parent);
}
}
}
tree->root->color = BLACK;
}
//中序遍历红黑树
void inorder_rb_tree(rb_node *node) {
if (node != NULL) {
inorder_rb_tree(node->left);
printf("%d ", node->key);
inorder_rb_tree(node->right);
}
}
//测试红黑树
int main() {
rb_tree tree;
init_rb_tree(&tree);
insert_rb_tree(&tree, 10);
insert_rb_tree(&tree, 20);
insert_rb_tree(&tree, 30);
insert_rb_tree(&tree, 15);
insert_rb_tree(&tree, 18);
insert_rb_tree(&tree, 25);
insert_rb_tree(&tree, 35);
inorder_rb_tree(tree.root);
return 0;
}
```
阅读全文