C语言用字符串做红黑树

时间: 2024-02-05 09:39:55 浏览: 24
红黑树是一种二叉搜索树,它有以下特点: 1. 每个节点是红色或黑色。 2. 根节点是黑色。 3. 每个叶子节点(NIL节点,空节点)是黑色的。 4. 如果一个节点是红色的,则它的子节点必须是黑色的。 5. 从任意一个节点到其每个叶子节点的所有路径都包含相同数目的黑色节点。 6. 在插入和删除节点时,必须保持以上五条性质不变。 对于C语言中的字符串,我们需要使用一个比较函数来比较两个字符串的大小。比较函数可以使用strcmp函数,该函数返回值为0表示两个字符串相等,小于0表示字符串1小于字符串2,大于0表示字符串1大于字符串2。 下面是使用C语言字符串实现红黑树的示例代码: ``` #include <stdio.h> #include <stdlib.h> #include <string.h> typedef struct node { char *key; int value; int color; // 0 for black, 1 for red struct node *left; struct node *right; struct node *parent; } Node; Node *root = NULL; Node *create_node(char *key, int value) { Node *new_node = (Node *) malloc(sizeof(Node)); new_node->key = key; new_node->value = value; new_node->color = 1; // new node is always red new_node->left = NULL; new_node->right = NULL; new_node->parent = NULL; return new_node; } void rotate_left(Node *node) { Node *right_child = node->right; node->right = right_child->left; if (right_child->left != NULL) { right_child->left->parent = node; } right_child->parent = node->parent; if (node->parent == NULL) { root = right_child; } else if (node == node->parent->left) { node->parent->left = right_child; } else { node->parent->right = right_child; } right_child->left = node; node->parent = right_child; } void rotate_right(Node *node) { Node *left_child = node->left; node->left = left_child->right; if (left_child->right != NULL) { left_child->right->parent = node; } left_child->parent = node->parent; if (node->parent == NULL) { root = left_child; } else if (node == node->parent->left) { node->parent->left = left_child; } else { node->parent->right = left_child; } left_child->right = node; node->parent = left_child; } void insert_fixup(Node *node) { while (node != root && node->parent->color == 1) { if (node->parent == node->parent->parent->left) { Node *uncle = node->parent->parent->right; if (uncle != NULL && uncle->color == 1) { node->parent->color = 0; uncle->color = 0; node->parent->parent->color = 1; node = node->parent->parent; } else { if (node == node->parent->right) { node = node->parent; rotate_left(node); } node->parent->color = 0; node->parent->parent->color = 1; rotate_right(node->parent->parent); } } else { Node *uncle = node->parent->parent->left; if (uncle != NULL && uncle->color == 1) { node->parent->color = 0; uncle->color = 0; node->parent->parent->color = 1; node = node->parent->parent; } else { if (node == node->parent->left) { node = node->parent; rotate_right(node); } node->parent->color = 0; node->parent->parent->color = 1; rotate_left(node->parent->parent); } } } root->color = 0; } void insert_node(char *key, int value) { Node *new_node = create_node(key, value); Node *parent = NULL; Node *current = root; while (current != NULL) { parent = current; if (strcmp(new_node->key, current->key) < 0) { current = current->left; } else { current = current->right; } } new_node->parent = parent; if (parent == NULL) { root = new_node; } else if (strcmp(new_node->key, parent->key) < 0) { parent->left = new_node; } else { parent->right = new_node; } insert_fixup(new_node); } void inorder_traversal(Node *node) { if (node != NULL) { inorder_traversal(node->left); printf("%s: %d\n", node->key, node->value); inorder_traversal(node->right); } } int main() { insert_node("c", 3); insert_node("a", 1); insert_node("b", 2); insert_node("e", 5); insert_node("d", 4); inorder_traversal(root); return 0; } ``` 该代码使用了字符串作为红黑树的键,并使用strcmp函数来比较两个字符串的大小。在插入节点时,使用insert_fixup函数来保持红黑树的性质不变。最后,使用inorder_traversal函数来遍历红黑树并输出节点的键和值。

相关推荐

最新推荐

recommend-type

C语言实现将字符串转换为数字的方法

主要介绍了C语言实现将字符串转换为数字的方法,涉及系统函数atoi()函数的使用技巧,需要的朋友可以参考下
recommend-type

C语言字符串转换为Python字符串的方法

主要介绍了C语言字符串转换为Python字符串的方法,文中讲解非常细致,代码帮助大家更好的理解和学习,感兴趣的朋友可以了解下
recommend-type

C语言之字符串模糊查询方法的实现

本篇文章主要为大家介绍字符串模糊查询的C语言程序编写方法,有需要的朋友可以参考下
recommend-type

C语言之字符串典型例题解析

又遇见几个好题,和以前的一些凑一块写一篇文章,作为我延迟...  10 将s所指字符串复制到字符串t中  题目有四个选项,为什么我会选这个题呢,因为自己在面试C++的岗位中第一次遇见,印象里好像是写错了,然后在练习
recommend-type

C语言字符串快速压缩算法代码

主要介绍了C语言字符串快速压缩算法代码,将字符串中连续出席的重复字母进行压缩,其主要的压缩字段的格式为”字符重复的次数+字符”。有需要的小伙伴参考下吧。
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

2. 通过python绘制y=e-xsin(2πx)图像

可以使用matplotlib库来绘制这个函数的图像。以下是一段示例代码: ```python import numpy as np import matplotlib.pyplot as plt def func(x): return np.exp(-x) * np.sin(2 * np.pi * x) x = np.linspace(0, 5, 500) y = func(x) plt.plot(x, y) plt.xlabel('x') plt.ylabel('y') plt.title('y = e^{-x} sin(2πx)') plt.show() ``` 运行这段
recommend-type

JSBSim Reference Manual

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