Guest User

Untitled

a guest
Jan 18th, 2019
80
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.29 KB | None | 0 0
  1. package main
  2.  
  3. import "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. var temp chan int
  10. ch <- t.Value
  11. if t.Left!=nil{go Walk(t.Left,temp)}
  12. if t.Right!=nil{go Walk(t.Right,temp)}
  13. for i := range temp{
  14. ch <- i
  15. }
  16. close(ch)
  17. }
  18.  
  19. // Same determines whether the trees
  20. // t1 and t2 contain the same values.
  21. func Same(t1, t2 *tree.Tree) bool
  22.  
  23. var temp chan int = make(chan int)
  24. var ch chan int = make(chan int)
  25.  
  26. package main
  27.  
  28. import "tour/tree"
  29. import "fmt"
  30.  
  31. // Walk walks the tree t sending all values
  32. // from the tree to the channel ch.
  33. func Walk(t *tree.Tree, ch chan int) {
  34. var temp1 chan int = make(chan int)
  35. var temp2 chan int = make(chan int)
  36. ch <- t.Value
  37. if t.Left != nil {
  38. go Walk(t.Left, temp1)
  39. }
  40. if t.Right != nil {
  41. go Walk(t.Right, temp2)
  42. }
  43. if t.Left != nil {
  44. for i := range temp1 {
  45. ch <- i
  46. }
  47. }
  48. if t.Right != nil {
  49. for i := range temp2 {
  50. ch <- i
  51. }
  52. }
  53. close(ch)
  54. }
  55.  
  56. // Same determines whether the trees
  57. // t1 and t2 contain the same values.
  58. func Same(t1, t2 *tree.Tree) bool
  59.  
  60. func main() {
  61. var ch chan int = make(chan int)
  62. go Walk(tree.New(1), ch)
  63. for i := range ch {
  64. fmt.Println(i)
  65. }
  66. }
  67.  
  68. package main
  69.  
  70. import (
  71. "fmt"
  72. "tour/tree"
  73. )
  74.  
  75. // Walk walks the tree t sending all values
  76. // from the tree to the channel ch.
  77. func Walk(t *tree.Tree, c chan int, d bool) {
  78. if t.Left != nil {
  79. Walk(t.Left, c, false)
  80. }
  81. c <- t.Value
  82.  
  83. if t.Right != nil {
  84. Walk(t.Right, c, false)
  85. }
  86. if d {
  87. close(c)
  88. }
  89. }
  90.  
  91. // Same determines whether the trees
  92. // t1 and t2 contain the same values.
  93. func Same(t1, t2 *tree.Tree) bool {
  94. ch1 := make(chan int)
  95. ch2 := make(chan int)
  96. go Walk(t1, ch1, true)
  97. go Walk(t2, ch2, true)
  98. for {
  99. v1, ok1 := <-ch1
  100. v2, ok2 := <-ch2
  101.  
  102. if v1 != v2 {
  103. return false
  104. }
  105. if ok1 != ok2 {
  106. return false
  107. }
  108. if !ok1 && !ok2 {
  109. return true
  110. }
  111. }
  112. return false
  113. }
  114. func main() {
  115. ch := make(chan int)
  116. go Walk(tree.New(1), ch, true)
  117.  
  118. for i := range ch {
  119. fmt.Println(i)
  120. }
  121.  
  122. test1 := Same(tree.New(1), tree.New(1))
  123. test2 := Same(tree.New(1), tree.New(2))
  124.  
  125. fmt.Println(test1, test2)
  126. }
Add Comment
Please, Sign In to add comment