SHARE
TWEET

Untitled

a guest Jan 18th, 2020 61 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. package main
  2.  
  3. import "golang.org/x/tour/tree"
  4. import "fmt"
  5.  
  6. // Walk walks the tree t sending all values
  7. // from the tree to the channel ch.
  8. func Walk(t *tree.Tree, ch chan int) {
  9.     if t == nil {
  10.         return
  11.     }
  12.     Walk(t.Left, ch)
  13.     ch <- t.Value
  14.     Walk(t.Right, ch)
  15. }
  16.  
  17. // Same determines whether the trees
  18. // t1 and t2 contain the same values.
  19. func Same(t1, t2 *tree.Tree) bool {
  20.     t1Channel := make(chan int)
  21.     go Walk(t1, t1Channel)
  22.    
  23.     t2Channel := make(chan int)
  24.     go Walk(t2, t2Channel)
  25.    
  26.     for i := 0; i < 10; i++ {
  27.         t1Value := <- t1Channel
  28.         t2Value := <- t2Channel
  29.        
  30.         if t1Value != t2Value {
  31.             return false
  32.         }
  33.     }
  34.    
  35.     return true
  36. }
  37.  
  38. func main() {
  39.     fmt.Println(Same(tree.New(1), tree.New(1)))
  40.     fmt.Println(Same(tree.New(1), tree.New(2)))
  41. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
Top