有关红黑树的问题
#ifndef _MY_SET_H_
#define _MY_SET_H_
#include <assert.h>
template<typename T>
class MySet
{
private:
typedef struct Node
{
T value;
Node* parent;
Node* left;
Node* right;
int color; //0:black 1:red
}*PNode;
public:
class Iterator
{
public:
private:
PNode ptr;
};
public:
MySet()
{
Nullpoint = new Node;
Nullpoint->left = Nullpoint->right = Nullpoint->parent = 0;
Nullpoint->color = 0;
Rootpoint = Nullpoint;
}
~MySet()
{}
bool remove(T val)
{
PNode prev = Nullpoint;
PNode temp = Rootpoint;
PNode z;
while(temp!=Nullpoint && temp->value!=val)
{
prev = temp;
if(temp->value > val)
temp = temp->left;
else
temp = temp->right;
}
if(temp == Nullpoint)
return false;
//case 3: 2 child tree
if(temp->left!=Nullpoint && temp->right!=Nullpoint)
{
PNode next = temp->right;//next 值为空cl.exe 在成功(无错误)时返回零;否则返回非零值。
prev = temp;
while(next->right!=Nullpoint)
{
prev = next;
next = next->left;
}
temp->value = next->value;//copy
temp = next;
}
//case leaf
if(temp->left==Nullpoint && temp->right==Nullpoint)
{
if(prev == Nullpoint)// 根节点是叶子节点
{
Rootpoint = Nullpoint;
delete temp;
return true;
}
else
{
if(prev->left == temp)
prev->left = Nullpoint;
else
prev->right = Nullpoint;
z = Nullpoint;
Nullpoint->parent = prev;
}
}
else //case 1 child tree
{
z = (temp->left!=Nullpoint)?temp->left:temp->right;
if(prev == Nullpoint)
Rootpoint = z;
else
{
if(prev->left == temp)
prev->left = z;
else
prev->right = z;
}
z->parent = prev;
}
if(temp->color == 0)
{
Delete_Fixup(z);
}
delete temp;
return true;
}
//set mutilset
bool Insert(T val)
{
PNode newnode = new Node;
newnode->value = val;
newnode->left = newnode->right = Nullpoint;
newnode->color = 1;
PNode prev = Nullpoint;
PNode temp = Rootpoint;
while(temp != Nullpoint)
{
prev = temp;
if(temp->value > val)
temp = temp->left;
else if(temp->value < val)
temp = temp->right;
else
{
delete newnode;
return false;
}
}
if(prev == Nullpoint)
{
Rootpoint = newnode;
Rootpoint->parent = Nullpoint;
Rootpoint->color = 0;
}
else
{
if(prev->value > val)
prev->left = newnode;
else
prev->right = newnode;
newnode->parent = prev;
Insert_Fixup(newnode);
}
return true;
}
private:
void Delete_Fixup(PNode x)
{
PNode Parent;
PNode Brother;
while(x->color == 0)
{
if(x == Rootpoint)
return;
Parent = x->parent;
Brother = (Parent->left==x)?Parent->right:Parent->left;
/////case 1;
if(Brother->color == 1)
{
Parent->color = 1;
Brother->color = 0;
if(Parent->left == x)
Left_Rotate(Brother, Parent);
else
Right_Rotate(Brother, Parent);
continue;
}
//////case 2 3 4:
else
{
////case 2
if(Brother->left->color == 0 && Brother->right->color == 0)
{
Brother->color = 1;
x = Parent;
continue;
}
// case 3:
if((Parent->left == x && Brother->left->color == 1 && Brother->right->color ==0)
||(Parent->right == x && Brother->right->color == 1 && Brother->left->color == 0))
{
Brother->color = 1;
if(Parent->left == x)
{
Brother->left->color = 0;
Right_Rotate(Brother->left, Brother);
}
else
{
Brother->right->color = 0;
Left_Rotate(Brother->right, Brother);
}
Brother = (Parent->left==x)?Parent->right:Parent->left;
}
// case 4:
if((Parent->left == x && Brother->right->color == 1)||
(Parent->right == x && Brother->left->color == 1))
{
swap(Brother->color, Parent->color);
if(Parent->left == x)
{
Left_Rotate(Brother, Parent);
Brother->right->color = 0;
}
else
{
Right_Rotate(Brother, Parent);
Brother->left->color = 0;
}
return;
}
}
}
x->color = 0;
}
void Insert_Fixup(PNode z)
{
PNode _parent = z->parent; //
assert(_parent!=Nullpoint);
PNode _grandfather;
PNode _uncle;
while(_parent->color == 1)
{
_grandfather = _parent->parent;
assert(_grandfather != Nullpoint);
assert(_grandfather->color ==0 );
_uncle = (_grandfather->left==_parent)?_grandfather->right:_grandfather->left;
if(_uncle->color == 1) //case 1:
{
_uncle->color = 0;
_parent->color = 0;
_grandfather->color = 1;
z = _grandfather;
_parent = z->parent;
}
else //_uncle->color = 0
{
if((_parent->left==z && _grandfather->right==_parent) || (_parent->right==z && _grandfather->left==_parent))
{
if(_parent->left == z)
{
Right_Rotate(z, _parent);
}
else
{
Left_Rotate(z, _parent);
}
PNode temp = z;
z = _parent;
_parent = temp;
}
if((_parent->left==z && _grandfather->left==_parent) || (_parent->right==z && _grandfather->right==_parent))
{
_parent->color = 0;
_grandfather->color = 1;
if(_parent->left == z)
Right_Rotate(_parent, _grandfather);
else
Left_Rotate(_parent, _grandfather);
}
return;
}
}
if(Rootpoint == z)
z->color = 0;
}
void Left_Rotate(PNode y, PNode x)
{
PNode z = x->parent;
if(z == Nullpoint)
Rootpoint = y;
else
{
if(z->left == x)
z->left = y;
else
z->right = y;
}
y->parent = z;
x->right = y->left;
y->left->parent = x;
y->left = x;
x->parent = y;
}
void Right_Rotate(PNode x, PNode y)
{
PNode z = y->parent;
if(z == Nullpoint)
Rootpoint = x;
else
{
if(z->left == y)
z->left = x;
else
z->right = x;
}
x->parent = z;
y->left = x->right;
x->right->parent = y;
x->right = y;
y->parent = x;
}
private:
PNode Rootpoint;
PNode Nullpoint;
};
#endif
上面的是红黑代码但会在bool remove(T val)中出错!我编译时说next值是空的!不知道为什么!
请教大大们帮我看看!