Initial commit
parent
8551b633f5
commit
b0f2d371e3
@ -0,0 +1,63 @@
|
|||||||
|
package main
|
||||||
|
|
||||||
|
import "fmt"
|
||||||
|
import "golang.org/x/tour/tree"
|
||||||
|
|
||||||
|
func recWalk(t *tree.Tree, ch chan int) {
|
||||||
|
if t.Left != nil {
|
||||||
|
recWalk(t.Left,ch)
|
||||||
|
}
|
||||||
|
ch <- t.Value
|
||||||
|
if t.Right != nil {
|
||||||
|
recWalk(t.Right,ch)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Walk walks the tree t sending all values
|
||||||
|
// from the tree to the channel ch.
|
||||||
|
func Walk(t *tree.Tree, ch chan int) {
|
||||||
|
recWalk(t,ch)
|
||||||
|
close(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 !ok1 && !ok2 {
|
||||||
|
break
|
||||||
|
}
|
||||||
|
|
||||||
|
if ok1 != ok2 {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
|
||||||
|
if v1 != v2 {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
ch := make(chan int)
|
||||||
|
|
||||||
|
go Walk(tree.New(1), ch)
|
||||||
|
|
||||||
|
for i := range ch {
|
||||||
|
fmt.Println(i)
|
||||||
|
}
|
||||||
|
|
||||||
|
fmt.Println(Same(tree.New(1), tree.New(1)))
|
||||||
|
fmt.Println(Same(tree.New(1), tree.New(2)))
|
||||||
|
}
|
||||||
@ -0,0 +1,5 @@
|
|||||||
|
module gitea.paas.celticinfo.fr/oabrivard/tour-of-go/exercise-equivalent-binary-tree
|
||||||
|
|
||||||
|
go 1.21.0
|
||||||
|
|
||||||
|
require golang.org/x/tour v0.1.0
|
||||||
@ -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=
|
||||||
@ -0,0 +1,3 @@
|
|||||||
|
module gitea.paas.celticinfo.fr/oabrivard/tour-of-go/helloworld
|
||||||
|
|
||||||
|
go 1.20
|
||||||
@ -0,0 +1,8 @@
|
|||||||
|
package main
|
||||||
|
|
||||||
|
import "fmt"
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
fmt.Println("Hello, world.")
|
||||||
|
}
|
||||||
|
|
||||||
Loading…
Reference in New Issue