Guest User

Untitled

a guest
Jun 23rd, 2018
88
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.91 KB | None | 0 0
  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. ch <- t.Value
  13. Walk(t.Left, ch)
  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. ch1 := make(chan int)
  21. go func() {
  22. Walk(t1, ch1)
  23. close(ch1)
  24. }()
  25.  
  26. ch2 := make(chan int)
  27. go func() {
  28. Walk(t2, ch2)
  29. close(ch2)
  30. }()
  31.  
  32.  
  33. var arr1 []int
  34. for elem := range ch1 {
  35. arr1 = append(arr1, elem)
  36. }
  37. var arr2 []int
  38. for elem := range ch2 {
  39. arr2 = append(arr2, elem)
  40. }
  41.  
  42. for i, _ := range arr1 {
  43. if arr1[i] != arr2[i] {
  44. return false
  45. }
  46. }
  47.  
  48. return true
  49. }
  50.  
  51. func main() {
  52. ch1 := make(chan int)
  53.  
  54. go func() {
  55. Walk(tree.New(1), ch1)
  56. close(ch1)
  57. }()
  58.  
  59. a := tree.New(1)
  60. fmt.Println("Done %s", Same(a,tree.New(1)))
  61. }
Add Comment
Please, Sign In to add comment