Skip to content

Commit 26b0e2d

Browse files
Add btree tests (#356)
1 parent 87f185c commit 26b0e2d

File tree

1 file changed

+147
-0
lines changed

1 file changed

+147
-0
lines changed

structure/binarytree/btree_test.go

Lines changed: 147 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,147 @@
1+
package binarytree
2+
3+
import (
4+
"reflect"
5+
"testing"
6+
)
7+
8+
func TestInsert(t *testing.T) {
9+
BTree := BTree{
10+
Root: NewNode(90),
11+
}
12+
13+
root := BTree.Root
14+
15+
Insert(root, 80)
16+
Insert(root, 100)
17+
18+
if root.val != 90 {
19+
t.Errorf("root should have value = 90")
20+
}
21+
22+
if root.left.val != 80 {
23+
t.Errorf("left child should have value = 80")
24+
}
25+
26+
if root.right.val != 100 {
27+
t.Errorf("right child should have value = 100")
28+
}
29+
30+
if BTree.Depth() != 2 {
31+
t.Errorf("tree should have depth = 1")
32+
}
33+
}
34+
35+
func TestDelete(t *testing.T) {
36+
t.Run("Delete a node with no child", func(t *testing.T) {
37+
BTree := BTree{
38+
Root: NewNode(90),
39+
}
40+
41+
root := BTree.Root
42+
43+
Insert(root, 80)
44+
Insert(root, 100)
45+
46+
BstDelete(root, 100)
47+
48+
if root.val != 90 {
49+
t.Errorf("root should have value = 90")
50+
}
51+
52+
if root.left.val != 80 {
53+
t.Errorf("left child should have value = 80")
54+
}
55+
56+
if root.right != nil {
57+
t.Errorf("right child should have value = nil")
58+
}
59+
60+
if BTree.Depth() != 2 {
61+
t.Errorf("Depth should have value = 2")
62+
}
63+
})
64+
65+
t.Run("Delete a node with one child", func(t *testing.T) {
66+
BTree := BTree{
67+
Root: NewNode(90),
68+
}
69+
70+
root := BTree.Root
71+
72+
Insert(root, 80)
73+
Insert(root, 100)
74+
Insert(root, 70)
75+
76+
BstDelete(root, 80)
77+
78+
if root.val != 90 {
79+
t.Errorf("root should have value = 90")
80+
}
81+
82+
if root.right.val != 100 {
83+
t.Errorf("right child should have value = 100")
84+
}
85+
86+
if root.left.val != 70 {
87+
t.Errorf("left child should have value = 70")
88+
}
89+
90+
if BTree.Depth() != 2 {
91+
t.Errorf("Depth should have value = 2")
92+
}
93+
})
94+
95+
t.Run("Delete a node with two children", func(t *testing.T) {
96+
BTree := BTree{
97+
Root: NewNode(90),
98+
}
99+
100+
root := BTree.Root
101+
102+
Insert(root, 80)
103+
Insert(root, 100)
104+
Insert(root, 70)
105+
Insert(root, 85)
106+
107+
BstDelete(root, 80)
108+
109+
if root.val != 90 {
110+
t.Errorf("root should have value = 90")
111+
}
112+
113+
if root.left.val != 85 {
114+
t.Errorf("left child should have value = 85")
115+
}
116+
117+
if root.right.val != 100 {
118+
t.Errorf("right child should have value = 100")
119+
}
120+
121+
if BTree.Depth() != 3 {
122+
t.Errorf("Depth should have value = 3")
123+
}
124+
})
125+
}
126+
127+
func TestAccessNodesByLayer(t *testing.T) {
128+
BTree := BTree{
129+
Root: NewNode(90),
130+
}
131+
132+
root := BTree.Root
133+
134+
Insert(root, 80)
135+
Insert(root, 100)
136+
Insert(root, 70)
137+
Insert(root, 85)
138+
Insert(root, 95)
139+
Insert(root, 105)
140+
141+
a := AccessNodesByLayer(root)
142+
b := [][]int{{90}, {80, 100}, {70, 85, 95, 105}}
143+
144+
if !reflect.DeepEqual(a, b) {
145+
t.Errorf("Nodes should have value = [[90] [80 100] [70 85 95 105]]")
146+
}
147+
}

0 commit comments

Comments
 (0)