【发布时间】:2012-10-24 05:39:42
【问题描述】:
我正在尝试为二叉搜索树创建广度优先搜索功能,但我似乎无法使其工作。任何指针将不胜感激!
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。这是一个运行时错误。它只是陷入了循环。
标签: c++ binary-search-tree breadth-first-search