请用c语言写一段红黑树的插入
时间: 2023-08-31 21:34:10 浏览: 106
### 回答1:
我可以为您提供一段c语言代码,用于插入红黑树:/* Function to insert a new node with given data in RB Tree */
struct node* insert(struct node* root, int data)
{
// Allocate memory for new node
struct node* newnode = (struct node*)malloc(sizeof(struct node));
// Assign data to this node
newnode->data = data;
// Initialize left and right children as NULL
newnode->left = NULL;
newnode->right = NULL;
newnode->color = 1;
// If root is NULL, return new node
if (root == NULL)
return newnode;
// Traverse the tree and find a place to insert new node
struct node* temp = root;
struct node* parent = NULL;
while (temp != NULL)
{
parent = temp;
if (data < temp->data)
temp = temp->left;
else
temp = temp->right;
}
// Make the appropriate links
if (data < parent->data)
parent->left = newnode;
else
parent->right = newnode;
// Return root of the tree
return root;
}
### 回答2:
要使用C语言写一段红黑树的插入,可以参考下面的代码。
```c
#include <stdio.h>
#include <stdlib.h>
typedef struct Node {
int key;
int color; // 0为黑色,1为红色
struct Node* left;
struct Node* right;
struct Node* parent;
} Node;
Node* root = NULL; // 红黑树的根节点
// 创建一个新节点
Node* createNode(int key) {
Node* node = (Node*)malloc(sizeof(Node));
node->key = key;
node->color = 1; // 新插入的节点默认为红色
node->left = NULL;
node->right = NULL;
node->parent = NULL;
return node;
}
// 左旋操作
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* 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;
}
// 插入节点修正红黑树性质
void fixInsert(Node* node) {
while (node != root && node->parent->color == 1) {
if (node->parent == node->parent->parent->left) {
Node* y = node->parent->parent->right;
if (y != NULL && y->color == 1) {
node->parent->color = 0;
y->color = 0;
node->parent->parent->color = 1;
node = node->parent->parent;
} else {
if (node == node->parent->right) {
node = node->parent;
leftRotate(node);
}
node->parent->color = 0;
node->parent->parent->color = 1;
rightRotate(node->parent->parent);
}
} else {
Node* y = node->parent->parent->left;
if (y != NULL && y->color == 1) {
node->parent->color = 0;
y->color = 0;
node->parent->parent->color = 1;
node = node->parent->parent;
} else {
if (node == node->parent->left) {
node = node->parent;
rightRotate(node);
}
node->parent->color = 0;
node->parent->parent->color = 1;
leftRotate(node->parent->parent);
}
}
}
root->color = 0;
}
// 将节点插入红黑树
void insert(int key) {
Node* node = createNode(key);
Node* y = NULL;
Node* x = root;
while (x != NULL) {
y = x;
if (node->key < x->key) {
x = x->left;
} else {
x = x->right;
}
}
node->parent = y;
if (y == NULL) {
root = node;
} else if (node->key < y->key) {
y->left = node;
} else {
y->right = node;
}
fixInsert(node);
}
// 前序遍历红黑树
void preOrder(Node* node) {
if (node != NULL) {
printf("%d ", node->key);
preOrder(node->left);
preOrder(node->right);
}
}
int main() {
insert(10);
insert(20);
insert(30);
insert(15);
insert(18);
printf("前序遍历红黑树:\n");
preOrder(root);
return 0;
}
```
上述代码实现了简单的红黑树插入功能。在插入节点后,使用`fixInsert()`函数来修正红黑树的性质,保证整棵树仍然是一颗合法的红黑树。最后,打印红黑树的前序遍历结果。
阅读全文