Skip to content

Instantly share code, notes, and snippets.

@justinabrahms
Created February 17, 2011 14:53
Show Gist options
  • Save justinabrahms/831856 to your computer and use it in GitHub Desktop.
Save justinabrahms/831856 to your computer and use it in GitHub Desktop.
Simple tree walking using goroutines.
package main
import (
"tour/tree"
"fmt"
)
func Walk(tree *tree.Tree, ch chan int) {
if tree != nil {
Walk(tree.Left, ch)
ch <- tree.Value
Walk(tree.Right, ch)
}
}
func Same(t1, t2 *tree.Tree) bool {
ch1, ch2 := make(chan int), make(chan int)
go Walk(t1, ch1)
go Walk(t2, ch2)
for i:=0; i<10; i++ {
v1 := <-ch1
v2 := <-ch2
if v1 != v2 {
return false
}
}
return true
}
func PrintTree() {
ch := make(chan int)
go Walk(tree.New(1), ch)
for i := 0; i < 10; i++ {
foo := <-ch
fmt.Println(foo)
}
}
func main() {
PrintTree()
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