-
Notifications
You must be signed in to change notification settings - Fork 32
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
85 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 | ||
} |