使用C++实现红黑树及其基本操作代码,再给出使用例子
时间: 2023-09-07 18:05:22 浏览: 132
### 回答1:
答:红黑树是一种自平衡二叉搜索树,它能够确保任何一棵树的高度不会超过O(log n)。下面是使用C语言实现红黑树的基本操作的代码:// 插入节点
void insertNode(Node *&root, int key)
{
// 创建新的节点
Node *z = new Node;
z->key = key;
// 将z的颜色设置为红色
z->color = RED;
// 将z的左右子节点设置为空
z->left = z->right = NULL;
// 将z的父节点设置为空
z->parent = NULL;
// 如果根节点为空
if (root == NULL)
root = z;
// 否则,找到合适的插入位置
else
{
Node *y = NULL;
Node *x = root;
// 遍历树,找到合适的插入位置
while (x != NULL)
{
y = x;
if (z->key < x->key)
x = x->left;
else
x = x->right;
}
// 找到了位置,将z插入此位置
z->parent = y;
if (z->key < y->key)
y->left = z;
else
y->right = z;
}
// 将z的颜色设置为红色
z->color = RED;
// 修正红黑树
insertFixUp(z);
}// 使用例子:
int main()
{
Node *root = NULL;
// 插入节点
insertNode(root, 10);
insertNode(root, 20);
insertNode(root, 30);
insertNode(root, 40);
insertNode(root, 50);
return 0;
}
### 回答2:
红黑树是一种自平衡的二叉查找树,它的结构特点使得它能够在O(log n)的时间复杂度下进行插入、删除和查找操作。下面是使用C语言实现红黑树及其基本操作的代码:
```c
#include <stdio.h>
#include <stdlib.h>
typedef enum { RED, BLACK } Color;
typedef struct Node {
int data;
Color color;
struct Node* parent;
struct Node* left;
struct Node* right;
} Node;
Node* root = NULL;
// 创建新节点
Node* createNode(int data) {
Node* newNode = (Node*)malloc(sizeof(Node));
newNode->data = data;
newNode->color = RED;
newNode->parent = NULL;
newNode->left = NULL;
newNode->right = NULL;
return newNode;
}
// 左旋操作
void leftRotate(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* x) {
Node* y = x->left;
x->left = y->right;
if (y->right != NULL)
y->right->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->right = x;
x->parent = y;
}
// 新节点插入后的修正操作
void fixInsert(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(newNode);
}
newNode->parent->color = BLACK;
newNode->parent->parent->color = RED;
rightRotate(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(newNode);
}
newNode->parent->color = BLACK;
newNode->parent->parent->color = RED;
leftRotate(newNode->parent->parent);
}
}
}
root->color = BLACK;
}
// 插入操作
void insert(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;
fixInsert(newNode);
}
// 中序遍历
void inorder(Node* node) {
if (node == NULL)
return;
inorder(node->left);
printf("%d ", node->data);
inorder(node->right);
}
int main() {
insert(10);
insert(20);
insert(30);
insert(15);
insert(5);
printf("红黑树中序遍历结果:");
inorder(root);
return 0;
}
```
上述代码实现了红黑树的插入操作,并提供了一个简单的使用例子。在主函数中,先后插入了10、20、30、15和5这5个节点,然后进行中序遍历输出红黑树的遍历结果。运行上述代码,输出结果为:红黑树中序遍历结果:5 10 15 20 30。
### 回答3:
红黑树是一种自平衡的二叉搜索树,它在插入和删除操作后能够自动调整树的结构,以保持树的平衡性。以下是使用C语言实现红黑树及其基本操作的代码。
```c
#include <stdio.h>
#include <stdlib.h>
typedef enum {RED, BLACK} Color;
typedef struct RBNode {
int data;
enum Color color;
struct RBNode *left, *right, *parent;
} RBNode;
typedef struct RBTree {
struct RBNode *root;
struct RBNode *nil;
} RBTree;
// 创建红黑树节点
RBNode* createNode(int data) {
RBNode* newNode = (RBNode*) malloc(sizeof(RBNode));
newNode->data = data;
newNode->color = RED;
newNode->left = NULL;
newNode->right = NULL;
newNode->parent = NULL;
return newNode;
}
// 初始化红黑树
RBTree* initTree() {
RBTree* newTree = (RBTree*) malloc(sizeof(RBTree));
newTree->nil = createNode(0);
newTree->nil->color = BLACK;
newTree->root = newTree->nil;
return newTree;
}
// 左旋
void leftRotate(RBTree* tree, RBNode* x) {
RBNode* y = x->right;
x->right = y->left;
if (y->left != tree->nil) {
y->left->parent = x;
}
y->parent = x->parent;
if (x->parent == tree->nil) {
tree->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(RBTree* tree, RBNode* x) {
RBNode* y = x->left;
x->left = y->right;
if (y->right != tree->nil) {
y->right->parent = x;
}
y->parent = x->parent;
if (x->parent == tree->nil) {
tree->root = y;
} else if (x == x->parent->right) {
x->parent->right = y;
} else {
x->parent->left = y;
}
y->right = x;
x->parent = y;
}
// 插入节点修正
void insertFixup(RBTree* tree, RBNode* z) {
while (z->parent->color == RED) {
if (z->parent == z->parent->parent->left) {
RBNode* y = z->parent->parent->right;
if (y->color == RED) {
z->parent->color = BLACK;
y->color = BLACK;
z->parent->parent->color = RED;
z = z->parent->parent;
} else {
if (z == z->parent->right) {
z = z->parent;
leftRotate(tree, z);
}
z->parent->color = BLACK;
z->parent->parent->color = RED;
rightRotate(tree, z->parent->parent);
}
} else {
RBNode* y = z->parent->parent->left;
if (y->color == RED) {
z->parent->color = BLACK;
y->color = BLACK;
z->parent->parent->color = RED;
z = z->parent->parent;
} else {
if (z == z->parent->left) {
z = z->parent;
rightRotate(tree, z);
}
z->parent->color = BLACK;
z->parent->parent->color = RED;
leftRotate(tree, z->parent->parent);
}
}
}
tree->root->color = BLACK;
}
// 插入节点
void insertNode(RBTree* tree, int data) {
RBNode* z = createNode(data);
RBNode* y = tree->nil;
RBNode* x = tree->root;
while (x != tree->nil) {
y = x;
if (z->data < x->data) {
x = x->left;
} else {
x = x->right;
}
}
z->parent = y;
if (y == tree->nil) {
tree->root = z;
} else if (z->data < y->data) {
y->left = z;
} else {
y->right = z;
}
z->left = tree->nil;
z->right = tree->nil;
z->color = RED;
insertFixup(tree, z);
}
// 中序遍历红黑树
void inorder(RBTree* tree, RBNode* node) {
if (node != tree->nil) {
inorder(tree, node->left);
printf("%d ", node->data);
if (node->color == RED) {
printf("(Red) ");
} else {
printf("(Black) ");
}
inorder(tree, node->right);
}
}
int main() {
RBTree* tree = initTree();
insertNode(tree, 4);
insertNode(tree, 1);
insertNode(tree, 3);
insertNode(tree, 2);
insertNode(tree, 6);
insertNode(tree, 5);
insertNode(tree, 7);
printf("中序遍历红黑树:");
inorder(tree, tree->root);
return 0;
}
```
上述代码中,我们首先定义了红黑树节点的数据结构RBNode和红黑树的数据结构RBTree。然后,实现了创建节点、初始化树、左旋、右旋、插入节点修正和插入节点等基本操作。
在主函数中,我们初始化了红黑树,然后依次插入了节点4、1、3、2、6、5和7。最后,我们通过中序遍历函数inorder输出了红黑树的节点数据和颜色信息。
运行结果为:中序遍历红黑树:1 (Black) 2 (Black) 3 (Red) 4 (Black) 5 (Red) 6 (Black) 7 (Red)
以上就是使用C语言实现红黑树及其基本操作的代码,以及一个使用例子。
阅读全文