0
BST에서 노드 삭제 기능에 문제가 있습니다. 다른 모든 함수는 잘 작동하는지 테스트했지만 노드를 삭제 한 후 트리를 인쇄하면 세그먼트 화 오류가 발생합니다.
여기 삭제 내 코드입니다 :BST에서 노드 삭제
struct Tree
{
int value;
struct Tree *lchild;
struct Tree *rchild;
}*root;
void print (struct Tree *t)
{
if (!t) return;
if(t->lchild != NULL)
print(t->lchild);
printf("%d\n",t->value);
if(t->rchild != NULL)
print(t->rchild);
}
내 용의자가 노드를 인쇄에 원인이 문제를 null로 설정하지 방법을 몇 가지 있습니다 :
여기struct Tree * findinorder (struct Tree *t)
{
while (t->lchild != NULL)
t=t->lchild;
return t;
}
bool deleteNode (struct Tree *t, int fvalue)
{
bool find = false; //this is to check if node is already found don't go to right sub tree
struct Tree *inorder;
if (t==NULL) return find;
if (t->value == fvalue)
{
if (t->lchild == NULL && t->rchild == NULL)
{
free(t);
}
else {
if (t->lchild ==NULL)
t= t->rchild;
else {
if (t->rchild ==NULL)
t= t->lchild;
else {
inorder = findinorder(t->rchild);
t->value = inorder->value;
free(inorder);
}
}
}
return true;
}
find = deleteNode(t->lchild, fvalue);
if (!find)
find = deleteNode(t->rchild, fvalue);
return find;
}
인쇄에 대한 트리 구조와 기능입니다 계속.
도와주세요.
rchild
/
lchild
에 새 참조를 업데이트 할 수 있도록 이전 노드에 대한 포인터를 유지해야