-
Notifications
You must be signed in to change notification settings - Fork 97
/
symmetric_test.go
61 lines (56 loc) · 1.31 KB
/
symmetric_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
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)
}
}