-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
1382778
commit dc7b781
Showing
3 changed files
with
51 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
module main | ||
|
||
go 1.23.0 | ||
|
||
require golang.org/x/tour v0.1.0 // indirect |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
golang.org/x/tour v0.1.0 h1:OWzbINRoGf1wwBhKdFDpYwM88NM0d1SL/Nj6PagS6YE= | ||
golang.org/x/tour v0.1.0/go.mod h1:DUZC6G8mR1AXgXy73r8qt/G5RsefKIlSj6jBMc8b9Wc= |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
package main | ||
|
||
import "golang.org/x/tour/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 == nil { | ||
return | ||
} | ||
|
||
Walk(t.Left, ch) | ||
ch <- t.Value | ||
Walk(t.Right, ch) | ||
} | ||
|
||
// Same determines whether the trees | ||
// t1 and t2 contain the same values. | ||
func Same(t1, t2 *tree.Tree) bool { | ||
ch1 := make(chan int) | ||
ch2 := make(chan int) | ||
go Walk(t1, ch1) | ||
go Walk(t2, ch2) | ||
for { | ||
v1, ok1 := <-ch1 | ||
v2, ok2 := <-ch2 | ||
if v1 != v2 || ok1 != ok2 { | ||
return false | ||
} | ||
if !ok1 { | ||
break | ||
} | ||
} | ||
return true | ||
} | ||
|
||
func main() { | ||
ch := make(chan int) | ||
go Walk(tree.New(3), ch) | ||
|
||
for v := range ch { | ||
println(v) | ||
} | ||
} |