我已經實現了我的搜索功能。當我搜索一個不存在的值時,搜索功能可以查找並返回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;
};
我試過你說的,我得到了同樣的結果。 – Frontier