Merge branch 'treeiter' into 'master'
Add treeiter task See merge request slon/shad-go-private!54
This commit is contained in:
commit
9d1f10bc8d
4 changed files with 106 additions and 0 deletions
9
treeiter/README.md
Normal file
9
treeiter/README.md
Normal file
|
@ -0,0 +1,9 @@
|
|||
# treeiter
|
||||
|
||||
В этой задаче нужно написать итератор по бинарному дереву.
|
||||
|
||||
Итератор должен принимать на вход дерево и
|
||||
функцию с одним аргументом типа дерева, не возвращающую ничего.
|
||||
Сигнатуру необходимо придумать самостоятельно так, чтобы прошли тесты.
|
||||
|
||||
Пример использования итератора можно посмотреть в `treeiter_example_test.go`
|
3
treeiter/treeiter.go
Normal file
3
treeiter/treeiter.go
Normal file
|
@ -0,0 +1,3 @@
|
|||
//go:build !solution
|
||||
|
||||
package treeiter
|
24
treeiter/treeiter_example_test.go
Normal file
24
treeiter/treeiter_example_test.go
Normal file
|
@ -0,0 +1,24 @@
|
|||
package treeiter
|
||||
|
||||
import "fmt"
|
||||
|
||||
func ExampleDoInOrder() {
|
||||
tree := &ValuesNode[string]{
|
||||
value: "root",
|
||||
left: &ValuesNode[string]{
|
||||
value: "left",
|
||||
},
|
||||
right: &ValuesNode[string]{
|
||||
value: "right",
|
||||
},
|
||||
}
|
||||
|
||||
DoInOrder(tree, func(t *ValuesNode[string]) {
|
||||
fmt.Println(t.value)
|
||||
})
|
||||
|
||||
// Output:
|
||||
// left
|
||||
// root
|
||||
// right
|
||||
}
|
70
treeiter/treeiter_test.go
Normal file
70
treeiter/treeiter_test.go
Normal file
|
@ -0,0 +1,70 @@
|
|||
package treeiter
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
"github.com/stretchr/testify/assert"
|
||||
)
|
||||
|
||||
type ValuesNode[T any] struct {
|
||||
value T
|
||||
left, right *ValuesNode[T]
|
||||
}
|
||||
|
||||
func (t ValuesNode[T]) Left() *ValuesNode[T] {
|
||||
return t.left
|
||||
}
|
||||
|
||||
func (t ValuesNode[T]) Right() *ValuesNode[T] {
|
||||
return t.right
|
||||
}
|
||||
|
||||
type Collector[T any] struct {
|
||||
nodes []*ValuesNode[T]
|
||||
}
|
||||
|
||||
func (c *Collector[T]) Collect(tree *ValuesNode[T]) {
|
||||
c.nodes = append(c.nodes, tree)
|
||||
}
|
||||
|
||||
func TestInOrderNil(t *testing.T) {
|
||||
var collector Collector[any]
|
||||
DoInOrder(nil, collector.Collect)
|
||||
}
|
||||
|
||||
func TestInOrderIntTree(t *testing.T) {
|
||||
root := &ValuesNode[int]{value: 1}
|
||||
collector := Collector[int]{}
|
||||
|
||||
DoInOrder(root, collector.Collect)
|
||||
|
||||
assert.Equal(t, []*ValuesNode[int]{{value: 1}}, collector.nodes)
|
||||
}
|
||||
|
||||
func TestInOrderStringTree(t *testing.T) {
|
||||
rightLeftRight := &ValuesNode[string]{
|
||||
value: "right left right",
|
||||
}
|
||||
rightLeft := &ValuesNode[string]{
|
||||
value: "right left",
|
||||
right: rightLeftRight,
|
||||
}
|
||||
right := &ValuesNode[string]{
|
||||
value: "right",
|
||||
left: rightLeft,
|
||||
}
|
||||
left := &ValuesNode[string]{
|
||||
value: "left",
|
||||
}
|
||||
|
||||
root := &ValuesNode[string]{
|
||||
value: "root",
|
||||
left: left,
|
||||
right: right,
|
||||
}
|
||||
var collector Collector[string]
|
||||
|
||||
DoInOrder(root, collector.Collect)
|
||||
|
||||
assert.Equal(t, []*ValuesNode[string]{left, root, rightLeft, rightLeftRight, right}, collector.nodes)
|
||||
}
|
Loading…
Reference in a new issue