写一个 C 语言的红黑树

时间: 2023-08-31 18:29:42 浏览: 54
### 回答1: 很高兴为你回答这个问题。C 语言的红黑树可以使用以下代码实现:#include <stdio.h> #include <stdlib.h>struct node { int data; char color; struct node *left, *right, *parent; };struct node *newNode(int data) { struct node *n = (struct node*)malloc(sizeof(struct node)); n->data = data; n->color = 'R'; n->left = NULL; n->right = NULL; n->parent = NULL; return n; }void insert(struct node **root, int data) { struct node *n = newNode(data); if (*root == NULL) { *root = n; } else { struct node *temp = *root; while (temp != NULL) { if (data > temp->data) { if (temp->right == NULL) { temp->right = n; n->parent = temp; break; } else { temp = temp->right; } } else { if (temp->left == NULL) { temp->left = n; n->parent = temp; break; } else { temp = temp->left; } } } } }void inorder(struct node *root) { if (root == NULL) return; inorder(root->left); printf("%d %c ", root->data, root->color); inorder(root->right); }int main() { struct node *root = NULL; insert(&root, 10); insert(&root, 5); insert(&root, 15); insert(&root, 3); insert(&root, 8); insert(&root, 20); inorder(root); return 0; } ### 回答2: 红黑树是一种自平衡的二叉查找树,它通过在每个节点上增加一个额外的位来存储节点的颜色,使得树在插入、删除操作时始终保持平衡。 在C语言中实现红黑树主要包括以下几个步骤: 1. 定义节点结构体: ```c typedef struct Node { int data; // 存储节点数据 struct Node* parent; // 指向父节点 struct Node* left; // 指向左孩子节点 struct Node* right; // 指向右孩子节点 int isRed; // 是否为红节点,0表示黑色,1表示红色 } Node; ``` 2. 定义红黑树结构体: ```c typedef struct RedBlackTree { Node* root; // 指向根节点 Node* nil; // 空节点的指针,表示叶子节点 } RedBlackTree; ``` 3. 实现红黑树的插入操作: ```c void insert(RedBlackTree* tree, int data) { Node* newNode = createNode(data); Node* x = tree->root; Node* y = tree->nil; while (x != tree->nil) { y = x; if (newNode->data < x->data) { x = x->left; } else { x = x->right; } } newNode->parent = y; if (y == tree->nil) { tree->root = newNode; } else if (newNode->data < y->data) { y->left = newNode; } else { y->right = newNode; } newNode->left = tree->nil; newNode->right = tree->nil; newNode->isRed = 1; insertFixup(tree, newNode); } void insertFixup(RedBlackTree* tree, Node* z) { while (z->parent->isRed) { if (z->parent == z->parent->parent->left) { Node* y = z->parent->parent->right; if (y->isRed) { z->parent->isRed = 0; y->isRed = 0; z->parent->parent->isRed = 1; z = z->parent->parent; } else { if (z == z->parent->right) { z = z->parent; leftRotate(tree, z); } z->parent->isRed = 0; z->parent->parent->isRed = 1; rightRotate(tree, z->parent->parent); } } else { // 类似左右情况的对称处理 } } tree->root->isRed = 0; } ``` 4. 实现红黑树的旋转操作: ```c void leftRotate(RedBlackTree* tree, Node* x) { Node* 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(RedBlackTree* tree, Node* x) { // 类似左旋的实现,进行对称处理 } ``` 以上只是红黑树的基本实现,还可以继续完善其他操作,如删除、搜索等。在实际应用中,红黑树被广泛应用于数据结构和算法中,以提高效率和性能。 ### 回答3: 红黑树(Red-Black Tree)是一种平衡二叉搜索树,它通过在每个节点上增加一个额外的存储位来表示节点的颜色,从而在维护了二叉搜索树特性的同时,也保持了树的平衡性。以下是用C语言实现红黑树的基本步骤: 1. 定义红黑树的结构体: ``` typedef struct Node { int key; int color; struct Node *left, *right, *parent; } Node; typedef struct RedBlackTree { Node *root; Node *nil; // 表示树中的空节点 } RedBlackTree; ``` 2. 实现红黑树的基本操作函数: - 初始化函数:创建一个空的红黑树并初始化nil节点 ``` RedBlackTree* createRedBlackTree() { RedBlackTree *tree = (RedBlackTree*)malloc(sizeof(RedBlackTree)); tree->nil = (Node*)malloc(sizeof(Node)); tree->nil->color = 0; // 空节点颜色为黑色 tree->root = tree->nil; // 初始状态下根节点指向nil节点 return tree; } ``` - 左旋操作: ``` void leftRotate(RedBlackTree *tree, Node *x) { Node *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(RedBlackTree *tree, Node *y) { Node *x = y->left; y->left = x->right; if (x->right != tree->nil) { x->right->parent = y; } x->parent = y->parent; if (y->parent == tree->nil) { tree->root = x; } else if (y == y->parent->left) { y->parent->left = x; } else { y->parent->right = x; } x->right = y; y->parent = x; } ``` - 插入操作: ``` void insert(RedBlackTree *tree, int key) { Node *z = (Node*)malloc(sizeof(Node)); z->key = key; Node *y = tree->nil; Node *x = tree->root; while (x != tree->nil) { y = x; if (z->key < x->key) { x = x->left; } else { x = x->right; } } z->parent = y; if (y == tree->nil) { tree->root = z; } else if (z->key < y->key) { y->left = z; } else { y->right = z; } z->left = tree->nil; z->right = tree->nil; z->color = 1; // 插入节点为红色 insertFixup(tree, z); } ``` - 插入操作修复函数: ``` void insertFixup(RedBlackTree *tree, Node *z) { while (z->parent->color == 1) { // 当前节点的父节点是红色 if (z->parent == z->parent->parent->left) { Node *y = z->parent->parent->right; // y是z的叔节点 if (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; leftRotate(tree, z); } z->parent->color = 0; z->parent->parent->color = 1; rightRotate(tree, z->parent->parent); } } else { // 对称操作,左右调换即可 } } tree->root->color = 0; // 根节点为黑色 } ``` 以上是红黑树的基本操作实现,包括了初始化函数、左旋和右旋操作、插入操作以及相应的修复函数。根据这些基本操作,可以实现其他常用的红黑树操作,如删除节点、搜索节点等。

相关推荐

最新推荐

recommend-type

微信小程序-番茄时钟源码

微信小程序番茄时钟的源码,支持进一步的修改。番茄钟,指的是把工作任务分解成半小时左右,集中精力工作25分钟后休息5分钟,如此视作种一个“番茄”,而“番茄工作法”的流程能使下一个30分钟更有动力。
recommend-type

激光雷达专题研究:迈向高阶智能化关键,前瞻布局把握行业脉搏.pdf

电子元件 电子行业 行业分析 数据分析 数据报告 行业报告
recommend-type

安享智慧理财测试项目Mock服务代码

安享智慧理财测试项目Mock服务代码
recommend-type

课程设计 基于SparkMLlib的ALS算法的电影推荐系统源码+详细文档+全部数据齐全.zip

【资源说明】 课程设计 基于SparkMLlib的ALS算法的电影推荐系统源码+详细文档+全部数据齐全.zip课程设计 基于SparkMLlib的ALS算法的电影推荐系统源码+详细文档+全部数据齐全.zip 【备注】 1、该项目是高分毕业设计项目源码,已获导师指导认可通过,答辩评审分达到95分 2、该资源内项目代码都经过测试运行成功,功能ok的情况下才上传的,请放心下载使用! 3、本项目适合计算机相关专业(如软件工程、计科、人工智能、通信工程、自动化、电子信息等)的在校学生、老师或者企业员工下载使用,也可作为毕业设计、课程设计、作业、项目初期立项演示等,当然也适合小白学习进阶。 4、如果基础还行,可以在此代码基础上进行修改,以实现其他功能,也可直接用于毕设、课设、作业等。 欢迎下载,沟通交流,互相学习,共同进步!
recommend-type

华中科技大学电信专业 课程资料 作业 代码 实验报告-雷达与信息对抗-内含源码和说明书.zip

华中科技大学电信专业 课程资料 作业 代码 实验报告-雷达与信息对抗-内含源码和说明书.zip
recommend-type

zigbee-cluster-library-specification

最新的zigbee-cluster-library-specification说明文档。
recommend-type

管理建模和仿真的文件

管理Boualem Benatallah引用此版本:布阿利姆·贝纳塔拉。管理建模和仿真。约瑟夫-傅立叶大学-格勒诺布尔第一大学,1996年。法语。NNT:电话:00345357HAL ID:电话:00345357https://theses.hal.science/tel-003453572008年12月9日提交HAL是一个多学科的开放存取档案馆,用于存放和传播科学研究论文,无论它们是否被公开。论文可以来自法国或国外的教学和研究机构,也可以来自公共或私人研究中心。L’archive ouverte pluridisciplinaire
recommend-type

实现实时数据湖架构:Kafka与Hive集成

![实现实时数据湖架构:Kafka与Hive集成](https://img-blog.csdnimg.cn/img_convert/10eb2e6972b3b6086286fc64c0b3ee41.jpeg) # 1. 实时数据湖架构概述** 实时数据湖是一种现代数据管理架构,它允许企业以低延迟的方式收集、存储和处理大量数据。与传统数据仓库不同,实时数据湖不依赖于预先定义的模式,而是采用灵活的架构,可以处理各种数据类型和格式。这种架构为企业提供了以下优势: - **实时洞察:**实时数据湖允许企业访问最新的数据,从而做出更明智的决策。 - **数据民主化:**实时数据湖使各种利益相关者都可
recommend-type

解释minorization-maximization (MM) algorithm,并给出matlab代码编写的例子

Minorization-maximization (MM) algorithm是一种常用的优化算法,用于求解非凸问题或含有约束的优化问题。该算法的基本思想是通过构造一个凸下界函数来逼近原问题,然后通过求解凸下界函数的最优解来逼近原问题的最优解。具体步骤如下: 1. 初始化参数 $\theta_0$,设 $k=0$; 2. 构造一个凸下界函数 $Q(\theta|\theta_k)$,使其满足 $Q(\theta_k|\theta_k)=f(\theta_k)$; 3. 求解 $Q(\theta|\theta_k)$ 的最优值 $\theta_{k+1}=\arg\min_\theta Q(
recommend-type

JSBSim Reference Manual

JSBSim参考手册,其中包含JSBSim简介,JSBSim配置文件xml的编写语法,编程手册以及一些应用实例等。其中有部分内容还没有写完,估计有生之年很难看到完整版了,但是内容还是很有参考价值的。