有关红黑树的问题

x911420x 2009-07-09 02:19:48
#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值是空的!不知道为什么!
请教大大们帮我看看!

...全文
73 回复 打赏 收藏 转发到动态 举报
写回复
用AI写文章
回复
切换为时间正序
请发表友善的回复…
发表回复

16,551

社区成员

发帖
与我相关
我的任务
社区描述
VC/MFC相关问题讨论
社区管理员
  • 基础类社区
  • Creator Browser
  • encoderlee
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

        VC/MFC社区版块或许是CSDN最“古老”的版块了,记忆之中,与CSDN的年龄几乎差不多。随着时间的推移,MFC技术渐渐的偏离了开发主流,若干年之后的今天,当我们面对着微软的这个经典之笔,内心充满着敬意,那些曾经的记忆,可以说代表着二十年前曾经的辉煌……
        向经典致敬,或许是老一代程序员内心里面难以释怀的感受。互联网大行其道的今天,我们期待着MFC技术能够恢复其曾经的辉煌,或许这个期待会永远成为一种“梦想”,或许一切皆有可能……
        我们希望这个版块可以很好的适配Web时代,期待更好的互联网技术能够使得MFC技术框架得以重现活力,……

试试用AI创作助手写篇文章吧