资源简介
红黑树的详细描述,从数据结构到创建,最小值,最大值,后继,遍历,插入以及删除。
该代码是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源程序
相关资源
- Apolipoprotein E4 Impairs in vivo Hippocampal
- SSM+Shiro+redis实现单点登陆
- 北航 数值分析 Fredholm积分方程的数值
- Effects of the cultured Cordyceps exopolysacch
- redisson的demo
- 基于Blackfin 处理器的TFT LCD 驱动设计
- redmine系统agile敏捷插件安装包
- Spring-Data-Redis2.0+Spring5
- mongodb+redis资源
- 基于Blackfin的无线IP视频监控解决方案
- NetApp Data ONTAP:registered: GX产品简介
- Numerical simulation and prediction of radio f
- Hall effect of reactive sputtered iron nitride
- Can EC-MPS reduce gastrointestinal side effect
- redis安装包
- 存储系统向Sun StorEdge高端系统的成功
- 破解ServiceStack.Redis每小时6000次限制
- redis for Windows
- 基于四核和双核英特尔:registered: 至强
- 英特尔:registered: 酷睿:trade_mark:双核处
- 借助英特尔:registered: 主动管理技术缩
- LANDesk 管理解决方案和采用英特尔:r
- 英特尔:registered: 酷睿:trade_mark:2 双核
- 四核英特尔:registered: 至强:registered:
- 英特尔:registered: 酷睿:trade_mark:2 双核
- Unicenter 解决方案和采用英特尔:regis
- 英特尔:registered: 博锐:trade_mark: 技术关
- 面向采用英特尔:registered: 博锐:trade
- 英特尔:registered:至强:registered:处理器
- 基于英特尔:registered: 至强:registered:
评论
共有 条评论