Skip to content

Instantly share code, notes, and snippets.

@FlandreDaisuki
Created August 25, 2014 03:53
Show Gist options
  • Save FlandreDaisuki/b30c78c62b1a9147b2d4 to your computer and use it in GitHub Desktop.
Save FlandreDaisuki/b30c78c62b1a9147b2d4 to your computer and use it in GitHub Desktop.
package main
import (
"code.google.com/p/go-tour/tree"
"fmt"
)
// Walk walks the tree t sending all values
// from the tree to the channel ch.
func Walk(t *tree.Tree, ch chan int) {
if t.Left != nil {
Walk(t.Left, ch)
}
ch <- t.Value
if t.Right != nil {
Walk(t.Right, ch)
}
}
// Same determines whether the trees
// t1 and t2 contain the same values.
func Same(t1, t2 *tree.Tree) bool {
c1 := make(chan int)
c2 := make(chan int)
for i := 0; i < 10; i++ {
if <-c1 != <-c2 {
return false
}
}
return true
}
func main() {
tr1 := tree.New(1)
tr2 := tree.New(1)
if Same(tr1, tr2) {
fmt.Println("Same!!")
} else {
fmt.Println("Different!!")
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment