Skip to content

Instantly share code, notes, and snippets.

@oldergod
Last active August 29, 2015 14:06
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 oldergod/a5e0efb3d8a5c37e4deb to your computer and use it in GitHub Desktop.
Save oldergod/a5e0efb3d8a5c37e4deb to your computer and use it in GitHub Desktop.
Tour of Go Binary Trees
package main
import (
"code.google.com/p/go-tour/tree"
"fmt"
)
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)
}
}
func Same(t1, t2 *tree.Tree) bool {
ch1 := make(chan int)
ch2 := make(chan int)
go Walk(t1, ch1)
go Walk(t2, ch2)
var v1, v2 int
for i := 0; i < 10; i++ {
v1 = <-ch1
v2 = <-ch2
if v1 != v2 {
return false
}
}
return true
}
func main() {
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