-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbinary_tree_test.go
116 lines (108 loc) · 2.1 KB
/
binary_tree_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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
package myplayground
import (
"reflect"
"testing"
)
type testCase struct {
tree *Node
dfs []interface{}
bfs []interface{}
size int
height int
}
var testCases = map[string]testCase{
"nil tree": testCase{
tree: nil,
dfs: nil,
bfs: nil,
size: 0,
height: 0,
},
"only empty tree": testCase{
tree: &Node{},
dfs: []interface{}{nil},
bfs: []interface{}{nil},
size: 1,
height: 1,
},
"only head": testCase{
tree: &Node{Data: 1},
dfs: []interface{}{1},
bfs: []interface{}{1},
size: 1,
height: 1,
},
"triangle tree": testCase{
tree: &Node{Data: 1, Left: &Node{Data: 2}, Right: &Node{Data: 3}},
dfs: []interface{}{1, 2, 3},
bfs: []interface{}{1, 2, 3},
size: 3,
height: 2,
},
"line tree": testCase{
tree: &Node{
Data: 1,
Left: &Node{
Data: 2,
Left: &Node{
Data: 3,
},
},
},
dfs: []interface{}{1, 2, 3},
bfs: []interface{}{1, 2, 3},
size: 3,
height: 3,
},
"complex tree": testCase{
tree: &Node{
Data: 1,
Left: &Node{
Data: 2,
Left: &Node{Data: 4},
Right: &Node{Data: 5},
},
Right: &Node{Data: 3},
},
dfs: []interface{}{1, 2, 4, 5, 3},
bfs: []interface{}{1, 2, 3, 4, 5},
size: 5,
height: 3,
},
}
func TestDFS(t *testing.T) {
t.Parallel()
for name, tc := range testCases {
got := tc.tree.DFS()
if !reflect.DeepEqual(tc.dfs, got) {
t.Errorf("wrong dfs for %s. exp: %#v, got: %#v", name, tc.dfs, got)
}
}
}
func TestBFS(t *testing.T) {
t.Parallel()
for name, tc := range testCases {
got := tc.tree.BFS()
if !reflect.DeepEqual(tc.bfs, got) {
t.Errorf("wrong bfs for %s. exp: %#v, got: %#v", name, tc.bfs, got)
}
}
}
func TestSize(t *testing.T) {
t.Parallel()
for name, tc := range testCases {
got := tc.tree.Size()
if tc.size != got {
t.Errorf("wrong size for %s. exp: %#v, got: %#v", name, tc.size, got)
}
}
}
func TestHeight(t *testing.T) {
t.Parallel()
for name, tc := range testCases {
got := tc.tree.Height()
if tc.height != got {
t.Errorf("wrong height for %s. exp: %#v, got: %#v", name, tc.height, got)
}
}
}