0
我目前正在研究二叉搜索樹的C++實現。一切看起來都很完美,但我的搜索功能給了我很多問題。C++ Segmentation fault BST
BinarySearchTree::node* BinarySearchTree::SEARCH(node* x, int key)
{
if(root == NULL) {
cout << "This is an empty tree." << endl;
return NULL;
} else {
if(x->key == key) {
return x;
}
if(x == NULL) {
cout << "Value not in tree." << endl;
return x;
}
if(key < x->key) {
return SEARCH(x->left, key);
} else {
return SEARCH(x->right, key);
}
}
}
這給了我一個分段錯誤每次我要尋找的,是不是在樹中的鍵值時,當節點值爲NULL(如值,這將是無論是最大,或者如果它閔被包括在內)。
聽起來像你需要使用調試器。並且在解除引用後檢查'x'是否爲空。 –
我從來沒有在UNIX環境中使用調試器,假設我應該找出如何通過現在= P來做到這一點。 – DJWright97