【问题标题】:C++ Binary Search Tree Recursive search functionC++二叉搜索树递归搜索函数
【发布时间】:2016-11-01 09:39:22
【问题描述】:
template <class T>
bool BST<T>::search(const T& x, int& len) const
{
    return search(BT<T>::root, x);
}


template <class T>
bool BST<T>::search(struct Node<T>*& root, const T& x)
{
   if (root == NULL)
       return false;
   else
      {
         if (root->data == x)
             return true;
         else if(root->data < x)
             search(root->left, x);
         else 
             search(root->right, x);                 
      }             
}

这是我的搜索功能,用于搜索带有 T 节点的 BST 类。 x 是在树中搜索的数据,len 只是它必须经过的节点数量才能找到匹配节点(如果存在)。我还没有实现这一点,我只是在逐步发展我的任务。我通过这样做来调用它:

if(t.search(v[1], len) == true)
       cout << endl << "true";

v 只是我必须创建的一个向量来比较它,所以这只是为它提供一个 int。我得到的错误:

BST.h: In member function âbool BST<T>::search(const T&, int&) const [with T = int]â:
prog5.cc:24:   instantiated from here    
BST.h:78: error: no matching function for call to âBST<int>::search(Node<int>* const&, const int&) constâ    
BST.h:76: note: candidates are: bool BST<T>::search(const T&, int&) const [with T = int]
BST.h:83: note:                 bool BST<T>::search(Node<T>*&, const T&) [with T = int]

所以我不确定自己做错了什么或哪里做错了。

【问题讨论】:

  • 大量信息丢失。什么是 BT::root? BST和BT是一回事吗?节点和节点是一回事吗?请剪切粘贴,请勿输入。
  • 好吧,这是派生函数的一部分,所以要传递根节点,我必须专门告诉它 BT::root 否则我会出错。
  • 如果我不这样做,我得到的 root 没有在这个范围内声明。

标签: c++ binary-search-tree


【解决方案1】:

好的,bool BST&lt;T&gt;::search(struct Node&lt;T&gt;*&amp; root, const T&amp; x) 后面可能应该有 const,如下所示:bool BST&lt;T&gt;::search(struct Node&lt;T&gt;*&amp; root, const T&amp; x) const。基本上,您已经从 const 函数调用了非常量函数,这是不行的。

顺便说一句,这对我来说看起来很可疑“struct Node&lt;T&gt;*&amp;”...我可能会放弃 & 并使用 Node&lt;T&gt;*... 但也许您需要它,因为 struct ?

另外,这是 C++,没有理由将 Node 保留为结构......需要在参数定义中包含 struct 看起来很糟糕,恕我直言。为什么不让 Node 成为一个类?

【讨论】:

  • 我粘贴了完整的消息。但是之前的格式不太好,所以我把它删掉了,但我又重新输入了。
  • 它是一个节点类,但出于某种原因,为了能够以这种方式访问​​它,我不得不在插入和搜索函数中将其设为结构。这是通过维基百科和我的教授提供给我的
【解决方案2】:

您的搜索代码存在多个问题:

  • 排序顺序是倒序的,如果节点数据比你搜索的少,你应该在右分支中搜索,而不是左分支。

  • 你应该返回递归调用的结果

  • 还不清楚您为什么通过引用传递root。它应该作为 const 限定指针传递,方法主体也应该是 const 限定的。

这里有一个替代方案:

template <class T>
bool BST<T>::search(const struct Node<T> *root, const T& x) const {
    if (root == NULL)
        return false;
    else
    if (root->data == x)
        return true;
    else
    if (root->data < x)
        return search(root->right, x);
    else 
        return search(root->left, x);
}

这是一个更简单的非递归实现:

template <class T>
bool BST<T>::search(const struct Node<T> *root, const T& x) const {
    while (root != NULL) {
        if (root->data == x)
            return true;
        if (root->data < x)
            root = root->right;
        else 
            root = root->left;
    }
    return false;
}

【讨论】:

  • 感谢您提供非递归版本。
【解决方案3】:

算法:

  1. 取节点值数据;
  2. 重复第 3 步到第 5 步,直到找到值或超出树。
  3. 如果数据等于根节点值,则搜索成功并终止算法。
  4. 如果数据小于根节点值,我们必须搜索左子树。
  5. 否则数据小于根节点值,我们要搜索左子树。
  6. 输出打印消息“找到”或“未找到”。

C++ 实现

    node* search(node* root, int data)
    {
     if (root==NULL || root->data==data) return root;

     if (root->data < data)   return search(root->right, data);

     return search(root->left, data);
   }

【讨论】:

  • 你能添加一些上下文吗?
  • Tnq 发现我的错误。马库斯·穆勒
猜你喜欢
  • 1970-01-01
  • 2013-04-14
  • 1970-01-01
  • 2014-01-02
  • 1970-01-01
  • 2021-07-03
  • 2019-04-17
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多