-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathavl_test.go
304 lines (260 loc) · 8.2 KB
/
avl_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
package avl
import (
"fmt"
"slices"
"testing"
)
func rangeWithSteps(start, end, step int) []int {
var result []int
for i := start; i <= end; i += step {
result = append(result, i)
}
return result
}
func assert[T comparable](a, b T, msg string, t *testing.T) {
if a != b {
t.Errorf("%s %v != %v", msg, a, b)
}
}
func assertSlice[T comparable](a, b []T, msg string, t *testing.T) {
if !slices.Equal(a, b) {
t.Errorf("%s\nexpected: %v\ngot: %v", msg, b, a)
}
}
var cases = [][]int{
{}, // Empty tree
{1, 2, 3}, // Right-Right case
{1, 3, 2}, // Right-Left case
{2, 1, 3}, // Left-Left case
{2, 3, 1}, // Left-Right case
{3, 1, 2}, // Left-Right case
{3, 2, 1}, // Left-Left case
{10, 20, 30, 40, 50}, // Multiple Right-Right rotations
{10, 20, 30, 50, 40}, // Right-Right followed by Right-Left
{30, 20, 10, 5, 1}, // Multiple Left-Left rotations
{30, 20, 10, 1, 5}, // Left-Left followed by Left-Right
{5, 4, 6, 3, 7, 2, 8}, // Mixed rotations
{-1, -2, -3}, // Negative values
{-5, -3, 1, 3, 5}, // Negative and positive values
{50, 40, 60, 30, 70, 20, 80, 45},
{50, 40, 60, 30, 70, 20, 80, 15},
{50, 40, 60, 30, 70, 20, 80, 35},
{50, 40, 60, 30, 70, 20, 80, 25},
rangeWithSteps(-9, 16, 2),
rangeWithSteps(0, 34, 3),
}
// Test insertNode method, checking that insertion follows BST properties
func TestInsertNode(t *testing.T) {
insertCases := [][]int{
{10, 5, 15, 4, 6, 14, 16}, // Positives
{0, -5, 5, -6, -4, 4, 6}, // Zero
{-10, -15, -5, -16, -14, -6, -4}, // Negatives
// Example:
// 10
// / \
// / \
// 5 15
// / \ / \
// 4 6 14 16
}
type sampleTree struct {
root int
lsub int
rsub int
lsubl int
lsubr int
rsubl int
rsubr int
}
for _, testCase := range insertCases {
tree := NewAvlTree[int]()
sample := sampleTree{
root: testCase[0],
lsub: testCase[1],
rsub: testCase[2],
lsubl: testCase[3],
lsubr: testCase[4],
rsubl: testCase[5],
rsubr: testCase[6],
}
for _, v := range testCase {
tree.insertNode(v)
}
root := tree.getRootNode()
assert(root.value, sample.root, "insertNode (root)", t)
assert(root.left.value, sample.lsub, "insertNode(root.left)", t)
assert(root.right.value, sample.rsub, "insertNode(root.right)", t)
assert(root.left.left.value, sample.lsubl, "insertNode(root.left.left)", t)
assert(root.left.right.value, sample.lsubr, "insertNode(root.left.right)", t)
assert(root.right.left.value, sample.rsubl, "insertNode(root.right.left)", t)
assert(root.right.right.value, sample.rsubr, "insertNode(root.right.right)", t)
}
}
// Test Contains method, indicating whether a value exists in the AVL tree.
func TestContains(t *testing.T) {
for _, testCase := range cases {
values := testCase
tree := NewAvlTree[int]()
for _, v := range values {
tree.insertNode(v)
assert(tree.Contains(v), true, fmt.Sprintf("tree.Contains(%v)", v), t)
}
}
}
func populateTree(t *testing.T, values []int) *AvlTree[int] {
tree := NewAvlTree[int]()
for i, v := range values {
tree.Add(v)
assert(tree.Contains(v), true, fmt.Sprintf("tree.Add(%v", v), t)
assert(i+1, tree.Size(), "tree size after Add", t)
}
return tree
}
// Tests the AVL tree with integer values, covering all basic rotation cases
func TestIntegerTree(t *testing.T) {
for _, testCase := range cases {
tree := populateTree(t, testCase)
actual := tree.InOrderTraverse()
expected := slices.Clone(testCase)
slices.Sort(expected)
assertSlice(actual, expected, "tree.Add(...)", t)
}
}
// Tests the AVL tree with string values
func TestStringTree(t *testing.T) {
cases := [][]string{
{"chickpeas", "tahini", "za'atar"}, // Ordered
{"za'atar", "tahini", "chickpeas"}, // Reversed
{"tahini", "za'atar", "chickpeas"}, // Mixed
{"a", "b", "c", "d", "e"}, // Sequence
{"e", "d", "c", "b", "a"}, // Reversed sequence
}
for _, testCase := range cases {
tree := NewAvlTree[string]()
for _, value := range testCase {
tree.Add(value)
assert(tree.Contains(value), true, fmt.Sprintf("tree.Add(%v)", value), t)
}
actual := tree.InOrderTraverse()
expected := slices.Clone(testCase)
slices.Sort(expected)
assertSlice(actual, expected, "tree.Add(...)", t)
}
}
// Tests the AVL tree with floating-point values
func TestFloatTree(t *testing.T) {
cases := [][]float64{
{1.1, 2.2, 3.3}, // Ordered
{3.3, 2.2, 1.1}, // Reversed
{2.2, 1.1, 3.3}, // Mixed
}
for _, testCase := range cases {
tree := NewAvlTree[float64]()
for _, value := range testCase {
tree.Add(value)
assert(tree.Contains(value), true, fmt.Sprintf("tree.Add(%v)", value), t)
}
actual := tree.InOrderTraverse()
expected := slices.Clone(testCase)
slices.Sort(expected)
assertSlice(actual, expected, "tree.Add(...)", t)
}
}
// Test negative case for Contains method
func TestDoesNotContain(t *testing.T) {
tree := populateTree(t, []int{1, 2, 3})
assert(tree.Contains(4), false, "tree.Contains(4)", t)
}
// Test removing a value from the tree
func TestRemoveValues(t *testing.T) {
for _, testCase := range cases {
for _, v := range testCase {
tree := populateTree(t, testCase)
size := tree.Size()
// Successful remove returns true, so negate this to check against
// `Contains(value) == false`
assert(tree.Remove(v), !tree.Contains(v), "tree.Remove(v)", t)
assert(tree.Size(), size-1, "tree.size after Remove", t)
// Ensure order was maintained during removal
actualValues := tree.InOrderTraverse()
expectedValues := slices.Clone(actualValues)
slices.Sort(expectedValues)
assertSlice(actualValues, expectedValues, "tree.Remove(v)", t)
}
}
}
// Test removing a value that does not exist in the AVL tree (negative case)
func TestRemoveNonexistingValue(t *testing.T) {
values := []int{1, 2, 3}
tree := populateTree(t, values)
size := tree.Size()
assert(tree.Remove(0), tree.Contains(0), "tree.Remove(0)", t)
assert(tree.Size(), size, "tree.size after Remove", t)
}
// Test removing multiple values until the tree is empty
func TestRemoveMultipleValues(t *testing.T) {
for _, testCase := range cases {
tree := populateTree(t, testCase)
for _, v := range testCase {
assert(tree.Remove(v), !tree.Contains(v), fmt.Sprintf("tree.Remove(%v)", v), t)
}
assert(tree.IsEmpty(), true, "tree.IsEmpty()", t)
assert(tree.Size(), 0, "tree.size after Remove", t)
}
}
func TestClearTree(t *testing.T) {
testCase := []int{1, 2, 3, 4, 5, 6, 7, 8, 9}
tree := populateTree(t, testCase)
tree.Clear()
assert(tree.IsEmpty(), true, "tree.Clear()", t)
assert(tree.Size(), 0, "tree.size after Remove", t)
}
func TestGetMinNode(t *testing.T) {
for _, testCase := range cases {
tree := populateTree(t, testCase)
actualMinValue, err := tree.GetMin()
if len(testCase) == 0 { // Empty tree case
assert(err != nil, true, "tree.GetMin()", t)
} else {
expectedMinValue := slices.Min(testCase)
assert(actualMinValue, expectedMinValue, "tree.GetMin()", t)
}
}
}
func TestGetMaxNode(t *testing.T) {
for _, testCase := range cases {
tree := populateTree(t, testCase)
actualMaxValue, err := tree.GetMax()
if len(testCase) == 0 { // Empty tree case
assert(err != nil, true, "tree.GetMax()", t)
} else {
expectedMaxValue := slices.Max(testCase)
// assert(tree.GetMax().value, maxValue, "tree.GetMax()", t)
assert(actualMaxValue, expectedMaxValue, "tree.GetMax()", t)
}
}
}
// Test AvlTreeIterator.Next() returns next in-order value on each call
func TestAvlTreeIterator(t *testing.T) {
for _, testCase := range cases {
tree := populateTree(t, testCase)
iter := tree.NewIterator()
actual := make([]int, 0)
expected := slices.Clone(testCase)
slices.Sort(expected)
v, index := iter.Next()
for index != -1 {
actual = append(actual, v)
// Test next slice value is the same as sorted test case at index
assert(v, expected[index], "iterator.Next()", t)
v, index = iter.Next()
}
// Test slice created from iterator is sorted
assertSlice(actual, expected, "tree iterator", t)
// Test that subsequent calls to iter.Next() return -1 index
for range testCase {
_, index = iter.Next()
assert(index, -1, "iterator.Next() (end of iterator)", t)
}
}
}