递归,不通过返回语句来结束递归。

huangapple go评论82阅读模式
英文:

Recursion, and not ending the recursion by return statement

问题

我正在进行二叉搜索树,并期望在结果为true时结束Find递归。它确实有一个true的结果,但即使它得到了true值并运行了return语句,似乎仍然继续运行,最终达到了false的值。

我该如何使程序在找到值并返回时结束?

以下是翻译的代码:

package main

import "fmt"

type Tree struct {
  Left  *Tree
  Value int64
  Right *Tree
}

func NewT(val int64) *Tree {
  return &Tree{
    Left:  new(Tree),
    Value: val,
    Right: new(Tree),
  }
}

func (T *Tree) Insert(val int64) *Tree {
  if T == nil {
    return &Tree{nil, val, nil}
  }
  if val < T.Value {
    T.Left = T.Left.Insert(val)
  } else {
    T.Right = T.Right.Insert(val)
  }
  return T
}

func (T *Tree) Find(val int64) bool {
  fmt.Printf("%v , %v\n", T.Value, val)
  fmt.Printf("%v\n", T.Value == val)

  if fmt.Sprintf("%v", T.Value) == fmt.Sprintf("%v", val) {
    fmt.Println("True and we do return true")
    return true
  }
  if val < T.Value {
    return T.Left.Find(val)
  } else {
    return T.Right.Find(val)
  }
}

func main() {
  t1 := NewT(5)
  for i := 0; i < 10; i++ {
    t1 = t1.Insert(int64(i))
  }
  fmt.Println("Result:", t1.Find(7))
}

输出结果为:

5 , 7
false
0 , 7
false
5 , 7
false
6 , 7
false
7 , 7
true
True and we do return true
英文:

I am doing binary search in this tree and expect the Find recursion to end when the result is true. It does have a result of true but even if it gets the true value and runs the return statement, it seems like continue to run and finally reach the value of false

How do I make this program end when it finds the value and returns?

http://play.golang.org/p/miWqRVo_XO

package main
import &quot;fmt&quot;
type Tree struct {
Left  *Tree
Value int64
Right *Tree
}
func NewT(val int64) *Tree {
return &amp;Tree{
Left:  new(Tree),
Value: val,
Right: new(Tree),
}
}
func (T *Tree) Insert(val int64) *Tree {
if T == nil {
return &amp;Tree{nil, val, nil}
}
if val &lt; T.Value {
T.Left = T.Left.Insert(val)
} else {
T.Right = T.Right.Insert(val)
}
return T
}
func (T *Tree) Find(val int64) bool {
fmt.Printf(&quot;%v , %v\n&quot;, T.Value, val)
fmt.Printf(&quot;%v\n&quot;, T.Value == val)
if fmt.Sprintf(&quot;%v&quot;, T.Value) == fmt.Sprintf(&quot;%v&quot;, val) {
fmt.Println(&quot;True and we do return true&quot;)
return true
}
if val &lt; T.Value {
T.Left.Find(val)
} else {
T.Right.Find(val)
}
fmt.Println(&quot;False&quot;)
return false
}
func main() {
t1 := NewT(5)
for i := 0; i &lt; 10; i++ {
t1 = t1.Insert(int64(i))
}
fmt.Println(&quot;Result:&quot;, t1.Find(7))
}

Output is

5 , 7
false
0 , 7
false
5 , 7
false
6 , 7
false
7 , 7
true
True and we do return true

答案1

得分: 4

你似乎忽略了在递归调用Tree.Find时的返回值。如果你传递返回值,应该能够得到期望的行为。也就是说,修改Find函数的结尾如下:

if val < T.Value {
return T.Left.Find(val)
} else {
return T.Right.Find(val)
}
英文:

You seem to be ignoring the return value of Tree.Find when you recurse into it. If you pass on the return value, you should get the desired behaviour. That is, modify the end of Find to read:

if val &lt; T.Value {
return T.Left.Find(val)
} else {
return T.Right.Find(val)
}

huangapple
  • 本文由 发表于 2014年3月20日 16:08:15
  • 转载请务必保留本文链接:https://go.coder-hub.com/22526942.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定