59 lines
831 B
Go
59 lines
831 B
Go
|
|
package main
|
||
|
|
|
||
|
|
import (
|
||
|
|
"golang.org/x/tour/tree"
|
||
|
|
"fmt"
|
||
|
|
)
|
||
|
|
|
||
|
|
// type Tree struct {
|
||
|
|
// Left *Tree
|
||
|
|
// Value int
|
||
|
|
// Right *Tree
|
||
|
|
// }
|
||
|
|
|
||
|
|
func Walk(t *tree.Tree, ch chan int) {
|
||
|
|
defer close(ch)
|
||
|
|
var walker func(t *tree.Tree)
|
||
|
|
walker = func (t *tree.Tree) {
|
||
|
|
if t == nil {
|
||
|
|
return
|
||
|
|
}
|
||
|
|
walker(t.Left)
|
||
|
|
ch <- t.Value
|
||
|
|
walker(t.Right)
|
||
|
|
}
|
||
|
|
walker(t)
|
||
|
|
}
|
||
|
|
|
||
|
|
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 {
|
||
|
|
v := <- ch2
|
||
|
|
if i != v {
|
||
|
|
return false
|
||
|
|
}
|
||
|
|
}
|
||
|
|
return true
|
||
|
|
}
|
||
|
|
|
||
|
|
func main() {
|
||
|
|
ch := make(chan int)
|
||
|
|
fmt.Println("START")
|
||
|
|
// Create two trees
|
||
|
|
t1 := tree.New(2)
|
||
|
|
t2 := tree.New(2)
|
||
|
|
t3 := tree.New(3)
|
||
|
|
go Walk(t1, ch)
|
||
|
|
for i := range ch {
|
||
|
|
fmt.Println(i)
|
||
|
|
}
|
||
|
|
|
||
|
|
same_1 := Same(t1, t2)
|
||
|
|
same_2 := Same(t2, t3)
|
||
|
|
fmt.Println(same_1, same_2)
|
||
|
|
}
|