From a02717c6f94b540b7d8858f432430794fd67feaa Mon Sep 17 00:00:00 2001 From: Vasko Zdravevski Date: Sun, 20 Oct 2019 19:29:05 -0600 Subject: [PATCH 1/2] day 326: construct Cartesian tree from inorder traversal minheap --- day326/problem.go | 30 +++++++++++++++++++++++ day326/problem_test.go | 54 ++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 84 insertions(+) create mode 100644 day326/problem.go create mode 100644 day326/problem_test.go diff --git a/day326/problem.go b/day326/problem.go new file mode 100644 index 0000000..2d69004 --- /dev/null +++ b/day326/problem.go @@ -0,0 +1,30 @@ +package day326 + +// IntBinaryTree is a typical binary tree +// that stores integers for a value. +type IntBinaryTree struct { + Value int + Left, Right *IntBinaryTree +} + +// CartesianTree returns the corresponding Cartesian tree +// based on the given sequence. +func CartesianTree(s []int) *IntBinaryTree { + if len(s) == 0 { + return nil + } + + minIndex := 0 + + for i := range s { + if s[i] < s[minIndex] { + minIndex = i + } + } + + root := &IntBinaryTree{s[minIndex], nil, nil} + root.Left = CartesianTree(s[:minIndex]) + root.Right = CartesianTree(s[minIndex+1:]) + + return root +} diff --git a/day326/problem_test.go b/day326/problem_test.go new file mode 100644 index 0000000..8ee200c --- /dev/null +++ b/day326/problem_test.go @@ -0,0 +1,54 @@ +package day326 + +import ( + "testing" +) + +// nolint +var testcases = []struct { + seq []int + expected *IntBinaryTree +}{ + { + []int{3, 2, 6, 1, 9}, + &IntBinaryTree{ + 1, + &IntBinaryTree{ + 2, + &IntBinaryTree{3, nil, nil}, + &IntBinaryTree{6, nil, nil}, + }, + &IntBinaryTree{9, nil, nil}, + }, + }, +} + +func TestCartesianTree(t *testing.T) { + t.Parallel() + + for tcid, tc := range testcases { + if result := CartesianTree(tc.seq); !equalTree(result, tc.expected) { + t.Errorf("Trees do not match for TCID%d", tcid) + } + } +} + +func BenchmarkCartesianTree(b *testing.B) { + for i := 0; i < b.N; i++ { + for _, tc := range testcases { + CartesianTree(tc.seq) + } + } +} + +func equalTree(a, b *IntBinaryTree) bool { + if a == nil && b == nil { + return true + } else if a != nil && b != nil { + return a.Value == b.Value && + equalTree(a.Left, b.Left) && + equalTree(a.Right, b.Right) + } + + return false +} From f0dd0909df9f81356ef61345ac189efdbe52d66d Mon Sep 17 00:00:00 2001 From: Vasko Zdravevski Date: Sun, 20 Oct 2019 19:29:55 -0600 Subject: [PATCH 2/2] add day 326 to readme --- README.md | 1 + 1 file changed, 1 insertion(+) diff --git a/README.md b/README.md index dc77d6b..f87cbd5 100644 --- a/README.md +++ b/README.md @@ -330,6 +330,7 @@ problems from * [Day 323](https://github.com/vaskoz/dailycodingproblem-go/issues/656) * [Day 324](https://github.com/vaskoz/dailycodingproblem-go/issues/658) * [Day 325](https://github.com/vaskoz/dailycodingproblem-go/issues/660) +* [Day 326](https://github.com/vaskoz/dailycodingproblem-go/issues/661) * [Day 333](https://github.com/vaskoz/dailycodingproblem-go/issues/668) * [Day 337](https://github.com/vaskoz/dailycodingproblem-go/issues/674) * [Day 338](https://github.com/vaskoz/dailycodingproblem-go/issues/675)