【发布时间】:2014-03-31 07:56:28
【问题描述】:
我正在编写一个二叉搜索树,这个名为 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);
}
当我用新的更新节点递归调用搜索函数时,会发生段错误。二叉树初始化为 100 个值,并从根开始搜索。
【问题讨论】:
标签: c++ recursion segmentation-fault binary-tree binary-search-tree