【问题标题】:binary tree search for string c++二叉树搜索字符串c ++
【发布时间】:2017-04-17 11:56:55
【问题描述】:

我必须在二叉树中实现字符串搜索方法。它以node*为输入,再次输出node*。

问题是由于某些指针异常而无法正常工作,它给了我错误。

如果我在某些时候不清楚,请告诉我。

提前谢谢你

-var-create: 无法创建变量对象错误

zoo_tree::node* tree_tools::search(zoo_tree::node* from,string animal) {
if (from != NULL) {
    if (from->question == animal) {
        return from;
    }
    if (from->question != animal) {
        search(from->left, animal);
        search(from->right, animal);

    }
}
return NULL;

}

但是,上面的代码可以工作,有什么区别?

zoo_tree::node* tree_tools::search(zoo_tree::node* from,string animal) {

if (from == NULL)
    return NULL;

if (from->question == animal)
    return from;

if (from->question != animal)
{
    search(from->left, animal);
    search(from->right, animal);
}

}

【问题讨论】:

    标签: c++ recursion binary-tree binary-search-tree


    【解决方案1】:

    您的代码中有明显错误:对search() 的递归调用不会在实例不为空时返回实例,因为递归调用必须递归返回。这意味着除非根是搜索到的节点,否则将返回 NULL。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-04-02
      • 1970-01-01
      • 2017-12-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多