RB-Tree(红黑树,Red-Back Tree)的C语言算法实现

来源:互联网 发布:什么是阿里云 编辑:程序博客网 时间:2024/06/05 08:30

 

源文件:red_back_tree.h

#ifndef _RED_BACK_TREE#define _RED_BACK_TREE#include <iostream.h> #define BLACK 0#define RED 1typedef struct _RBNode{int key;int color;struct _RBNode *parent, *leftchild, *rightchild;} RBNode,*RBTree;static RBNode _Nil = {0, BLACK, NULL, NULL, NULL};const static RBTree Nil = &_Nil;#define EQ(x, y)  ((x)->key==(y)->key)#define LT(x, y) ((x)->key < (y)->key)#define RBTREE_SIZE sizeof(RBNode) /* 根据关键字集合创建RBTree */int create_RBTree(RBTree &T, int keys[], int n);/* 左旋 */void L_Rote(RBTree &T, RBTree x);/* 右旋 */void R_Rote(RBTree &T, RBTree x);/* 将新节点插入到RBTree中 */int insert_to_RBTree(RBTree &T, RBTree node);/* 调整插入T的z结点 */void fixup_insert_RBTree(RBTree &T, RBTree z);/* 找到关键字所对应的节点 */RBTree find_RBTree(const RBTree T, int key);/* 删除z元素,并返回真正被删除结点的指针。要求z为T中一存在的结点~ */RBTree delete_from_RBTree(RBTree &T, RBTree z);/* 删除以key为关键字的结点,~若成功,则返回1,否则返回0. */int delete_from_RBTree(RBTree &T, int key);/* 当删除x的原父亲之后,调整x */void fixup_delete_RBTree(RBTree &T, RBTree x);/* 先序遍历RBTree */void pre_order_RBTree(const RBTree T);void _pre_order_RBTree(const RBTree T);#endif


 

源代码:red_back_tree.cpp

#include "red_back_tree.h"#include <stdio.h>#include <iostream.h>#include <stdlib.h>#include <memory.h>int create_RBTree(RBTree &T, int keys[], int n){RBTree p = NULL;for(int i = 0; i < n; i++){if(!(p=(RBTree) malloc (RBTREE_SIZE))){exit(-1);}p->leftchild = p->rightchild = Nil;p->key = keys[i];insert_to_RBTree(T, p);printf("add the key:%d\n", keys[i]);pre_order_RBTree(T);}return 1;}int insert_to_RBTree(RBTree &T, RBTree  z){RBTree y , x ;y = Nil;x = T;// 找到插入z的父结点y~while(x!=Nil){y = x;x = LT(z, x) ? x->leftchild : x->rightchild;}z->parent = y;if(y==Nil){T = z;}else if(LT(z, y)){y->leftchild = z;}else{y->rightchild = z;}z->leftchild = z->rightchild = Nil;z->color = RED;fixup_insert_RBTree(T, z);return 1;}void fixup_insert_RBTree(RBTree &T, RBTree z){RBTree y; // y is z's "uncle"// while循环中,p[p[z]]结点存在。若z为根,或p[z]为根,// 与z,和z->parent为RED相矛盾,故p[p[z]]存在。while(z->parent->color==RED){if(z->parent->parent->leftchild == z->parent){ // p[z]为p[p[z]]的左结点y = z->parent->parent->rightchild;if(y->color == RED){ // case1z->parent->color = BLACK;y->color = BLACK;z->parent->parent->color = RED;z = z->parent->parent;}else{if(z->parent->rightchild == z){ //case 2z = z->parent;L_Rote(T, z);}// case 3z->parent->color = BLACK;z->parent->parent->color = RED;R_Rote(T, z->parent->parent);}} // if(z->parent->leftchild == z)else{ // p[z]为p[p[z]]的右结点y = z->parent->parent->leftchild;if(y->color == RED){ // case4z->parent->color = BLACK;y->color = BLACK;z->parent->parent->color = RED;z = z->parent->parent;}else{if(z->parent->leftchild == z){ // case5z = z->parent;R_Rote(T, z);}// case6z->parent->color = BLACK;z->parent->parent->color = RED;L_Rote(T, z->parent->parent);}} // else}T->color = BLACK;}void L_Rote(RBTree &T, RBTree x){RBTree y;y = x->rightchild;// y->leftchild.x->rightchild = y->leftchild;y->leftchild->parent = x;y->parent = x->parent;if(x->parent == Nil){T = y;}else if(x ==  x->parent->leftchild){x->parent->leftchild = y;}else{x->parent->rightchild = y;}// adjust the x.y->leftchild = x;x->parent = y;}void R_Rote(RBTree &T, RBTree x){RBTree y;y = x->leftchild; // adjust y's leftchild.x->leftchild = y->rightchild;y->rightchild->parent = x;// adjust y's parent.y->parent = x->parent;if(x->parent == Nil){T = y;}else if(x == x->parent->leftchild){x->parent->leftchild = y;}else{x->parent->rightchild = y;}// adjust y' rightchild.y->rightchild = x;x->parent = y;}int delete_from_RBTree(RBTree &T, int key){RBTree z = Nil;z = find_RBTree(T, key);if(z == Nil){return 0;}else{z = delete_from_RBTree(T, z);free(z);printf("delete the key:%d\n", key);pre_order_RBTree(T);return 1;}}RBTree find_RBTree(const RBTree T, int key){RBTree p;p=T;while(p!=Nil && p->key!=key){p = (key < p->key) ? p->leftchild : p->rightchild;}return p;}RBTree delete_from_RBTree(RBTree &T, RBTree z){RBTree x = Nil, y = Nil; // y为需要删除的指针; x为y的后继指针,~if(z->leftchild == Nil || z->rightchild == Nil){ // 如果z为单树树y = z;}else{ // 找到z的后继,~即右树子的最左结点,~y = z->rightchild;while(y->leftchild != Nil) {y = y->leftchild;}}// 找到y的后继if(y->leftchild != Nil){x = y->leftchild;}else{x = y->rightchild;}x->parent = y->parent;// 删除y,处理y的父亲与y的孩子x的关系,~if(y->parent == Nil){// y即为根结点T = x;}else if(y->parent->leftchild == y){y->parent->leftchild = x;}else{y->parent->rightchild = x;}if(y!=z){z->key = y->key;}if(y->color == BLACK)fixup_delete_RBTree(T, x);return y;}void fixup_delete_RBTree(RBTree &T, RBTree x){RBTree m = Nil;while(T!=x && x->color == BLACK){ // x为重黑结点,~if(x == x->parent->leftchild){ // x为p[x]左孩子的情况~m = x->parent->rightchild; // m为x的兄弟~if(m->color == RED){ // case 1x->parent->color = RED;m->color = BLACK;L_Rote(T, x->parent);}else if(m->rightchild->color == BLACK && m->leftchild->color == BLACK){ // case 2m->color = RED;x = x->parent;}else{if(m->rightchild->color == BLACK){ // case 3 -> case4m->color = RED;m->leftchild->color = BLACK;R_Rote(T, m);m = x->parent->rightchild;}// case 4m->rightchild->color = RED;m->color = x->parent->color;x->parent->color = BLACK;L_Rote(T, x->parent);x = T;}} // if(x == x->parent->leftchild)else{ // x为p[x]右孩子的情况~m = x->parent->leftchild;if(m->color == RED){ // case 1'x->parent->color = RED;m->color = BLACK;R_Rote(T, x->parent);}else if(m->leftchild->color == BLACK && m->rightchild->color == BLACK){ // case 2'm->color = RED;x = x->parent;}else{if(m->leftchild->color == BLACK){ // case3'm->color = RED;m->rightchild->color = BLACK;L_Rote(T, m);m = x->parent->leftchild;}// case4'm->leftchild->color = BLACK;m->color = x->parent->color;x->parent->color = BLACK;R_Rote(T, x->parent);x = T;}//else} //  else{ // x为p[x]右孩子的情况~} // while(T!=x && x->color == BLACK)x->color = BLACK; // 或x为红黑结点~,或为根结点~}void pre_order_RBTree(const RBTree T){printf("print the RBTree in pre_order:\n");_pre_order_RBTree(T);printf("\n\n");}void _pre_order_RBTree(const RBTree T){if(Nil==T)return;if(T->color==BLACK){printf("%-4s:%-4d", "BLACK", T->key);}else{printf("%-4s:%-4d", "RED", T->key);}_pre_order_RBTree(T->leftchild);_pre_order_RBTree(T->rightchild);}int main(){RBTree T = Nil;int keys[] = {1,2,3,5,4, 6, 7, 8};create_RBTree(T, keys, 8);delete_from_RBTree(T, 7);delete_from_RBTree(T, 4);return 0;}


 

原创粉丝点击