그래서 난이 구조체에 보이는 C에서 이진 검색 트리를 코딩 :이진 검색 트리
struct tnode
{
int content;
struct tnode *left; /* left tree part */
struct tnode *right; /* right tree part */
};
내 주요 방법 :
int main()
struct tnode *Baum = NULL;
struct tnode *tmpPos = NULL;
Baum = addelement (Baum, 32);
Baum = addelement(Baum, 50);
Baum = addelement(Baum, 60);
tmpPos = searchnode(Baum,50);
그래서 basicly이 나에게 3와 이진 검색 트리를 생성 (32, 50, 60)를 포함한다. 내 searchnode 메서드는 포인터를 "50"으로 이동시켜 나중에 삭제할 수 있습니다. 그러나 내 searchnode 메서드는 검색하는 요소가 내 이진 검색 트리의 루트 인 경우에만 포인터를 반환합니다.
는 searchnode :
struct tnode *searchnode(struct tnode *p, int nodtodelete)
{
if (p == NULL)
{
printf("Baum ist leer oder Element nicht vorhanden \n");
}
if (p -> content == nodtodelete)
{
return p;
}
if (p->content > nodtodelete)
{
searchnode (p->right, p->content);
}
if (p->content < nodtodelete)
{
searchnode(p->left, p->content);
}
}
아마 너희들은 나에게 도움이 될 수 있습니다.
searchnode (p->right, nodtodelete);
^
다른 재귀 호출에 같은 변화를 확인 :
왼쪽/오른쪽 하위 트리 결정을 취소하지 않았습니까? 'if (p-> content> nodtodelete)'라면 왼쪽 경로를 택해야합니다. –
사이트에 오신 것을 환영합니다! 품질 좋은 답변을 얻을 수있는 질문에 대한 자세한 내용은 [둘러보기] (https://stackoverflow.com/tour)를 확인하십시오. 'addelement()'가 올바르게 작동한다는 것을 이미 확인 했습니까? – cxw
'searchnode'는 4 개의'if' 중 3 번째에 아무것도 반환하지 않습니다. – halex