Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package main
- import (
- "fmt"
- "golang.org/x/tour/tree"
- )
- type Tree struct {
- Left *Tree
- Value int
- Right *Tree
- }
- // Walk walks the tree t sending all values
- // from the tree to the channel ch.
- 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)
- }
- }
- // Same determines whether the trees
- // t1 and t2 contain the same values.
- func Same(t1, t2 *tree.Tree) bool {
- ch1, ch2 := make(chan int, 10), make(chan int, 10)
- Walk(t1, ch1)
- Walk(t2, ch2)
- for i := 0; i < 10; i++ {
- x, y := <-ch1, <-ch2
- if x != y {
- return false
- }
- }
- return true
- }
- func main() {
- ch := make(chan int, 10)
- go Walk(tree.New(1), ch)
- for i := 0; i < 10; i++ {
- fmt.Println(<-ch)
- }
- fmt.Println(Same(tree.New(1), tree.New(2)))
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement