Skip to content

Instantly share code, notes, and snippets.

@groovili
Created July 3, 2018 11:05
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 groovili/be70416ce9b3805eda548bf7ee6a4b88 to your computer and use it in GitHub Desktop.
Save groovili/be70416ce9b3805eda548bf7ee6a4b88 to your computer and use it in GitHub Desktop.
package main
import (
"golang.org/x/tour/tree"
"fmt"
)
func _walk(t *tree.Tree, ch chan int) {
if t == nil {
return
}
ch <- t.Value
if t.Left != nil{
_walk(t.Left, ch)
}
if t.Right != nil{
_walk(t.Right, ch)
}
return
}
func Walk(t *tree.Tree, ch chan int){
_walk(t, ch)
close(ch)
}
func Same(t1, t2 *tree.Tree) bool {
ch1 := make(chan int)
ch2 := make(chan int)
go Walk(t1, ch1)
go Walk(t2, ch2)
for {
v1, ok1 := <-ch1
v2, ok2 := <-ch2
if v1 != v2 || ok1 != ok2 {
return false
}
if !ok1 || !ok2 {
break
}
}
return true
}
func main() {
ch := make(chan int)
go Walk(tree.New(1), ch)
for i:= range ch {
fmt.Println(i)
}
treeOne := tree.New(1)
treeTwo := tree.New(2)
fmt.Printf("Theese are the same trees - %t \n", Same(treeOne, treeOne))
fmt.Printf("Theese are the same trees - %t \n", Same(treeOne, treeTwo))
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment