Initial for treeiter
This commit is contained in:
parent
966462cc08
commit
e995714826
5 changed files with 122 additions and 1 deletions
2
go.mod
2
go.mod
|
@ -1,6 +1,6 @@
|
|||
module gitlab.com/slon/shad-go
|
||||
|
||||
go 1.17
|
||||
go 1.18
|
||||
|
||||
require (
|
||||
github.com/ClickHouse/clickhouse-go v1.4.0
|
||||
|
|
16
treeiter/README.md
Normal file
16
treeiter/README.md
Normal file
|
@ -0,0 +1,16 @@
|
|||
# treeiter
|
||||
|
||||
В этой задаче нужно написать итератор по бинарному дереву.
|
||||
Это дерево удовлетворяет следующему интерфейсу и само является типом `T` в нем:
|
||||
|
||||
```go
|
||||
type Node[T any] interface {
|
||||
Left() *T
|
||||
Right() *T
|
||||
}
|
||||
```
|
||||
|
||||
Итератор должен принимать на вход дерево и
|
||||
функцию с одним аргументом типа дерева, не возвращающую ничего.
|
||||
|
||||
Пример использования итератора можно посмотреть в `treeiter_example_test.go`
|
4
treeiter/treeiter.go
Normal file
4
treeiter/treeiter.go
Normal file
|
@ -0,0 +1,4 @@
|
|||
//go:build !solution
|
||||
// +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
|
||||
}
|
77
treeiter/treeiter_test.go
Normal file
77
treeiter/treeiter_test.go
Normal file
|
@ -0,0 +1,77 @@
|
|||
package treeiter
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
"github.com/stretchr/testify/assert"
|
||||
)
|
||||
|
||||
type Node[T any] interface {
|
||||
Left() *T
|
||||
Right() *T
|
||||
}
|
||||
|
||||
type ValuesNode[T any] struct {
|
||||
value T
|
||||
left, right *ValuesNode[T]
|
||||
}
|
||||
|
||||
var _ Node[ValuesNode[any]] = (*ValuesNode[any])(nil)
|
||||
|
||||
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