我正在写一个二叉搜索树,这个称为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个值初始化,并开始在根目录搜索。