-
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.
Merge pull request #244 from vaskoz/day115
Day115
- Loading branch information
Showing
3 changed files
with
72 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,24 @@ | ||
package day115 | ||
|
||
// IntBinaryTree is a binary tree of integers. | ||
type IntBinaryTree struct { | ||
Value int | ||
Left, Right *IntBinaryTree | ||
} | ||
|
||
// SameTreeSubstructure returns true if 't' exists somewhere inside of 's'. | ||
func SameTreeSubstructure(t, s *IntBinaryTree) bool { | ||
if s == nil { | ||
return false | ||
} | ||
return equalTrees(t, s) || SameTreeSubstructure(t, s.Left) || SameTreeSubstructure(t, s.Right) | ||
} | ||
|
||
func equalTrees(one, two *IntBinaryTree) bool { | ||
if one == nil && two == nil { | ||
return true | ||
} else if (one == nil && two != nil) || (one != nil && two == nil) || (one.Value != two.Value) { | ||
return false | ||
} | ||
return equalTrees(one.Left, two.Left) || equalTrees(one.Right, two.Right) | ||
} |
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,47 @@ | ||
package day115 | ||
|
||
import "testing" | ||
|
||
var testcases = []struct { | ||
t, s *IntBinaryTree | ||
expected bool | ||
}{ | ||
{&IntBinaryTree{ | ||
10, | ||
&IntBinaryTree{15, nil, nil}, | ||
&IntBinaryTree{20, nil, nil}}, | ||
&IntBinaryTree{15, | ||
&IntBinaryTree{10, | ||
&IntBinaryTree{15, nil, nil}, | ||
&IntBinaryTree{20, nil, nil}}, | ||
&IntBinaryTree{20, nil, nil}}, | ||
true}, | ||
{&IntBinaryTree{ | ||
10, nil, nil}, | ||
&IntBinaryTree{15, | ||
&IntBinaryTree{25, | ||
&IntBinaryTree{15, nil, nil}, | ||
&IntBinaryTree{20, nil, nil}}, | ||
&IntBinaryTree{20, nil, nil}}, | ||
false}, | ||
{&IntBinaryTree{10, nil, nil}, | ||
&IntBinaryTree{15, nil, nil}, | ||
false}, | ||
} | ||
|
||
func TestSameTreeSubstructure(t *testing.T) { | ||
t.Parallel() | ||
for tcid, tc := range testcases { | ||
if result := SameTreeSubstructure(tc.t, tc.s); result != tc.expected { | ||
t.Errorf("TCID%d expected %v got %v", tcid, tc.expected, result) | ||
} | ||
} | ||
} | ||
|
||
func BenchmarkSameTreeSubstructure(b *testing.B) { | ||
for i := 0; i < b.N; i++ { | ||
for _, tc := range testcases { | ||
SameTreeSubstructure(tc.t, tc.s) | ||
} | ||
} | ||
} |