Created
November 21, 2015 19:45
-
-
Save matipan/8c9a89e27cbef6b3e16b to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"fmt" | |
"golang.org/x/tour/tree" | |
) | |
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 GoWalk(t *tree.Tree) chan int { | |
ch := make(chan int) | |
go func() { | |
Walk(t, ch) | |
close(ch) | |
}() | |
return ch | |
} | |
func Same(t1, t2 *tree.Tree) bool { | |
c1, c2 := GoWalk(t1), GoWalk(t2) | |
for { | |
i, done_1 := <-c1 | |
n, done_2 := <-c2 | |
if !done_1 || !done_2 { | |
// if we are done with both channels then they have the same values | |
return done_1 == done_2 | |
} | |
if i != n { | |
break | |
} | |
} | |
return false | |
} | |
func main() { | |
fmt.Println(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