Skip to content

Commit

Permalink
Solve the "Test if a binary tree is symmetric" problem
Browse files Browse the repository at this point in the history
  • Loading branch information
mrekucci committed Oct 21, 2015
1 parent 5c8a224 commit 54c8fe9
Show file tree
Hide file tree
Showing 3 changed files with 91 additions and 3 deletions.
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ Binary Trees
| Problem | Test | Implemented |
|--------------------------------------------------------------------------|:------------:|:-----------:|
| [Test if a binary tree is balanced][144] | [tests][145] ||
| [Test if a binary tree is symmetric][146] | [tests][147] | |
| [Test if a binary tree is symmetric][146] | [tests][147] | |
| [Compute the lowest common ancestor in a binary tree][148] | [tests][149] | |
| [Compute the LCA when nodes have parent pointers][150] | [tests][151] | |
| [Sum the root-to-leaf paths in a binary tree][152] | [tests][153] | |
Expand Down Expand Up @@ -542,8 +542,8 @@ Honors Class
[143]: in_progress.md
[144]: btrees/balanced.go
[145]: btrees/balanced_test.go
[146]: in_progress.md
[147]: in_progress.md
[146]: btrees/symmetric.go
[147]: btrees/symmetric_test.go
[148]: in_progress.md
[149]: in_progress.md
[150]: in_progress.md
Expand Down
27 changes: 27 additions & 0 deletions btrees/symmetric.go
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)
}
61 changes: 61 additions & 0 deletions btrees/symmetric_test.go
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)
}
}

0 comments on commit 54c8fe9

Please sign in to comment.