Skip to content

Commit 9abcbe7

Browse files
committed
comment & cleanup
1 parent 130d04a commit 9abcbe7

File tree

1 file changed

+11
-23
lines changed

1 file changed

+11
-23
lines changed

bst_test.go

+11-23
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import (
55
"testing"
66
)
77

8+
// Ensure items are inserted into the tree
89
func TestInsertSingle(t *testing.T) {
910
tests := []struct {
1011
name string
@@ -66,6 +67,7 @@ func TestInsertBulkCheckCorrectStructure(t *testing.T) {
6667
}
6768
}
6869

70+
// Make sure that we form a tree of the expected height
6971
func TestHeightCount(t *testing.T) {
7072
tests := []struct {
7173
name string
@@ -97,6 +99,7 @@ func TestHeightCount(t *testing.T) {
9799
}
98100
}
99101

102+
// Test find Min/Max functionality of the BST
100103
func TestMinMax(t *testing.T) {
101104
tests := []struct {
102105
name string
@@ -146,15 +149,11 @@ func TestMinMax(t *testing.T) {
146149
// Fulfill interface for Sort, see: https://gobyexample.com/sorting-by-functions
147150
type myIntSlice []MyInt
148151

149-
func (s myIntSlice) Len() int {
150-
return len(s)
151-
}
152-
func (s myIntSlice) Swap(i, j int) {
153-
s[i], s[j] = s[j], s[i]
154-
}
155-
func (s myIntSlice) Less(i, j int) bool {
156-
return s[i] < s[j]
157-
}
152+
func (s myIntSlice) Len() int { return len(s) }
153+
func (s myIntSlice) Swap(i, j int) { s[i], s[j] = s[j], s[i] }
154+
func (s myIntSlice) Less(i, j int) bool { return s[i] < s[j] }
155+
156+
// Ensure that in-order traversal works as expected
158157
func TestInOrder(t *testing.T) {
159158
tests := []struct {
160159
name string
@@ -175,16 +174,9 @@ func TestInOrder(t *testing.T) {
175174
t.Run(test.name, func(t *testing.T) {
176175
test.tree = &Node{}
177176
_, _ = insertBulk(test.tree, test.input)
178-
// Test Min
179177
got := test.tree.InOrder()
180-
//var want []int
181-
sort.Sort(myIntSlice(test.input))
178+
sort.Sort(myIntSlice(test.input)) // Use Sort to verify traversal
182179
want := test.input
183-
//if !reflect.DeepEqual(want, got) {
184-
// if !(len(want) == 0 && len(got) == 0) { // TODO DeepEqual on empty slices
185-
// t.Errorf("wrong InOrder(). got %+v, want %+v", got, want)
186-
// }
187-
//}
188180
for i := 0; i < len(want)-1; i++ {
189181
if !(got[i].Equals(want[i])) {
190182
t.Errorf("wrong InOrder(). got %+v, want %+v", got, want)
@@ -194,7 +186,8 @@ func TestInOrder(t *testing.T) {
194186
}
195187
}
196188

197-
func TestInSearch(t *testing.T) {
189+
// Test BST's search function
190+
func TestSearch(t *testing.T) {
198191
tests := []struct {
199192
name string
200193
tree *Node
@@ -253,11 +246,7 @@ func TestRemove(t *testing.T) {
253246
t.Run(test.name, func(t *testing.T) {
254247
test.tree = &Node{}
255248
_, _ = insertBulk(test.tree, test.input)
256-
// Test Remove
257-
//fmt.Println("Test tree bf remove", test.tree)
258249
test.tree = test.tree.Remove(test.removeVal)
259-
//fmt.Println("Test tree af remove", test.tree)
260-
261250
// test that only the specified node was removed
262251
for _, inputVal := range test.input {
263252
stillPresent, _ := test.tree.Search(inputVal)
@@ -279,7 +268,6 @@ func TestRemove(t *testing.T) {
279268
// make sure the ordering is still correct
280269
gotOrdered := test.tree.InOrder()
281270
for i := 1; i < len(gotOrdered); i++ {
282-
//fmt.Println("hi")
283271
if !(gotOrdered[i-i].Less(gotOrdered[i])) {
284272
t.Errorf("Remove(%v) not in ascending order %v %v %v", test.removeVal, gotOrdered, test.input, i)
285273
}

0 commit comments

Comments
 (0)