花了2天的时间阅读,但是感觉还是有点错误,在里面。个人感觉,里面的nil和NULL处理有点问题。特别是在要求父节点,祖父节点,叔叔节点,兄弟节点时出问题。

#include<iostream>
using namespace std;
template<class T>
struct Node{
T value;
Node<T> *parent;
Node<T> *left;
Node<T> *right;
int flag;//zero is black ,and one is red
};
template<class T>
class RedBlack{
private:
Node<T> *root;
Node<T> *nil;
public:
Node<T>* getRoot();
RedBlack();
bool leftRotate(Node<T> *test);
bool rightRotate(Node<T> *test);
bool insert(T value);
bool remove(T value);
void remove_fixUp(Node<T> *test);
Node<T> *find(Node<T> *root,T key);
void insert_fixUp(Node<T> *test);
int getHight(Node<T> *root);
Node<T> *getGrandFather(Node<T> *node);
Node<T> *getUncle(Node<T> *node);
Node<T> *getBrother(Node<T> *node);
};
template<class T>
Node<T> *RedBlack<T>::getBrother(Node<T> *node){
if((node==this->root)||(node==NULL)||(node==this->nil))
return NULL;
else{
if(node == node->parent->left)
return node->parent->right;
else
return node->parent->left;
}
}
template<class T>
Node<T>* RedBlack<T>::getGrandFather(Node<T> *node){
if((node==this->nil)||(node==NULL))
return NULL;
else
return node->parent->parent;
}
template<class T>
Node<T>* RedBlack<T>::getUncle(Node<T> *node){
Node<T> *grand = getGrandFather(node);
if( (grand==NULL)||(grand== this->nil) )
return NULL;
else if(node->parent==grand->left)
return grand->right;
else
return grand->left;
}
template<class T>
int RedBlack<T>::getHight(Node<T> *root){
if((root==NULL)||(root==this->nil))
return 0;
else if(getHight(root->left)>getHight(root->right))
return getHight(root->left)+1;
else
return getHight(root->right)+1;
}
template<class T>
Node<T>* RedBlack<T>::find(Node<T> *root,T key){
if(root == NULL)
return NULL;
else{
if(root->value == key)
return root;
else if(root->value < key)
return(find(root->right,key));
else
return (find(root->left,key));
}

}
template<class T>
void RedBlack<T>::remove_fixUp(Node<T> *node){
Node<T> *brother = this->getBrother(node);
if(node==this->root)
this->root->flag=0;
else if((brother=NULL)&&(brother->flag==1)){
if(node==node->left){
this->leftRotate(node->parent);
node->parent->flag=1;
node->parent->parent->flag=0;
}
else{
this->rightRotate(node->parent);
node->parent->flag=1;
node->parent->parent->flag=0;
}
}
if((node!=this->root)&&(node!=this->nil)&&(node!=this->nil)&&(node->parent->flag==0)&&(brother!=NULL)&&(brother->flag==0)&&(brother->left->flag==0)&&(brother->right->flag==0)){
brother->flag=1;
remove_fixUp(node->parent);
}
else if((node!=this->root)&&(node!=this->nil)&&(node!=this->nil)&&(node->parent->flag==1)&&(brother!=NULL)&&(brother->flag==0)&&(brother->left->flag==0)&&(brother->right->flag==0)){
node->parent->flag=1;
brother->flag=0;
}
else if((node==node->parent->left)&&(brother!=NULL)&&(brother->left!=NULL)&&(brother->left->flag==1)&&(brother->flag==0)){///////////////
this->rightRotate(brother);
brother->flag=1;
brother->parent->flag=0;
}
else if((node==node->parent->right)&&(brother!=NULL)&&(brother->right!=NULL)&&(brother->right->flag==1)&&(brother->flag==0)){
this->leftRotate(brother);
brother->flag=1;
brother->parent->flag=0;
}
if((node==node->parent->left)&&(getBrother(node)!=NULL)&&(getBrother(node)->right!=NULL)&&(getBrother(node)->right->flag==1)&&(getBrother(node)->flag==0)){
this->rightRotate(node->parent);
int flag = node->parent->flag;
node->parent->flag = this->getBrother(node)->flag;
this->getBrother(node)->flag=flag;
this->getBrother(node)->right->flag=0;
}
else if((node==node->parent->right)&&(getBrother(node)!=NULL)&&(getBrother(node)->left!=NULL)&&(getBrother(node)->left->flag==1)&&(getBrother(node)->flag==0)){
this->leftRotate(node->parent);
int flag = node->parent->flag;
node->parent->flag = this->getBrother(node)->flag;
this->getBrother(node)->flag=flag;
this->getBrother(node)->left->flag=0;
}
}
template<class T>
bool RedBlack<T>::remove(T value){
Node<T> *node = this->find(this->root,value);
if((node==this->nil)||(node == NULL))
return false;
else{

if(node->left==this->nil){
if(node==this->root){
if(node->right == this->nil)
this->root = NULL;
else
{
this->root = node->right;
remove_fixUp(this->root);//renew it
this->root->parent = this->nil;
}
}
else{
if(node==node->parent->left){
node->parent->left = node->right;
if(node->right!=this->nil){
node->right->parent = node->parent;
if(node->flag==0)
remove_fixUp(node->right);
}else{
if(node->flag==0)
remove_fixUp(node->parent);

}
}
else{
node->parent->right = node->right;
if(node->right!=this->nil){
node->right->parent = node->parent;
if(node->flag == 0)
remove_fixUp(node->right);
}else{
if(node->flag==0)
remove_fixUp(node->parent);
}
}
}
}
else if(node->right == this->nil){
if(node==this->root){
this->root = node->left;
remove_fixUp(this->root);
}
else{
if(node==node->parent->left){
node->parent->left = node->left;
node->left->parent = node->parent;
}
else{
node->parent->right= node->left;
node->left->parent = node->parent;
}
if(node->flag==0)
remove_fixUp(node->left);
}
}
else{
Node<T> *left = node->left;
Node<T> *p = NULL;
while(left != this->nil){
p = left;
left = left->right;
}
if(p==node->left){
node->value = left->value;
if(left->left!=this->nil){
node->left = left->left;
left->left->parent = node;
if(left->flag==0)
remove_fixUp(left->left);
}
else{
node->left = this->nil;
}
}
else{
node->value = p->value;
if(p->left==this->nil){
p->parent->right = this->nil;
}
else{
p->parent->right = p->left;
p->left->parent = p->parent;
if(p->flag==0)
remove_fixUp(p->left);
}
}

}
}

return true;
}
template<class T>
bool RedBlack<T>::insert(T value){
if(this->find(this->root,value)!= NULL)
return false;
Node<T> *now = new Node<T>();
now->flag = 1;
now->value = value;
now->left = this->nil;
now->right = this->nil;
now->parent = this->nil;
Node<T> *y = this->nil;
Node<T> *root = this->root;
if(root==NULL){
this->root = now;
now->parent = this->nil;
now->left = this->nil;
now->right = this->nil;
}
else{
while((root!=this->nil)){
y = root;
if(root->value > value)///////////////////////////
root = root->left;
else
root = root->right;
}
if(value<y->value)
y->left = now;
else
y->right = now;
now->parent = y;
}
this->insert_fixUp(now);
return true;
}
template<class T>
void RedBlack<T>::insert_fixUp(Node<T> *test){
if(this->root == test)
this->root->flag = 0;
else if(test->parent->flag==1){
Node<T> *node = test;
if((getUncle(node)!=NULL)&&(getUncle(node)->flag==1)){
node->parent->flag = 0;
getUncle(node)->flag =0;
node->parent->parent->flag=1;
insert_fixUp(node->parent->parent);
}
else if((node==node->parent->left)&&(node->parent == node->parent->parent->right)){
leftRotate(node->parent);
node = node->left;
}
else if((node==node->parent->right)&&(node->parent == node->parent->parent->left)){
rightRotate(node->parent);
node = node->right;
}
else if((node==node->parent->right)&&(node->parent == node->parent->parent->right)){
leftRotate(node->parent->parent);
}
else if((node==node->parent->left)&&(node->parent == node->parent->parent->left)){
rightRotate(node->parent->parent);
}

}
}
template<class T>
RedBlack<T>::RedBlack(){
this->root = NULL;
nil = new Node<T>();
nil->value = -1;
nil->flag = 0;
nil->parent = NULL;
nil->left = NULL;
nil->right = NULL;
}
template<class T>
Node<T>* RedBlack<T>::getRoot(){
return this->root;
}
template<class T>
bool RedBlack<T>::leftRotate(Node<T> *test){
if(test->right==this->nil)
return false;
Node<T> *right = test->right;
test->right = right->left;
if(right->left!=this->nil)
right->left->parent = test;
right->parent = test->parent;
if(test->parent == this->nil)
this->root = right;
else if(test==test->parent->left)
test->parent->left = right;
else
test->parent->right = right;
right->left = test;
test->parent = right;
return true;
}
template<class T>
bool RedBlack<T>::rightRotate(Node<T> *test){
if(test->left==this->nil)
return false;
Node<T> *left = test->left;
test->left = left->right;
if(left->right!=this->nil)
left->right->parent = test;
left->parent = test->parent;
if(test->parent == this->nil)
this->root = left;
else if(test==test->parent->left)
test->parent->left = left;
else
test->parent->right = left;
left->right = test;
test->parent = left;
return true;
}
int main(){
RedBlack<int> *test = new RedBlack<int>();
test->insert(1);
test->insert(2);
test->insert(3);
test->insert(4);
test->insert(5);
test->insert(6);
test->insert(7);
test->insert(8);
test->remove(8);
test->remove(7);
test->remove(6);
test->remove(5);
test->remove(4);

cout<<"hight is: "<<test->getHight(test->getRoot())<<endl;
system("pause");
return 0;
}

posted on 2012-09-17 15:04  不懂自然语言  阅读(141)  评论(0)    收藏  举报