Skip to content

Instantly share code, notes, and snippets.

@suzuken
Created January 29, 2015 05:47
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save suzuken/20bf05130d26cd9f1293 to your computer and use it in GitHub Desktop.
Save suzuken/20bf05130d26cd9f1293 to your computer and use it in GitHub Desktop.
package main
import (
"code.google.com/p/go-tour/tree"
"fmt"
)
func Walk(t *tree.Tree, ch chan int) {
WalkIter(t, ch)
close(ch)
}
func WalkIter(t *tree.Tree, ch chan int) {
if t == nil {
return
}
WalkIter(t.Left, ch)
ch <- t.Value
WalkIter(t.Right, ch)
}
// 二分木の比較
func Same(t1, t2 *tree.Tree) bool {
c1 := make(chan int, 10)
c2 := make(chan int, 10)
go Walk(t1, c1)
go Walk(t2, c2)
for i := range c1 {
if i != <-c2 {
return false
}
}
return true
}
func main() {
// fmt.Println(tree.New(1))
// ((((1 (2)) 3 (4)) 5 ((6) 7 ((8) 9))) 10)
// 空リストを表示してもpanicにはならない
// 長さ10のチャンネルを作成
ch := make(chan int, 10)
go Walk(tree.New(1), ch)
for i := range ch {
fmt.Println(i)
}
fmt.Println(Same(tree.New(1), tree.New(1)))
fmt.Println(Same(tree.New(1), tree.New(2)))
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment