C++ 显示二叉搜索树中节点的路径
我正在努力显示从BST的根节点到目标节点的路径。我使用的函数在前两层中运行良好,但之后会出现问题。例如,测试编号为6、9、4、11、10(按该顺序插入)。如果我搜索6、9或4,它会工作(例如:“6 9”)。但如果我尝试11或10,它会同时显示它们,并且不符合顺序。我有点不明白为什么。任何想法都会很棒C++ 显示二叉搜索树中节点的路径,c++,binary-search-tree,C++,Binary Search Tree,我正在努力显示从BST的根节点到目标节点的路径。我使用的函数在前两层中运行良好,但之后会出现问题。例如,测试编号为6、9、4、11、10(按该顺序插入)。如果我搜索6、9或4,它会工作(例如:“6 9”)。但如果我尝试11或10,它会同时显示它们,并且不符合顺序。我有点不明白为什么。任何想法都会很棒 template <class T> void BST<T>::displayPath(T searchKey, BST<T> *node) { if (
template <class T>
void BST<T>::displayPath(T searchKey, BST<T> *node)
{
if (searchKey == node->mData)
{
cout << node->mData << " ";
}
else if (searchKey < node->mData )
{
cout << node->mData << " ";
displayPath(searchKey, node->mLeft);
}
else// (searchKey > node->mData)
{
cout << node->mData << " ";
displayPath(searchKey, node->mRight);
}
}
模板
void BST::displayPath(T searchKey,BST*节点)
{
if(searchKey==node->mData)
{
cout-mData-mData)
{
cout-mData-mLeft);
}
else/(搜索键>节点->mData)
{
无法读取数据(右侧);
}
}
这是插入函数。数字按上面的顺序插入
template <class T>
void BST<T>::insert(BST<T> *&node, T data)
{
// If the tree is empty, make a new node and make it
// the root of the tree.
if (node == NULL)
{
node = new BST<T>(data, NULL, NULL);
return;
}
// If num is already in tree: return.
if (node->mData == data)
return;
// The tree is not empty: insert the new node into the
// left or right subtree.
if (data < node->mData)
insert(node->mLeft, data);
else
insert(node->mRight, data);
}
模板
void BST::insert(BST*&节点,T数据)
{
//如果树为空,则创建一个新节点并将其
//树根。
if(node==NULL)
{
节点=新BST(数据,NULL,NULL);
返回;
}
//如果num已经在树中:return。
如果(节点->mData==数据)
返回;
//树不是空的:将新节点插入
//左或右子树。
if(数据mData)
插入(节点->mLeft,数据);
其他的
插入(节点->右侧,数据);
}
您的代码证实了我的怀疑。您的方法很好-这是您通过按以下顺序插入6、9、4、11、10构建的树:
第一(6):
第二(9)
第三(4)
第4(11)条:
第5(10)条:
因此,搜索10,将得到路径(6,9,11,10)
请注意,从根到BST中某个元素的路径不一定会被排序—如果这是您所期望的。事实上,只有当节点位于树中最右边叶的路径上时,才会对其进行排序
代码中的另一个问题:搜索7(或树中不存在的任何元素)会给您提供一些虚假路径。您的代码证实了我的怀疑。您的方法很好-这是您通过按以下顺序插入6、9、4、11、10构建的树:
第一(6):
第二(9)
第三(4)
第4(11)条:
第5(10)条:
因此,搜索10,将得到路径(6,9,11,10)
请注意,从根到BST中某个元素的路径不一定会被排序—如果这是您所期望的。事实上,只有当节点位于树中最右边叶的路径上时,才会对其进行排序
代码中的另一个问题:搜索7(或树中不存在的任何元素)会给您提供一些虚假路径。只有当输入值在树中时,代码才会工作。当您搜索不在树中的值时,您正在查找不在树中的节点
template <class T>
void BST<T>::displayPath(T searchKey, BST<T> *node)
{
if (searchKey == node->mData)
{
cout << node->mData << " ";
}
else if (searchKey < node->mData )
{
cout << node->mData << " ";
if (node->mLeft == NULL) // When trying to access a node that isn't there
cout << "Not Found\n";
else
displayPath(searchKey, node->mLeft);
}
else // (searchKey > node->mData)
{
cout << node->mData << " ";
if (node->mRight == NULL)
cout << "Not Found\n";
else
displayPath(searchKey, node->mRight);
}
}
模板
void BST::displayPath(T searchKey,BST*节点)
{
if(searchKey==node->mData)
{
cout-mData-mData)
{
cout mData mLeft==NULL)//尝试访问不存在的节点时
库特姆利夫特);
}
else/(搜索键>节点->mData)
{
cout mData mRight==NULL)
(右),;
}
}
只有当输入值在树中时,代码才会起作用。当您搜索不在树中的值时,您正在查找不在树中的节点
template <class T>
void BST<T>::displayPath(T searchKey, BST<T> *node)
{
if (searchKey == node->mData)
{
cout << node->mData << " ";
}
else if (searchKey < node->mData )
{
cout << node->mData << " ";
if (node->mLeft == NULL) // When trying to access a node that isn't there
cout << "Not Found\n";
else
displayPath(searchKey, node->mLeft);
}
else // (searchKey > node->mData)
{
cout << node->mData << " ";
if (node->mRight == NULL)
cout << "Not Found\n";
else
displayPath(searchKey, node->mRight);
}
}
模板
void BST::displayPath(T searchKey,BST*节点)
{
if(searchKey==node->mData)
{
cout-mData-mData)
{
cout mData mLeft==NULL)//尝试访问不存在的节点时
库特姆利夫特);
}
else/(搜索键>节点->mData)
{
cout mData mRight==NULL)
(右),;
}
}
此代码看起来不错。您的树是否有可能构造错误?发布的代码没有问题。可能错误出现在构建BST的代码中。这毕竟是一个更复杂的代码。代码中存在另一个问题-当您搜索不存在的节点时,将打印一个虚假路径。我假设问题在这里是树插入-我怀疑你总是以叶子的形式插入元素,而不是保持树的平衡。记住-有很多方法可以为某一组元素构建BST。因此-顺序不是你期望的-而是实际上树的实际路径。@amit对我来说BST指的是二进制搜索树,而不是平衡搜索树ree.啊,我现在明白了。正如你们所说,我只是对树的布局感到困惑,因为缺乏平衡。谢谢!这段代码看起来很好。你的树有没有可能构造错误?发布的代码没有问题。可能错误在构建BST的代码中。这毕竟是更复杂的代码。这是一个不同的问题代码-当你搜索一个不存在的节点时,会打印出一个虚假的路径。我假设这里的问题是树的插入-我怀疑你总是以叶子的形式插入元素,而不是保持树的平衡或什么的。记住-有很多方法可以为某一组元素构建BST。因此-顺序不是你想要的u expect-但实际上是树的实际路径。@amit对我来说BST是指二叉搜索树,而不是平衡搜索树。啊,我现在明白了。正如你们所说,我只是对树的布局感到困惑,因为缺乏平衡。谢谢!
6
/ \
4 9
6
/ \
/ \
4 9
\
\
11
6
/ \
/ \
4 9
\
\
11
/
/
10
template <class T>
void BST<T>::displayPath(T searchKey, BST<T> *node)
{
if (searchKey == node->mData)
{
cout << node->mData << " ";
}
else if (searchKey < node->mData )
{
cout << node->mData << " ";
if (node->mLeft == NULL) // When trying to access a node that isn't there
cout << "Not Found\n";
else
displayPath(searchKey, node->mLeft);
}
else // (searchKey > node->mData)
{
cout << node->mData << " ";
if (node->mRight == NULL)
cout << "Not Found\n";
else
displayPath(searchKey, node->mRight);
}
}