implemented Same
This commit is contained in:
parent
916def1c5e
commit
0566ee594f
|
@ -12,7 +12,7 @@ func Walk(t *tree.Tree, ch chan int) {
|
||||||
// that I can use the range-based forloop in main.main
|
// that I can use the range-based forloop in main.main
|
||||||
var walk func(t *tree.Tree)
|
var walk func(t *tree.Tree)
|
||||||
walk = func(t *tree.Tree) {
|
walk = func(t *tree.Tree) {
|
||||||
if t == nil {
|
if t != nil {
|
||||||
walk(t.Left)
|
walk(t.Left)
|
||||||
ch <- t.Value
|
ch <- t.Value
|
||||||
walk(t.Right)
|
walk(t.Right)
|
||||||
|
@ -24,15 +24,27 @@ func Walk(t *tree.Tree, ch chan int) {
|
||||||
|
|
||||||
// Same determines whether the trees
|
// Same determines whether the trees
|
||||||
// t1 and t2 contain the same values.
|
// t1 and t2 contain the same values.
|
||||||
func Same(t1, t2 *tree.Tree) bool
|
func Same(t1, t2 *tree.Tree) bool {
|
||||||
|
ch1 := make(chan int)
|
||||||
|
ch2 := make(chan int)
|
||||||
|
go Walk(t1, ch1)
|
||||||
|
go Walk(t2, ch2)
|
||||||
|
for i := range ch1 {
|
||||||
|
if i != <-ch2 {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
t := tree.New(1)
|
t := tree.New(1)
|
||||||
ch := make(chan int)
|
ch := make(chan int)
|
||||||
go Walk(t, ch)
|
go Walk(t, ch)
|
||||||
fmt.Printf("%v\n", t)
|
|
||||||
for i := range ch {
|
for i := range ch {
|
||||||
fmt.Printf("%d, ", i)
|
fmt.Printf("%d, ", i)
|
||||||
}
|
}
|
||||||
fmt.Println()
|
fmt.Println()
|
||||||
|
fmt.Println(Same(tree.New(1), tree.New(1)))
|
||||||
|
fmt.Println(Same(tree.New(1), tree.New(2)))
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue