From e99571482691b80047606fdba1b68a4fc67ec95d Mon Sep 17 00:00:00 2001 From: Pavel Date: Sun, 20 Feb 2022 02:23:36 +0300 Subject: [PATCH] Initial for treeiter --- go.mod | 2 +- treeiter/README.md | 16 +++++++ treeiter/treeiter.go | 4 ++ treeiter/treeiter_example_test.go | 24 ++++++++++ treeiter/treeiter_test.go | 77 +++++++++++++++++++++++++++++++ 5 files changed, 122 insertions(+), 1 deletion(-) create mode 100644 treeiter/README.md create mode 100644 treeiter/treeiter.go create mode 100644 treeiter/treeiter_example_test.go create mode 100644 treeiter/treeiter_test.go diff --git a/go.mod b/go.mod index aa5c5f7..15928df 100644 --- a/go.mod +++ b/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 diff --git a/treeiter/README.md b/treeiter/README.md new file mode 100644 index 0000000..0ef2213 --- /dev/null +++ b/treeiter/README.md @@ -0,0 +1,16 @@ +# treeiter + +В этой задаче нужно написать итератор по бинарному дереву. +Это дерево удовлетворяет следующему интерфейсу и само является типом `T` в нем: + +```go +type Node[T any] interface { + Left() *T + Right() *T +} +``` + +Итератор должен принимать на вход дерево и +функцию с одним аргументом типа дерева, не возвращающую ничего. + +Пример использования итератора можно посмотреть в `treeiter_example_test.go` diff --git a/treeiter/treeiter.go b/treeiter/treeiter.go new file mode 100644 index 0000000..0cf96f2 --- /dev/null +++ b/treeiter/treeiter.go @@ -0,0 +1,4 @@ +//go:build !solution +// +build !solution + +package treeiter diff --git a/treeiter/treeiter_example_test.go b/treeiter/treeiter_example_test.go new file mode 100644 index 0000000..95724fc --- /dev/null +++ b/treeiter/treeiter_example_test.go @@ -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 +} diff --git a/treeiter/treeiter_test.go b/treeiter/treeiter_test.go new file mode 100644 index 0000000..d1cba09 --- /dev/null +++ b/treeiter/treeiter_test.go @@ -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) +}