MCPcopy Index your code
hub / github.com/TheAlgorithms/Go / TestTreeAccessNodesByLayer

Function TestTreeAccessNodesByLayer

structure/tree/tree_test.go:354–415  ·  view source on GitHub ↗
(t *testing.T)

Source from the content-addressed store, hash-verified

352}
353
354func TestTreeAccessNodesByLayer(t *testing.T) {
355 t.Run("Test for Binary-Search Tree", func(t *testing.T) {
356 tests := []struct {
357 input []int
358 want [][]int
359 }{
360 {[]int{90, 80, 100, 70, 85, 95, 105}, [][]int{{90}, {80, 100}, {70, 85, 95, 105}}},
361 {[]int{90, 80, 100, 70, 85, 95, 105, 1, 21, 31, 41, 51, 61, 71},
362 [][]int{{90}, {80, 100}, {70, 85, 95, 105}, {1, 71}, {21}, {31}, {41}, {51}, {61}}},
363 {[]int{10, 9, 8, 7, 6, 5, 4, 3, 2, 1}, [][]int{{10}, {9}, {8}, {7}, {6}, {5}, {4}, {3}, {2}, {1}}},
364 {[]int{}, [][]int{}},
365 }
366 for i, tt := range tests {
367 tree := bt.NewBinarySearch[int]()
368 tree.Push(tt.input...)
369 if ret := tree.AccessNodesByLayer(); !reflect.DeepEqual(ret, tt.want) {
370 t.Errorf("#%d Error with AccessNoedsByLayer", i)
371 }
372 }
373 })
374
375 t.Run("Test for AVL Tree", func(t *testing.T) {
376 tests := []struct {
377 input []int
378 want [][]int
379 }{
380 {[]int{90, 80, 100, 70, 85, 95, 105}, [][]int{{90}, {80, 100}, {70, 85, 95, 105}}},
381 {[]int{90, 80, 100, 70, 85, 95, 105, 1, 21, 31, 41, 51, 61, 71},
382 [][]int{{70}, {41, 90}, {21, 51, 80, 100}, {1, 31, 61, 71, 85, 95, 105}}},
383 {[]int{10, 9, 8, 7, 6, 5, 4, 3, 2, 1}, [][]int{{7}, {3, 9}, {2, 5, 8, 10}, {1, 4, 6}}},
384 {[]int{}, [][]int{}},
385 }
386 for i, tt := range tests {
387 tree := bt.NewAVL[int]()
388 tree.Push(tt.input...)
389 if ret := tree.AccessNodesByLayer(); !reflect.DeepEqual(ret, tt.want) {
390 t.Errorf("#%d Error with AccessNoedsByLayer", i)
391 }
392 }
393 })
394
395 t.Run("Test for Red-Black Tree", func(t *testing.T) {
396 tests := []struct {
397 input []int
398 want [][]int
399 }{
400 {[]int{90, 80, 100, 70, 85, 95, 105}, [][]int{{90}, {80, 100}, {70, 85, 95, 105}}},
401 {[]int{90, 80, 100, 70, 85, 95, 105, 1, 21, 31, 41, 51, 61, 71},
402 [][]int{{80}, {41, 90}, {21, 61, 85, 100}, {1, 31, 51, 70, 95, 105}, {71}}},
403 {[]int{10, 9, 8, 7, 6, 5, 4, 3, 2, 1}, [][]int{{7}, {5, 9}, {3, 6, 8, 10}, {2, 4}, {1}}},
404 {[]int{}, [][]int{}},
405 }
406 for i, tt := range tests {
407 tree := bt.NewRB[int]()
408 t.Log(reflect.TypeOf(tree).String() == "*tree.RB[int]")
409 tree.Push(tt.input...)
410 if ret := tree.AccessNodesByLayer(); !reflect.DeepEqual(ret, tt.want) {
411 t.Errorf("#%d Error with AccessNoedsByLayer, %v", i, ret)

Callers

nothing calls this directly

Calls 3

StringMethod · 0.80
PushMethod · 0.65
AccessNodesByLayerMethod · 0.65

Tested by

no test coverage detected