| 51 | } |
| 52 | |
| 53 | func TestTreePreOrder(t *testing.T) { |
| 54 | t.Run("Test for Binary-Search Tree", func(t *testing.T) { |
| 55 | tests := []struct { |
| 56 | input []int |
| 57 | want []int |
| 58 | }{ |
| 59 | {[]int{90, 80, 100, 70, 85, 95, 105}, []int{90, 80, 70, 85, 100, 95, 105}}, |
| 60 | {[]int{90, 80, 100, 70, 85, 95, 105, 1, 21, 31, 41, 51, 61, 71}, []int{90, 80, 70, 1, 21, 31, 41, 51, 61, 71, 85, 100, 95, 105}}, |
| 61 | {[]int{10, 9, 8, 7, 6, 5, 4, 3, 2, 1}, []int{10, 9, 8, 7, 6, 5, 4, 3, 2, 1}}, |
| 62 | } |
| 63 | for _, tt := range tests { |
| 64 | tree := bt.NewBinarySearch[int]() |
| 65 | tree.Push(tt.input...) |
| 66 | if ret := tree.PreOrder(); !reflect.DeepEqual(ret, tt.want) { |
| 67 | t.Errorf("Error with PreOrder") |
| 68 | } |
| 69 | } |
| 70 | }) |
| 71 | |
| 72 | t.Run("Test for AVL Tree", func(t *testing.T) { |
| 73 | tests := []struct { |
| 74 | input []int |
| 75 | want []int |
| 76 | }{ |
| 77 | {[]int{90, 80, 100, 70, 85, 95, 105}, []int{90, 80, 70, 85, 100, 95, 105}}, |
| 78 | {[]int{90, 80, 100, 70, 85, 95, 105, 1, 21, 31, 41, 51, 61, 71}, []int{70, 41, 21, 1, 31, 51, 61, 90, 80, 71, 85, 100, 95, 105}}, |
| 79 | {[]int{10, 9, 8, 7, 6, 5, 4, 3, 2, 1}, []int{7, 3, 2, 1, 5, 4, 6, 9, 8, 10}}, |
| 80 | } |
| 81 | for _, tt := range tests { |
| 82 | tree := bt.NewAVL[int]() |
| 83 | tree.Push(tt.input...) |
| 84 | if ret := tree.PreOrder(); !reflect.DeepEqual(ret, tt.want) { |
| 85 | t.Errorf("Error with PreOrder") |
| 86 | } |
| 87 | } |
| 88 | }) |
| 89 | |
| 90 | t.Run("Test for Red-Black Tree", func(t *testing.T) { |
| 91 | tests := []struct { |
| 92 | input []int |
| 93 | want []int |
| 94 | }{ |
| 95 | {[]int{90, 80, 100, 70, 85, 95, 105}, []int{90, 80, 70, 85, 100, 95, 105}}, |
| 96 | {[]int{90, 80, 100, 70, 85, 95, 105, 1, 21, 31, 41, 51, 61, 71}, []int{80, 41, 21, 1, 31, 61, 51, 70, 71, 90, 85, 100, 95, 105}}, |
| 97 | {[]int{10, 9, 8, 7, 6, 5, 4, 3, 2, 1}, []int{7, 5, 3, 2, 1, 4, 6, 9, 8, 10}}, |
| 98 | } |
| 99 | for _, tt := range tests { |
| 100 | tree := bt.NewRB[int]() |
| 101 | tree.Push(tt.input...) |
| 102 | if ret := tree.PreOrder(); !reflect.DeepEqual(ret, tt.want) { |
| 103 | t.Errorf("Error with PreOrder") |
| 104 | } |
| 105 | } |
| 106 | }) |
| 107 | } |
| 108 | |
| 109 | func TestTreeInOrder(t *testing.T) { |
| 110 | lens := []int{100, 1_000, 10_000, 100_000} |