Skip to content

Instantly share code, notes, and snippets.

@kkabdol
Created April 21, 2016 05:48
Show Gist options
  • Select an option

  • Save kkabdol/16052db3e5eeabfe8d8bcbc17168b2bb to your computer and use it in GitHub Desktop.

Select an option

Save kkabdol/16052db3e5eeabfe8d8bcbc17168b2bb to your computer and use it in GitHub Desktop.
go language exercise
package main
import (
"golang.org/x/tour/tree"
"fmt"
)
// Walk walks the tree t sending all values
// from the tree to the channel ch.
func Walker(t *tree.Tree, ch chan int) {
if t != nil {
Walker(t.Left, ch)
ch <- t.Value
Walker(t.Right, ch)
}
}
func Walk(t *tree.Tree, ch chan int) {
Walker(t, ch)
close(ch)
}
// Same determines whether the trees
// t1 and t2 contain the same values.
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 {
j,ok:=<-ch2
if(i!=j||!ok) {return false}
}
return false
}
func main() {
ch := make(chan int)
go Walk(tree.New(1), ch)
for i := range ch {
fmt.Println(i)
}
Same(tree.New(1), tree.New(1))
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment