// So I call this function after deleting a node.
// It works before I delete the node, but for some reason
// after I perform a deletion and update the tree it runs into
// EXC_BAD_ACCESS on the line below...
void BinaryTree::updateCost(BinaryNode *root) {
if (root != NULL)
root->updateCostRecursively(1);
}
void BinaryNode::updateCostRecursively(int newCost) {
cout << this << endl; // prints 0x3000000000000000 before the bad access
cost = newCost; // has a bad access here
if (right != NULL)
right->updateCostRecursively(newCost + 1);
if (left != NULL)
left->updateCostRecursively(newCost + 1);
}
매번 포인터를 확인할 때도 NULL 개체에서이 재귀 함수가 호출되는 이유는 무엇입니까?NULL 검사 후 포인터에서 재귀 함수가 호출되는 이유는 무엇입니까?
아래 노드를 삭제하는 데 사용하는 코드를 복사했습니다. 나는 여전히 재귀 함수를 이해하는 데 어려움을 겪고있다. 그러나 어떤 시점에서도 알 수없는 것에서 나는 매달려있는 포인터를 떠난다.
BinaryNode *BinaryTree::findMin(BinaryNode *t) {
if (t == NULL) return NULL;
while (t->left != NULL) t = t->left;
return t;
}
BinaryNode *BinaryTree::removeMin(BinaryNode *t) {
if (t == NULL) return NULL;
if (t->left != NULL)
t->left = removeMin(t->left);
else {
BinaryNode *node = t;
t = t->right;
delete node;
}
return t;
}
bool BinaryTree::remove(int key) {
if (root != NULL && remove(key, root))
return true;
return false;
}
BinaryNode *BinaryTree::remove(int x, BinaryNode *t) {
if (t == NULL) return NULL;
if (x < t->key)
t->left = remove(x, t->left);
else if (x > t->key)
t->right = remove(x, t->right);
else if (t->left != NULL && t->right != NULL) {
// item x is found; t has two children
t->key = findMin(t->right)->key;
t->right = removeMin(t->right);
} else { //t has only one child
BinaryNode *node = t;
t = (t->left != NULL) ? t->left : t->right;
delete node;
}
updateCost(root);
return t;
}
NULL이 아니기 때문에? 'delete'는 포인터를 수정하지 않는다. 그게 문제가 아니라면 [최소한의 테스트 케이스] (http://sscce.org)를 만드십시오. –
0x3000000000000000! = 0 – fbafelipe