我正在寫一個二叉搜索樹,這個稱爲Search的函數獲取一個值x並搜索樹中的節點並返回它是否爲葉。這段代碼爲什麼會給我一個分段錯誤?
bool search(Node<T>* ¤tNode, const T& x) const
{
//~ cout << "CURRENT NODE DATA: " << currentNode->data << " : ";
/* FUNCTION: Searches for variable that is passed in X and checks if this value is a leaf or not */
//Left Subtree Search
if (x < binTree<T>::root->data)
{
if ((leaf(currentNode)) == true)
{
return true;
}
else
{
search(currentNode->left, x);
}
}
//Right Subtree Search
else if (x >= binTree<T>::root->data)
{
//If node in right subtree is a node check
if ((leaf(currentNode)) == true)
{
return true;
}
else
{
search(currentNode->right, x);
}
}
//Return false if the node is not a leaf
return false;
} //END OF SEARCH FUNCTION
bool leaf(Node<T>* currentNode) const
{
return ((currentNode->left == nullptr && currentNode->right == nullptr) ? true : false);
}
當我用新的更新節點遞歸調用搜索功能時會發生seg故障。二叉樹用100個值初始化,並開始在根目錄搜索。