/
BinaryTrees.go
59 lines (47 loc) · 1.02 KB
/
BinaryTrees.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
package main
import (
"reflect"
"fmt"
"golang.org/x/tour/tree"
)
func Walk(t *tree.Tree, ch chan int) {
if t != nil {
go Walk(t.Left, ch)
ch <- t.Value
go Walk(t.Right, ch)
}
}
func Same(t1, t2 *tree.Tree) bool {
channel_one := make(chan int)
channel_two := make(chan int)
go Walk(t1, channel_one)
go Walk(t2, channel_two)
m1 := make(map[int]bool)
m2 := make(map[int]bool)
for i := 0; i < 10; i++ {
m1[<-channel_one] = true;
m2[<-channel_two] = true;
}
if reflect.DeepEqual(m1, m2) {
return true
} else {
return false
}
}
// Same determines whether the trees
// t1 and t2 contain the same values.
//func Same(t1, t2 *tree.Tree) bool
//Learning material used from
//https://tour.golang.org/concurrency/7
//https://tour.golang.org/concurrency/8
//This won't work if not run on the golang site.
func main() {
ch := make(chan int)
go Walk(tree.New(1), ch)
for i := 0; i < 10; i++ {
v := <-ch
fmt.Println(v)
}
fmt.Println(Same(tree.New(1), tree.New(1)))
fmt.Println(Same(tree.New(1), tree.New(2)))
}