Created
July 3, 2018 11:05
-
-
Save groovili/be70416ce9b3805eda548bf7ee6a4b88 to your computer and use it in GitHub Desktop.
This file contains 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 ( | |
"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