资源简介
红黑树的详细描述,从数据结构到创建,最小值,最大值,后继,遍历,插入以及删除。
该代码是clion IDE中实现的,代码全部在main.c中。
代码片段和文件信息
#include
#include
typedef int ElemType;
typedef enum {RED BLACK} Color;
//structure
// node of the tree
typedef struct Node{
Color color;
struct Node *left;
struct Node *right;
struct Node *p;
ElemType key;
}Node;
// red black tree
typedef struct Tree{
Node *nillNode;
Node * root;
}*RedBlackTree;
// find the min key with x as the root
Node * min(RedBlackTree t Node * x) {
if (x != t->nillNode) {
Node *y = x->left;
while (y != t->nillNode) {
x = y;
y = x->left;
}
}
return x;
}
// find the max key with x as the root
Node * max(RedBlackTree t Node *x) {
if (x != t->nillNode) {
Node *y = x->right;
while (y != t->nillNode) {
x = y;
y = x->right;
}
}
return x;
}
Node *successor(RedBlackTree t Node * x) {
if(x->right != t->nillNode) {
return min(t x->right);
}
Node * y = x->p;
while (y != t->nillNode && x == y->left) {
x = y;
y = y->p;
}
return y;
}
// create red black tree and initial
void createTree(RedBlackTree *t) {
// global nil node
Node * nillNode = NULL;
// create nillNode and initial
nillNode = (Node *)malloc(sizeof(Node));
if(nillNode == NULL) {
printf(“nilNode allocation failed!\n“);
exit(1);
}
nillNode->color = BLACK;
nillNode->left = NULL;
nillNode->right = NULL;
nillNode->p = NULL;
//create Red Black Tree and initial
(*t) = (RedBlackTree) malloc(sizeof(struct Tree));
if(*t == NULL) {
printf(“red black allocation failed!\n“);
exit(-1);
}
(*t)->root = nillNode;
(*t)->nillNode = nillNode;
}
void leftRotate(RedBlackTree t Node *x) {
// Suppose the right node exists(right node not null)
Node * y = x->right;
//turn y‘s left subtree into x‘s right subtree
x->right = y->left;
if(y->left != t->nillNode) {
y->left->p = x;
}
y->p = x->p;
if(x->p == t->nillNode) { // x is the root of the tree
t->root = y;
} else if (x == x->p->left) {
x->p->left = y;
} else {
x->p->right = y;
}
// put x on y‘s leftg
y->left = x;
// link x->p to y
x->p = y;
}
void rightRotate(RedBlackTree t Node *x) {
Node *y = x->left;
x->left = y->right;
if(y->left != t->nillNode) {
y->right->p = x;
}
y->p = x->p;
if(x->p == t->nillNode) {
t->root = y;
} else if (x == x->p->left) {
x->p->left = y;
} else {
x->p->right = y;
}
y->right = x;
x->p = y;
}
// used for fix up when insert
void insertFixUp(RedBlackTree t Node *z) {
Node * y = t->nillNode;
while (z->p->color == RED) {
if(z->p == z->p->p->left) {
// find z‘s uncle node
y = z->p->p->right;
// case 1
if (y->color == RED) {
z->p->color = BLACK;
属性 大小 日期 时间 名称
----------- --------- ---------- ----- ----
文件 137 2017-01-18 20:46 RedBlackTree\.idea\misc.xm
文件 276 2017-01-18 20:45 RedBlackTree\.idea\modules.xm
文件 97 2017-01-18 20:46 RedBlackTree\.idea\RedBlackTree.iml
文件 27200 2017-01-27 17:20 RedBlackTree\.idea\workspace.xm
文件 34567 2017-01-18 20:45 RedBlackTree\cmake-build-debug\CMakeCache.txt
文件 2151 2017-01-18 20:45 RedBlackTree\cmake-build-debug\CMakeFiles\3.6.3\CMakeCCompiler.cmake
文件 4703 2017-01-18 20:45 RedBlackTree\cmake-build-debug\CMakeFiles\3.6.3\CMakeCXXCompiler.cmake
文件 60657 2017-01-18 20:45 RedBlackTree\cmake-build-debug\CMakeFiles\3.6.3\CMakeDetermineCompilerABI_C.bin
文件 60552 2017-01-18 20:45 RedBlackTree\cmake-build-debug\CMakeFiles\3.6.3\CMakeDetermineCompilerABI_CXX.bin
文件 234 2017-01-18 20:45 RedBlackTree\cmake-build-debug\CMakeFiles\3.6.3\CMakeRCCompiler.cmake
文件 395 2017-01-18 20:45 RedBlackTree\cmake-build-debug\CMakeFiles\3.6.3\CMakeSystem.cmake
文件 60777 2017-01-18 20:45 RedBlackTree\cmake-build-debug\CMakeFiles\3.6.3\CompilerIdC\a.exe
文件 17387 2017-01-18 20:45 RedBlackTree\cmake-build-debug\CMakeFiles\3.6.3\CompilerIdC\CMakeCCompilerId.c
文件 60691 2017-01-18 20:45 RedBlackTree\cmake-build-debug\CMakeFiles\3.6.3\CompilerIdCXX\a.exe
文件 16930 2017-01-18 20:45 RedBlackTree\cmake-build-debug\CMakeFiles\3.6.3\CompilerIdCXX\CMakeCXXCompilerId.cpp
文件 108 2017-01-18 20:45 RedBlackTree\cmake-build-debug\CMakeFiles\clion-environment.txt
文件 917 2017-01-18 20:45 RedBlackTree\cmake-build-debug\CMakeFiles\clion-log.txt
文件 86 2017-01-18 20:45 RedBlackTree\cmake-build-debug\CMakeFiles\cmake.check_cache
文件 670 2017-01-18 20:45 RedBlackTree\cmake-build-debug\CMakeFiles\CMakeDirectoryInformation.cmake
文件 49455 2017-01-18 20:45 RedBlackTree\cmake-build-debug\CMakeFiles\CMakeOutput.log
文件 62055 2017-01-18 20:45 RedBlackTree\cmake-build-debug\CMakeFiles\feature_tests.bin
文件 722 2017-01-18 20:45 RedBlackTree\cmake-build-debug\CMakeFiles\feature_tests.c
文件 10416 2017-01-18 20:45 RedBlackTree\cmake-build-debug\CMakeFiles\feature_tests.cxx
文件 11857 2017-01-18 20:45 RedBlackTree\cmake-build-debug\CMakeFiles\Makefile.cmake
文件 3479 2017-01-18 20:45 RedBlackTree\cmake-build-debug\CMakeFiles\Makefile2
文件 3 2017-01-18 20:45 RedBlackTree\cmake-build-debug\CMakeFiles\progress.marks
文件 4934 2017-01-18 20:45 RedBlackTree\cmake-build-debug\CMakeFiles\RedBlackTree.dir\build.make
文件 224 2017-01-27 17:20 RedBlackTree\cmake-build-debug\CMakeFiles\RedBlackTree.dir\C.includecache
文件 332 2017-01-18 20:45 RedBlackTree\cmake-build-debug\CMakeFiles\RedBlackTree.dir\cmake_clean.cmake
文件 183 2017-01-27 17:20 RedBlackTree\cmake-build-debug\CMakeFiles\RedBlackTree.dir\depend.internal
............此处省略28个文件信息
- 上一篇:软件工程之可行性报告案例
- 下一篇:基于51单片机的空调遥控器C源程序
相关资源
- maven+spring4+springmvc+redis实现分页
- Hadoop MapReduce实现tfidf源码
- VMwarevSphereDataProtection6.1.6.txt
- Posterior Regularization for Structured Latent
- A New Method of Automatic Modulation Recogniti
- windows64位平台的hadoop2.5.2插件包(ha
- create-react-app构建dva项目20180728
- LINUX-FTP服务包 vsftpd-2.0.1-5.src.rpm
- Redis新手入门详解.pdf
- isapi_redirect-1.2.2764位
- 应用径向基神经网络求解第二类线性
- MapReduce求解物流配送单源最短路径研
- Model predictive control of a mobile robot usi
- Spring集成redis集群
- redis集群以及Spring-data-redis操作集群
- redis-2.6.13.tar.gz
- Redis+Spring缓存windows环境,附及详解
- Redis命令中文文档CHM
- RHCE8.0 有答案题库密码:redhat666.pdf
- 用MapReduce实现KMeans算法
- redis手册.zip
- redis-3.0.0.gem
- 基于mapreduce的并行算法的设计 课件
- vue+springboot前后端分离项目源码
- redis消息大小对性能的影响
- SpringBoot+redis+RabbitMq整合
- ServiceStack.Redis.4.0.60破解版
- ServiceStack.Redis5.2.0 最新版去除6000次限
- redhat官方镜像大全
- Red_Hat_Enterprise_Linux-7-Performance_Tuning_
评论
共有 条评论