二叉搜索树:搜索功能问题
Binary Search Tree: Search Function Issue
我已经实现的搜索功能有点工作。当我搜索不存在的值时,搜索函数会查找并返回 false。当我搜索一个作为根的值时,它工作正常并返回 true。问题是当我搜索树中已经存在的根以外的值时,但它返回 false。关于我做错了什么的任何想法?
template <class Comparable>
bool BinarySearchTree<Comparable>::findValue(const Comparable& value){
if(root->element == value){
return true;
}
if(value > root->element)
{
if(root->right != NULL)
{
root->right->findValue(value);
}
else
{
return false;
}
}
if(value < root->element)
{
if(root->left != NULL)
{
root->left->findValue(value);
}
else
{
return false;
}
这些是我的私人数据成员,这些不能以任何方式更改。
private:
struct BinaryNode
{
Comparable element;
BinarySearchTree<Comparable> *left;
BinarySearchTree<Comparable> *right;
};
BinaryNode *root;
};
当你从右节点return false
时,你甚至从不尝试左节点。
尝试返回 true;
if(value > root->element)
{
if(root->right != NULL)
{
if(root->left->findValue(value))
{
return true;
}
}
}
if(value < root->element)
{
if(root->left != NULL)
{
if(root->left->findValue(value)
{
return true;
}
}
}
return false
你应该返回 root->left->findValue(value);
和 root->right->findValue(value)
的结果
当前,您正在为这些节点调用 findValue 函数,但不存储值或将其返回到任何位置,因此结果会丢失。
相关文章:
- 在用于格式4的arm模拟器中实现功能时的一个问题
- 解析模板的好友功能时出现问题
- creat_list2功能有什么问题?
- 此工厂功能有什么问题?
- 具有sleep_for和sleep_until功能的 gcc 可能存在的问题
- Sinlge 链表,C++,删除所有和搜索功能的问题
- VS2019中内联功能的一些问题
- 反向指针功能问题
- C++ lower_bound比较功能问题
- C 模板功能问题与具体化
- POP_FROND双重链接列表功能问题
- 链接列表中的搜索功能问题
- 二叉搜索树:搜索功能问题
- 公众成员功能问题
- 面临功能问题的问题
- OpenCV-SURF功能问题
- C++中的虚拟功能问题
- 哈希表添加和打印功能问题
- 对vtable的未定义引用;虚拟功能问题
- 热狗站静态功能问题