-
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
60 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,17 @@ | ||
package day83 | ||
|
||
// BinaryTree is a binary tree with string Values. | ||
type BinaryTree struct { | ||
Value string | ||
Left, Right *BinaryTree | ||
} | ||
|
||
// InvertBinaryTree works in-place on the given tree. | ||
func InvertBinaryTree(tree *BinaryTree) { | ||
if tree == nil { | ||
return | ||
} | ||
InvertBinaryTree(tree.Left) | ||
InvertBinaryTree(tree.Right) | ||
tree.Left, tree.Right = tree.Right, tree.Left | ||
} |
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,42 @@ | ||
package day83 | ||
|
||
import "testing" | ||
|
||
func TestInvertBinaryTree(t *testing.T) { | ||
t.Parallel() | ||
input := &BinaryTree{"a", | ||
&BinaryTree{"b", &BinaryTree{"d", nil, nil}, &BinaryTree{"e", nil, nil}}, | ||
&BinaryTree{"c", &BinaryTree{"f", nil, nil}, nil}} | ||
original := &BinaryTree{"a", | ||
&BinaryTree{"b", &BinaryTree{"d", nil, nil}, &BinaryTree{"e", nil, nil}}, | ||
&BinaryTree{"c", &BinaryTree{"f", nil, nil}, nil}} | ||
inverted := &BinaryTree{"a", | ||
&BinaryTree{"c", nil, &BinaryTree{"f", nil, nil}}, | ||
&BinaryTree{"b", &BinaryTree{"e", nil, nil}, &BinaryTree{"d", nil, nil}}} | ||
InvertBinaryTree(input) | ||
if !equalTrees(input, inverted) { | ||
t.Errorf("Expected it to equal the inverted tree") | ||
} | ||
InvertBinaryTree(input) | ||
if !equalTrees(input, original) { | ||
t.Errorf("Invert an invert should match the original") | ||
} | ||
} | ||
|
||
func BenchmarkInvertBinaryTree(b *testing.B) { | ||
input := &BinaryTree{"a", | ||
&BinaryTree{"b", &BinaryTree{"d", nil, nil}, &BinaryTree{"e", nil, nil}}, | ||
&BinaryTree{"c", &BinaryTree{"f", nil, nil}, nil}} | ||
for i := 0; i < b.N; i++ { | ||
InvertBinaryTree(input) | ||
} | ||
} | ||
|
||
func equalTrees(one, two *BinaryTree) 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) | ||
} |