我想为二叉搜索树制作宽度优先的搜索函数,但我似乎无法使其工作。任何指针将不胜感激!二进制搜索树的宽度优先搜索
template <class T>
bool BST<T>::displayBfs(T searchKey, BST<T> *node)
{
BST<T> *tmp = node;
queue <int> queue;
queue.push(node->mData);
if (node == NULL)
{
return false;
}
while (!queue.empty())
{
queue.pop();
if (tmp->mData == searchKey)
return true;
else
{
if(tmp->mLeft != NULL)
queue.push(tmp->mLeft->mData);
if(tmp->mRight != NULL)
queue.push(tmp->mRight->mData);
}
}
return false;
}
哪里定义了'tmp'? – codaddict
任何症状?它是编译还是运行时错误? – Lazin
对不起,我忘了我意外删除了那条线。现在定义tmp。 这是一个运行时错误。它只是陷入循环。 –