-
Notifications
You must be signed in to change notification settings - Fork 96
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Solve the "Test if a binary tree is symmetric" problem
- Loading branch information
Showing
3 changed files
with
91 additions
and
3 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,27 @@ | ||
// Copyright (c) 2015, Peter Mrekaj. All rights reserved. | ||
// Use of this source code is governed by a MIT-style | ||
// license that can be found in the LICENSE.txt file. | ||
|
||
package btrees | ||
|
||
// checkSymmetry returns true if the l binary | ||
// tree is symmetric to the r binary tree. | ||
func checkSymmetry(l, r *BTree) bool { | ||
switch { | ||
case l == nil && r == nil: | ||
return true | ||
case l != nil && r != nil: | ||
return l.Data == r.Data && | ||
checkSymmetry(l.left, r.right) && | ||
checkSymmetry(l.right, r.left) | ||
} | ||
return false | ||
} | ||
|
||
// IsSymmetric returns true if t is a symmetric binary tree. | ||
func IsSymmetric(t *BTree) bool { | ||
if t == nil { | ||
return true | ||
} | ||
return checkSymmetry(t.left, t.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,61 @@ | ||
// Copyright (c) 2015, Peter Mrekaj. All rights reserved. | ||
// Use of this source code is governed by a MIT-style | ||
// license that can be found in the LICENSE.txt file. | ||
|
||
package btrees | ||
|
||
import "testing" | ||
|
||
func TestIsSymmetric(t *testing.T) { | ||
for _, test := range []struct { | ||
tree *BTree | ||
want bool | ||
}{ | ||
// Symmetric binary trees. | ||
{(*BTree)(nil), true}, | ||
{&BTree{"TREE_0", nil, nil}, true}, | ||
{&BTree{"TREE_1", | ||
&BTree{"A", | ||
&BTree{"B", | ||
nil, | ||
&BTree{"C", nil, nil}}, | ||
nil}, | ||
&BTree{"A", | ||
nil, | ||
&BTree{"B", | ||
&BTree{"C", nil, nil}, | ||
nil}}}, true}, | ||
|
||
// Non-symmetric binary trees. | ||
{&BTree{"TREE_2", &BTree{"A", nil, nil}, nil}, false}, | ||
{&BTree{"TREE_3", nil, &BTree{"A", nil, nil}}, false}, | ||
{&BTree{"TREE_4", &BTree{"A", nil, nil}, &BTree{"B", nil, nil}}, false}, | ||
{&BTree{"TREE_5", | ||
&BTree{"A", | ||
&BTree{"B", nil, nil}, | ||
nil}, | ||
&BTree{"C", nil, nil}}, false}, | ||
} { | ||
if got := IsSymmetric(test.tree); got != test.want { | ||
t.Errorf("IsSymmetric(%v) = %t; want %t", test.tree, got, test.want) | ||
} | ||
} | ||
} | ||
|
||
func BenchmarkIsSymmetric(b *testing.B) { | ||
tree := &BTree{"ROOT", | ||
&BTree{"A", | ||
&BTree{"B", | ||
nil, | ||
&BTree{"C", nil, nil}}, | ||
nil}, | ||
&BTree{"A", | ||
nil, | ||
&BTree{"B", | ||
&BTree{"C", nil, nil}, | ||
nil}}} | ||
b.ResetTimer() | ||
for i := 0; i < b.N; i++ { | ||
IsSymmetric(tree) | ||
} | ||
} |