读书人

请问二叉树的删除算法

发布时间: 2012-02-05 12:07:15 作者: rapoo

请教二叉树的删除算法
下面是代码一直有错,是逻辑错误,请教下大家,看了很久一直没找出来
void DS::Destroy(FSS *BRoot)
{
if(BRoot)
{
Destroy(BRoot->child); //销毁左子树
Destroy(BRoot->brother); //销毁右子树
delete BRoot; //释放根结点
BRoot=NULL;
}
}

//删除current的左子树
void DS::DeleteL(FSS*current)
{
Destroy(current->child);
}

//删除current的右子树
void DS::DeleteR(FSS *current)
{
Destroy(current->brother);
}

void DS::DeleteDS(FSS *FS,string str)
{
if(FS)
{
if(FS->child->data.name==str)
{
DeleteL(FS);
FS->child=NULL;
}
else if(FS->brother->data.name==str)
{
DeleteL(FS->brother);
FStructure *p;
p=FS->brother;
FS->brother=p->brother;
delete p;
}
DeleteDS(FS->child,str);
DeleteDS(FS->brother,str);
}
}

[解决办法]
DeleteDS函数没有判断child是否为NULL就去取了data.name,可能会出core。对brother的处理同理。

[解决办法]
void DS::Destroy(FSS *&BRoot)

读书人网 >C++

热点推荐