Skip to content

Commit 27cd32d

Browse files
committed
Commit tests for WalkLevelOrder()
1 parent 2b3c2b2 commit 27cd32d

File tree

1 file changed

+31
-0
lines changed

1 file changed

+31
-0
lines changed

binarytree_test.go

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -170,6 +170,37 @@ func TestWalkPostOrder(t *testing.T) {
170170
}
171171
}
172172

173+
func TestWalkLevelOrder(t *testing.T) {
174+
// Our test tree
175+
//
176+
// __1
177+
// / \
178+
// 2 3
179+
// / \
180+
// 4 5
181+
//
182+
root := binarytree.NewNode(1)
183+
two := root.InsertLeft(2)
184+
root.InsertRight(3)
185+
two.InsertLeft(4)
186+
two.InsertRight(5)
187+
188+
result := make([]int, 0)
189+
wantResult := []int{1, 2, 3, 4, 5}
190+
walkFunc := func(node *binarytree.Node[int]) error {
191+
result = append(result, node.Value)
192+
return nil
193+
}
194+
195+
if err := root.WalkLevelOrder(walkFunc); err != nil {
196+
t.Fatal(err)
197+
}
198+
199+
if !reflect.DeepEqual(wantResult, result) {
200+
t.Fatalf("want level-order values %v, got %v", wantResult, result)
201+
}
202+
}
203+
173204
func TestSkipNodeHandlers(t *testing.T) {
174205
// Construct the following simple binary tree
175206
//

0 commit comments

Comments
 (0)