바이너리 검색 트리에서 leaf node
을 삭제하려고하는데 저에게 맞지 않습니다. 코드가 디버깅되어 문제를 찾을 수 없습니다. 나는 흐름이 정확하다는 것을 볼 수 있고, 전화는 leaf node
주소에 도달하고, free
을 호출한다. 하지만 그 후 나는 pre-order
을 통과 할 때 가치가 여전히 존재한다는 것을 알게됩니다.이진 트리에서 리프 노드를 삭제하십시오.
진 나무는 내가 (그 간단한 일) 생성 - = 14
삭제 될
10
/ \
6 14
Leaf Node
값을; 삭제 Pre - order
탐색 결과 전
= 10->6->14
. 이것은 내 콘솔에 인쇄되어 있습니다.
// Delete a leaf node
void deleteNode(struct Nodes * root,int value){
// Check if root is null
if (root == NULL) {
return;
}
// If no left and right node present, it's a leaf node. Perform delete.
while (root->left == NULL && root->right == NULL) {
// Check if value at leaf node is same as value to be deleted. If yes, go inside (if).
if (root->info == value) {
printf("Delete the leaf node \n");
printf("delete node address is \n %p",root);
// free the root (which is currently a leaf node)
free(root);
return;
}
}
// keep checking if value to be deleted is on right or left, till a value is found.
if (root->info < value) {
// Ccheck on right
deleteNode(root->right,value);
}else{
// check on left
deleteNode(root->left,value);
}
}
내가 어떤 errors
하지 않는, 그래서 내가 근본 원인를 추측 할 수없는입니다 : -
코드 leaf node
은 삭제합니다. 삭제 Pre - order
탐색 결과 후
= 10->6->14
. 누구든지 나를 도울 수 있습니까? 나는 매우 어리석은 실수를하고 있다는 것을 알고있다, 또는 나의 개념은 아직도 맑지 않다. 감사합니다.
기타 정보가 필요한 경우 알려 주시기 바랍니다.
출력 이미지 : 올바른 값과 동일한 주소를 볼 수 있습니다.
어쨌든'free()'-d 노드를'NULL'로 설정해야 할 수도 있습니다. 그렇다면 잘못된 메모리를 읽을 것입니다. –
@SouravGhosh 나는 루트 = NULL을 시도했다; 그러나 같은 결과. 잘못된 메모리를 확인하려면 어떻게합니까? –
C는 값으로 전달을 사용합니다 .... –