RB-Tree(红黑树,Red-Back Tree)的C语言算法实现
2011-08-10 01:27
363 查看
源文件:red_back_tree.h
源代码:red_back_tree.cpp
#ifndef _RED_BACK_TREE #define _RED_BACK_TREE #include <iostream.h> #define BLACK 0 #define RED 1 typedef 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) { // case1 z->parent->color = BLACK; y->color = BLACK; z->parent->parent->color = RED; z = z->parent->parent; } else { if(z->parent->rightchild == z) { //case 2 z = z->parent; L_Rote(T, z); } // case 3 z->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) { // case4 z->parent->color = BLACK; y->color = BLACK; z->parent->parent->color = RED; z = z->parent->parent; } else { if(z->parent->leftchild == z) { // case5 z = z->parent; R_Rote(T, z); } // case6 z->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 1 x->parent->color = RED; m->color = BLACK; L_Rote(T, x->parent); } else if(m->rightchild->color == BLACK && m->leftchild->color == BLACK) { // case 2 m->color = RED; x = x->parent; } else { if(m->rightchild->color == BLACK) { // case 3 -> case4 m->color = RED; m->leftchild->color = BLACK; R_Rote(T, m); m = x->parent->rightchild; } // case 4 m->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; }
相关文章推荐
- 数据结构 - 红黑树(Red Black Tree)删除详解与实现(Java)
- 红黑树(RedBlackTree)实现
- 红黑树(red black tree)的实现
- 自平衡二叉查找树(二)-----------红黑树(redblack tree)分析和代码实现
- 红黑树(Red BlackTree)的实现
- 红黑树(red black tree) Common Lisp 实现
- 红黑树(RedBlackTree),平衡树(BalancedTree),SkipList 的实现
- [转载] 红黑树(Red Black Tree)- 对于 JDK TreeMap的实现
- 红黑树与C实现算法 - RedBlackTree.c
- 红黑树(Red Black Tree) - Flex实现
- 红黑树与C实现算法 - RedBlackTree.c
- [从头学数学] 第261节 Python实现数据结构:红黑树(RB Tree)
- C++实现一个简单的红黑树(RB_TREE)
- 红黑树(RB-tree)比AVL树的优势在哪?
- 红黑树(Red-Black Tree)
- RedBlackTree(红黑树)--一种自平衡(最优)的二叉搜索树算法
- 红黑树(Red Black Tree)
- Linux内核中使用红黑树的扩展特性实现区间树(Interval tree)
- 非递归学习树结构(六)--RB-Tree(红黑树)
- 红黑树(rb_tree) 源代码