#include<stdafx.h> #include<malloc.h> #include <assert.h>
typedef enum ColorType {RED, BLACK} ColorType; typedef struct rbt_t{ int key; rbt_t * left; rbt_t * right; rbt_t * p; ColorType color; }rbt_t;
typedef struct rbt_root_t{ rbt_t* root; rbt_t* nil; }rbt_root_t;
rbt_root_t* rbt_init(void); static void rbt_handleReorient(rbt_root_t* T, rbt_t* x, int k); rbt_root_t* rbt_insert(rbt_root_t* &T, int k); rbt_root_t* rbt_delete(rbt_root_t* &T, int k);
void rbt_transplant(rbt_root_t* T, rbt_t* u, rbt_t* v);
static void rbt_left_rotate( rbt_root_t* T, rbt_t* x); static void rbt_right_rotate( rbt_root_t* T, rbt_t* x);
void rbt_inPrint(const rbt_root_t* T, rbt_t* t); void rbt_prePrint(const rbt_t * T, rbt_t* t); void rbt_print(const rbt_root_t* T);
static rbt_t* rbt_findMin(rbt_root_t * T, rbt_t* t); static rbt_t* rbt_findMax(rbt_root_t * T, rbt_t* t);
static rbt_t* rbt_findMin(rbt_root_t * T, rbt_t* t){ if(t == T->nil) return T->nil;
while(t->left != T->nil) t = t->left; return t; } static rbt_t* rbt_findMax(rbt_root_t * T, rbt_t* t){ if(t == T->nil) return T->nil;
while(t->right != T->nil) t = t->right; return t; }
rbt_root_t* rbt_init(void){ rbt_root_t* T;
T = (rbt_root_t*)malloc(sizeof(rbt_root_t)); assert( NULL != T);
T->nil = (rbt_t*)malloc(sizeof(rbt_t)); assert(NULL != T->nil); T->nil->color = BLACK; T->nil->left = T->nil->right = NULL; T->nil->p = NULL;
T->root = T->nil;
return T; }
void rbt_handleReorient(rbt_root_t* T, rbt_t* x, int k){
x->color = RED; x->left->color = x->right->color = BLACK;
if( RED == x->p->color){ x->p->p->color = RED;
if(x->p->key < x->p->p->key){ if(k > x->p->key){ x->color = BLACK; rbt_left_rotate(T,x->p); rbt_right_rotate(T,x->p); }else{ x->p->color = BLACK; rbt_right_rotate(T,x->p->p); }
}else{ if(k < x->p->key){ x->color = BLACK; rbt_right_rotate(T,x->p); rbt_left_rotate(T,x->p); }else{ x->p->color = BLACK; rbt_left_rotate(T,x->p->p); }
} }
T->root->color = BLACK; }
rbt_root_t* rbt_insert(rbt_root_t* &T, int k){
rbt_t * x, *p; x = T->root; p = x;
while( x != T->nil){ if(x != T->nil) if(x->left->color == RED && x->right->color == RED) rbt_handleReorient(T,x,k);
p = x; if(k<x->key) x = x->left; else if(k>x->key) x = x->right; else{ printf("\n%d已存在\n",k); return T; }
}
x = (rbt_t *)malloc(sizeof(rbt_t)); assert(NULL != x); x->key = k; x->color = RED; x->left = x->right = T->nil; x->p = p;
if(T->root == T->nil) T->root = x; else if(k < p->key) p->left = x; else p->right = x;
rbt_handleReorient(T,x,k);
return T; } void rbt_transplant(rbt_root_t* T, rbt_t* u, rbt_t* v){ if(u->p == T->nil) T->root = v; else if(u == u->p->left) u->p->left =v; else u->p->right = v; v->p = u->p; }
rbt_root_t* rbt_delete(rbt_root_t* &T, int k){ assert(T != NULL); if(NULL == T->root) return T;
rbt_t * toDelete = T->root; rbt_t * x;
while(toDelete != T->nil && toDelete->key != k){ if(k<toDelete->key) toDelete = toDelete->left; else if(k>toDelete->key) toDelete = toDelete->right; }
if(toDelete == T->nil){ printf("\n%d 不存在\n",k); return T; }
if(toDelete->left != T->nil && toDelete->right != T->nil){ rbt_t* alternative = rbt_findMin(T, toDelete->right); k = toDelete->key = alternative->key; toDelete = alternative; }
if(toDelete->left == T->nil){ x = toDelete->right; rbt_transplant(T,toDelete,toDelete->right); }else if(toDelete->right == T->nil){ x = toDelete->left; rbt_transplant(T,toDelete,toDelete->left); }
if(toDelete->color == BLACK){ while(x != T->root && x->color == BLACK){ if(x == x->p->left){ rbt_t* w = x->p->right;
if(RED == w->color){ w->color = BLACK; w->p->color = RED; rbt_left_rotate(T,x->p); w = x->p->right; }
if(w->left->color == BLACK && w->right->color == BLACK){ if(x->p->color == RED){ x->p->color = BLACK; w->color = RED;
break; }else{ w->color = RED; x = x->p; continue; } }
if(w->right->color == BLACK){ w->left->color = BLACK; w->color = RED; rbt_right_rotate(T,w); w = x->p->right; }
w->color=x->p->color; x->p->color=BLACK; w->right->color=BLACK; rbt_left_rotate(T,x->p); x = T->root; }else{ rbt_t* w = x->p->left; if(w->color == RED){ w->color = BLACK; x->p->color = RED; rbt_right_rotate(T,x->p); w = x->p->left; } if(w->left->color==BLACK && w->right->color == BLACK){ if(x->p->color == RED){ x->p->color = BLACK; w->color = RED; break; }else{ x->p->color = BLACK; w->color = RED; x = x->p; continue; } }
if(w->left->color == BLACK){ w->color = RED; w->right->color = BLACK; w = x->p->left; }
w->color=w->p->color; x->p->color = BLACK; w->left->color = BLACK; rbt_right_rotate(T,x->p); x = T->root; }
} x->color = BLACK; }
free(toDelete);
return T; }
void rbt_left_rotate( rbt_root_t* T, rbt_t* x){ rbt_t* y = x->right;
x->right = y->left; if(x->right != T->nil) x->right->p = x;
y->p = x->p; if(y->p == T->nil){ T->root = y; }else if(y->key < y->p->key) y->p->left = y; else y->p->right = y;
y->left = x; x->p = y; }
void rbt_right_rotate( rbt_root_t* T, rbt_t* x){ rbt_t * y = x->left; x->left = y->right;
if(T->nil != x->left) x->left->p = x;
y->p = x->p; if(y->p == T->nil) T->root = y; else if(y->key < y->p->key) y->p->left= y; else y->p->right = y;
y->right = x; x->p = y; } void rbt_prePrint(const rbt_root_t* T, rbt_t* t){ if(T->nil == t)return ; if(t->color == RED) printf("%3dR",t->key); else printf("%3dB",t->key); rbt_prePrint(T,t->left); rbt_prePrint(T,t->right); } void rbt_inPrint(const rbt_root_t* T, rbt_t* t){ if(T->nil == t)return ; rbt_inPrint(T,t->left); if(t->color == RED) printf("%3dR",t->key); else printf("%3dB",t->key); rbt_inPrint(T,t->right); }
void rbt_print(const rbt_root_t* T){ assert(T!=NULL); printf("\n前序遍历 :"); rbt_prePrint(T,T->root); printf("\n中序遍历 :"); rbt_inPrint(T,T->root); printf("\n"); }
void rbt_test(){ rbt_root_t* T = rbt_init();
T = rbt_insert(T,11); T = rbt_insert(T,7); T = rbt_insert(T,1); T = rbt_insert(T,2); T = rbt_insert(T,8); T = rbt_insert(T,14); T = rbt_insert(T,15); T = rbt_insert(T,5); T = rbt_insert(T,4);
T = rbt_insert(T,4); rbt_print(T);
rbt_delete(T,8); rbt_delete(T,14);rbt_delete(T,7);rbt_delete(T,11);
rbt_delete(T,8); rbt_print(T);
}
|