Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package main
- import (
- "golang.org/x/tour/tree"
- "fmt"
- )
- func Walk(name string, t *tree.Tree, ch chan int) {
- if t.Left != nil {
- Walk(name, t.Left, ch)
- }
- ch <- t.Value
- if t.Right != nil {
- Walk(name, t.Right, ch)
- }
- }
- var tree1 = tree.Tree{
- Left: &tree.Tree{
- Value: 1,
- Right: &tree.Tree{
- Value: 2,
- },
- },
- Value: 3,
- Right: &tree.Tree{
- Left: &tree.Tree{
- Value: 5,
- },
- Value: 8,
- },
- }
- var tree2 = tree.Tree{
- Left: &tree.Tree{
- Left: &tree.Tree{
- Value: 1,
- },
- Value: 2,
- },
- Value: 3,
- }
- func Same(t1, t2 *tree.Tree) bool {
- var ch1 = make(chan int, 1)
- go func() {
- Walk("t1", t1, ch1)
- fmt.Println("close 1")
- close(ch1)
- } ()
- var ch2 = make(chan int, 1)
- go func() {
- Walk("t2", t2, ch2)
- fmt.Println("close 2")
- close(ch2)
- } ()
- for i:= 0; i<5; i++ {
- v1, ok1 := <-ch1
- v2, ok2 := <-ch2
- if !ok1 || !ok2 || v1 != v2 {
- return false
- }
- }
- return true
- }
- func main() {
- result := Same(&tree1, &tree2)
- fmt.Println("result", result)
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement