算法导论12通过继承二叉树实现红黑树

//Bitree.h

#ifndef __BITREE_HH__
#define __BITREE_HH__


template <typename T>
class node
{
public:
node<T> *p;
node<T> *left;
node<T> *right;
T key;
node(T key = T()):p(NULL),left(NULL),right(NULL),key(key){};
};


template <typename T>
class bitree
{
private:
node<T> *root;


public:
void Insert(T x);
void inordertraversal();
bitree():root(NULL){};
void Delete(T x);
node<T> *Serach(T x);
node<T> *MINMUM(node<T> *r);
node<T> *MAXMUM(node<T> *r);
void Transplant(node<T> *u, node<T> *v);
};


#include "Bitree.cpp"
#endif

//Bitree.cpp

#ifdef __BITREE_HH__


#include "Bitree.h"
#include <iostream>
#include <stack>
#include <stdlib.h>
using namespace std;


template <typename T>
node<T> *bitree<T>::MINMUM(node<T> *r)
{
node<T> *f;
f = r;
while(f->left != NULL)
f = f->left;
return f;
}
template <typename T>
node<T> *bitree<T>::MAXMUM(node<T> *r)
{
node<T> *f;
f = r;
while(f->right != NULL)
f = f->left;
return f;
}
template <typename T>
node<T> *bitree<T>::Serach(T x)
{
node<T> *t = root;
while(t != NULL)
{
if(x == t->key)
return t;
else if(x > t->key)
t = t->right;
else
t = t->left;
}
return NULL;
}
template <typename T>
void bitree<T>::Delete(T x)
{
node<T> *u,*y;
u = Serach(x);
if(u->left == NULL)
Transplant(u,u->right);
else if (u->right == NULL)
Transplant(u,u->left);
else
{
y = MINMUM(u->right);
if(y->p != u)
{
Transplant(y,y->right);
y->right = u->right;
y->right->p = y;
}
Transplant(u,y);
y->left = u->left;
y->left->p = y;
}
}


template <typename T>
void bitree<T>::Transplant(node<T> *u, node<T> *v)/*not update v.left v.right*/
{
if(u->p == NULL)
root = v;
else if(u == u->p->left)
u->p->left = v;
else
u->p->right = v;
if(v != NULL)
v->p = u->p;
}


template <typename T>
void bitree<T>::Insert(T x)
{
node<T> *t = root;
node<T> *f = NULL;
node<T> *n = new node<T>(x);
while(t != NULL)
{
f = t;
if(x > (t->key))
t = t->right;
else
t = t->left; 
}
if(f == NULL)
root = n;
else if(x > (f->key))
f->right = n;
else
f->left = n;
n->p = f;

}


template <typename T>
/* add an addition stack*/
void bitree<T>::inordertraversal()
{
stack<node<T>*> s;
node<T> *t = root;
while(t!=NULL || !s.empty())
{
while(t!=NULL)
{
s.push(t);
t = t->left;
}
if(!s.empty())
{
t = s.top();
s.pop();
cout<<t->key<<endl;
t = t->right;
}
}
}




#endif

//RBtree.h

#ifndef __RBTREE_HH__
#define __RBTREE_HH__


#include "Bitree.h"


template <typename T>
class RBnode:public node<T>
{
public:
RBnode<T> *p;
RBnode<T> *left, *right;
int color;//0 = black 1 = red
RBnode(T key = T()):node<T>(key),color(0){};
};


template <typename T>
class RBtree:public bitree<T>
{
private:
RBnode<T> *root;
RBnode<T> *nil;/*sentinel node*/
public:
void Leftrotate(RBnode<T> *a);
void Rightrotate(RBnode<T> *a);
void RBInsert(T x);
void RBInsertfix(RBnode<T> *a);
RBtree()//:nil(NULL),root(NULL)
{
root = NULL;
nil = new RBnode<T>(55);
};
void inordertraversal();
void Transplant(RBnode<T> *u, RBnode<T> *v);
void RBDelete(T x);
void RBDeletefix(RBnode<T> *x);
RBnode<T> *RBSerach(T x);
RBnode<T> *MINMUM(RBnode<T> *r);
};
#include "RBtree.cpp"
#endif

//RBtree.cpp

#ifdef __RBTREE_HH__


#include "RBtree.h"
using namespace std;


template <typename T>
void RBtree<T>::RBDeletefix(RBnode<T> *x)
{
RBnode<T> *w;
while(x != root && x->color == 0)
{
if(x == x->p->left)
{
w = x->p->right;
if(w->color == 1)
{
w->color = 0;
x->p->color = 1;
Leftrotate(x->p);
w = x->p->right;
}
if(w->left->color == 0 && w->right->color == 0)
{
w->color = 1;
x = x->p;
}
else if(w->right->color == 0)
{
w->left->color = 0;
w->color = 1;
Rightrotate(w);
w = x->p->right;
}
w->color = x->p->color;
x->p->color = 0;
w->right->color = 0;
Leftrotate(x->p);
x = root;
}
else
{
w = x->p->left;
if(w->color == 1)
{
w->color = 0;
x->p->color = 1;
Rightrotate(x->p);
w = x->p->left;
}
if(w->left->color == 0 && w->right->color == 0)
{
w->color = 1;
x = x->p;
}
else if(w->left->color == 0)
{
w->right->color = 0;
w->color = 1;
Leftrotate(w);
w = x->p->left;
}
w->color = x->p->color;
x->p->color = 0;
w->left->color = 0;
Rightrotate(x->p);
x = root;
}
}
x->color = 0;
}
template <typename T>
void RBtree<T>::RBInsertfix(RBnode<T> *a)
{
RBnode<T> *y;
while(a->p->color == 1)
{
if(a->p == a->p->p->left)
{
y = a->p->p->right;
if(y->color == 1)
{
a->p->color = 0;
y->color = 0;
a->p->p->color = 1;
a = a->p->p;
}
else 
{
if (a == a->p->right)
{
a = a->p;
Leftrotate(a);
}
a->p->color = 0;
a->p->p->color = 1;
Rightrotate(a->p->p);
}
}
else //if(a->p == a->p->p->right)
{
y = a->p->p->left;
if(y->color == 1)
{
a->p->color = 0;
y->color = 0;
a->p->p->color = 1;
a = a->p->p;
}
else 
{
if (a == a->p->left)
{
a = a->p;
Rightrotate(a);
}
a->p->color = 0;
a->p->p->color = 1;
Rightrotate(a->p->p);
}
}
}
root->color = 0;
}
template <typename T>
void RBtree<T>::Leftrotate(RBnode<T> *a)
{
RBnode<T> *y = a->right;
a->right = y->left;
if(y->left != nil)
{
y->left->p = a;
}
y->p = a->p;
if(a->p == nil)
{
root = y;
}
else if (a == a->p->left)
a->p->left = y;
else
a->p->right = y;
y->left = a;
a->p = y;
}
template <typename T>
void RBtree<T>::Rightrotate(RBnode<T> *a)
{
RBnode<T> *y = a->left;
a->left = y->right;
if(y->right != nil)
{
y->right->p = a;
}
y->p = a->p;
if(a->p == nil)
{
root = y;
}
else if (a == a->p->left)
a->p->left = y;
else
a->p->right = y;
y->right = a;
a->p = y;
}
template <typename T>
void RBtree<T>::RBInsert(T x)
{
RBnode<T> *t = root;
RBnode<T> *f = NULL;//nil
//cout<<nil->color;
RBnode<T> *n = new RBnode<T>(x);
while(t != NULL && t != nil)
{
f = t;
if(x > (t->key))
t = t->right;
else
t = t->left; 
}
if(f == NULL)
{
//nil->color = 0;
root = n;
}
else if(x > (f->key))
f->right = n;
else
f->left = n;


if(f == NULL)
{
n->p = nil;
}
else
n->p = f;
n->left = nil;
n->right = nil;
n->color = 1;
RBInsertfix(n);
}
template <typename T>
RBnode<T> *RBtree<T>::MINMUM(RBnode<T> *r)
{
RBnode<T> *f;
f = r;
while(f->left != NULL && f->left != nil)
f = f->left;
return f;
}


template <typename T>
RBnode<T> *RBtree<T>::RBSerach(T x)
{
RBnode<T> *t = root;
while(t != NULL && t != nil)
{
if(x == t->key)
{
cout<<"find";
return t;
}
else if(x > t->key)
t = t->right;
else
t = t->left;
}
return NULL;
}


template <typename T>
void RBtree<T>::RBDelete(T x)
{
RBnode<T> *u, *y;
RBnode<T> *z;// = new RBnode<T>;
z = RBSerach(x);
y = z;
int y_original_color = z->color;
if(z->left == nil)
{
u = z->right;
Transplant(z,z->right);
}
else if (z->right == nil)
{
u = z->left;
Transplant(z,z->left);
}
else
{
y = MINMUM(z->right);
y_original_color = y->color;
u = y->right;
if(y->p == z)
{
u->p = y;
}
else
{
Transplant(y,y->right);
y->right = z->right;
y->right->p = y;
}
Transplant(z,y);
y->left = z->left;
y->left->p = y;
y->color = z->color;
}
if(y_original_color == 0)
{
RBDeletefix(u);
cout<<"fix";
}
}


template <typename T>
void RBtree<T>::Transplant(RBnode<T> *u, RBnode<T> *v)
{
if(u->p == nil)
root = v;
else if (u == u->p->left)
u->p->left = v;
else
u->p->right = v;
v->p = u->p;
}


template <typename T>
/* add an addition stack*/
void RBtree<T>::inordertraversal()
{
stack<RBnode<T>*> s;
RBnode<T> *t = root;
while((t != NULL || !s.empty()))
{
while(t != NULL)// && t != nil)
{
s.push(t);
t = t->left;
}
if(!s.empty())
{
t = s.top();
s.pop();
cout<<t->key<< t->color<<endl;
t = t->right;
}
}
}


#endif

//main.cpp

#include <iostream>
#include <stack>
#include <stdlib.h>
using namespace std;


#include "/home/yuxiaolei/Desktop/Introduction_to_algorithms/12/RBtree.h"
#include "Bitree.h"


int main()
{
bitree<int> bt;
RBtree<int> RB;
//node<int> *s;
//srand(time(NULL));
int x[10] = {5,8,2,4,6,7,1,9,3,0};
int y[5] = {5,8,2,4,6};
int z[6] = {5,8,2,4,6,9};
for(int i = 0; i < 6; ++i)
{
RB.RBInsert(z[i]);
}
RB.inordertraversal();
RB.RBDelete(8);
RB.inordertraversal();
return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值