【问题标题】:Why we need "return" in recursions when we need to return a value?当我们需要返回一个值时,为什么我们需要在递归中“返回”?
【发布时间】:2019-06-14 04:58:38
【问题描述】:

我对递归问题中的 return 关键字感到很困惑。例如,下面的代码是关于检查一个节点是否在二叉搜索树中。在 contains 方法中,我可以知道为什么我们需要

中的“return”
return this.right.contains(data) 

return this.left.contains(data) 

? 我认为没有这个回报,只要我们打电话

this.right.contains(data) 

如果没有 return 关键字,它会根据节点数据返回“null”或这个节点。为什么我们需要再把 return 放在外面?

  if (this.data === data) {
        return this;
    }

  return null;

我认为我们已经将结果返回到外部层。此外,当我们在 javascript 中使用回调函数时,我们没有使用 return。我很混乱。非常感谢!

class Node {
 constructor(data) {
    this.data = data;
    this.left = null;
    this.right = null;
 }
 insert(data) {
    if (data < this.data && this.left) {
        this.left.insert(data);
    } else if (data < this.data) {
        this.left = new Node(data);
    } else if (data > this.data && this.right) {
        this.right.insert(data)
    } else if (data > this.data) {
        this.right = new Node(data);
    }
 }
 contains(data) {
    if (this.data === data) {
        return this;
    }
    if (this.data < data && this.right) {
        return this.right.contains(data)
    } else if (this.data > data && this.left) {
        return this.left.contains(data)
    }
    return null;

 }
}

【问题讨论】:

  • 在 JavaScript 中,没有return 的函数总是返回undefined。你需要return,因为这就是语言的工作原理。
  • 是的。如果你习惯了像 Ruby 这样隐含 return 的语言,那么 Javascript 就不是这样工作的。
  • 非常感谢!但是我很困惑,每当我们调用 this.right.contains(data) 时,它已经返回“this”或“null”,或者再次调用自身,所以它已经包含返回,为什么我们在调用它之前放置另一个返回?
  • @Ying 当函数调用自身时,它是一个正常的函数调用,就像其他任何函数一样。它不只是“再次跳到顶部”或其他东西,因此它可以稍后直接返回给调用者。不,当你return结果时,你只返回到递归调用的位置,代码继续在那里运行。

标签: javascript algorithm recursion data-structures return


【解决方案1】:

我试图通过使您的代码更加详细和记录在案来说明这一点。

contains(data) {
    if (this.data === data) {
        // This node has the data return this
        return this;
    }
    if (this.data < data && this.right) {
        // We need to search the right subtree
        // when the result is back we take it and put it in a variable
        const rightSubtreeSearchResult = this.right.contains(data);

        // we have the result, we just need to return it
        return rightSubtreeSearchResult;

    } else if (this.data > data && this.left) {
        // Same as above but for left subtree
        // this time we just return the result directly without
        // putting it in a variable. The resulting behavior is just as above
        return this.left.contains(data);

    }
    // None of the above return null
    return null;

}

this.right.contains(data) 返回一个值是对的,但是如果你只是调用该方法而不使用它的返回值,那么该值就在那里未被使用,直到函数执行完成然后它就消失了。

简单来说,如果不return,则leftright的搜索结果没有被使用,函数最后总是会到达return null,返回null,不正确搜索结果。

【讨论】:

  • 非常感谢!
【解决方案2】:

我想我们已经将结果返回到了外层。

没有。

这就是return 的真正目的:做到这一点。如果您没有return,则说明您没有这样做。

如果没有 return 关键字,它将根据节点数据返回“null”或此节点。为什么我们需要再把 return 放在外面?

在 JavaScript 中不是这样。在其他一些编程语言中可能是这样。

除了我们在javascript中使用回调函数时我们没有使用return

有时我们会这样做。这取决于您是否需要它们返回值。

所以,回答:

当我们需要返回一个值时,为什么我们需要在递归中“返回”?

因为return 就是这样做的,因此得名。

【讨论】:

  • 非常感谢!但是在 contains(data) 函数内部,我们有一个 return null 或 return this 语句,所以每当我们调用 contains(data) 时,它都会返回 null 或 this,或者调用自身也包含 return null 或自身,为什么我们仍然需要另一个回报它的结果?
  • 返回函数调用的结果?否则,您不会将其传递给调用者。 thisnull 都不是这个结果,那么为什么你认为它们就足够了?
  • 如果我们找到了节点,并且在“return this”之后,我可以知道节点返回给谁吗?而在return this.right.contains(data)之后,返回的节点又是谁呢?在“return this”之后将节点传递给当前层函数是否正确?并 return this.right.contains(data) 将节点传递给它的外层函数?或者 return this.right.contains(data) 是把节点传递到调用 contains(data) 的地方?所以“return this”只是结束当前函数,而不是out层函数?非常感谢!
猜你喜欢
  • 2022-01-17
  • 2021-08-23
  • 2019-10-29
  • 2020-02-04
  • 2022-01-07
  • 2021-01-17
  • 2020-02-04
  • 1970-01-01
  • 2017-08-05
相关资源
最近更新 更多