-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathavl_test.go
516 lines (451 loc) · 10.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
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
package avl
import (
"fmt"
"testing"
)
type Record struct {
Key int
Data int
}
func (r *Record) Less(k Key) bool {
return r.Key < k.(*Record).Key
}
func (r *Record) Equals(k Key) bool {
return r.Key == k.(*Record).Key
}
type RecordTree struct {
Tree
}
func (rt *RecordTree) Get(key Key) *Record {
v := rt.Tree.Get(key)
if v == nil {
return nil
}
return v.(*Record)
}
func TestRecord(t *testing.T) {
nt := &Tree{}
key := 10
nt.Put(&Record{Key: key, Data: 10})
v := nt.Get(&Record{Key: key})
if v == nil {
t.Errorf("Failed to get %v", key)
}
r := v.(*Record)
if r.Data != 10 {
t.Errorf("Data should be 10, not %v", r.Data)
}
}
func TestRecordTree(t *testing.T) {
nt := &RecordTree{}
key := 10
nt.Put(&Record{Key: key, Data: 20})
v := nt.Get(&Record{Key: key})
if v == nil {
t.Errorf("Failed to get %v", key)
}
if v.Data != 20 {
t.Errorf("Data should be 10, not %v", v.Data)
}
}
func TestTreeGet(t *testing.T) {
nt := NewTree()
nt.root = buildTree(7)
n := nt.Get(testKey(10))
if n != nil {
t.Errorf("Key 10 should not exist: %v", n)
}
n = nt.Get(testKey(4))
i, ok := n.(testKey)
if !ok || i != 4 {
t.Errorf("Get should return 4, not %v", i)
}
}
func TestTreeClear(t *testing.T) {
nt := NewTree()
nt.root = buildTree(7)
nt.Clear()
if nt.root != nil {
t.Errorf("Root node should be null")
}
}
func TestTreeHas(t *testing.T) {
nt := NewTree()
nt.root = buildTree(7)
if nt.Has(testKey(10)) {
t.Error("Key 10 should not exist")
}
if !nt.Has(testKey(4)) {
t.Error("Key 4 should exist")
}
}
func TestTreePut(t *testing.T) {
nt := NewTree()
nt.Put(testKey(4))
if !nt.Has(testKey(4)) {
t.Error("Key 4 should exist")
}
}
func TestTreeDel(t *testing.T) {
nt := NewTree()
nt.Put(testKey(4))
nt.Del(testKey(4))
if nt.Has(testKey(4)) {
t.Error("Key 4 should not exist")
}
}
func TestTreeSize(t *testing.T) {
nt := NewTree()
if nt.Size() != 0 {
t.Errorf("Empty tree should have 0 size, not %v", nt.Size())
}
nt.Put(testKey(4))
if nt.Size() != 1 {
t.Errorf("Expected tree size 1, not %v", nt.Size())
}
nt.Del(testKey(4))
if nt.Size() != 0 {
t.Errorf("Empty tree should have 0 size, not %v", nt.Size())
}
}
func TestTreeVisitAscending(t *testing.T) {
nt := NewTree()
nt.root = buildTree(7)
ints := make([]int, 0)
nt.VisitAscending(func(d Key) bool {
ints = append(ints, int(d.(testKey)))
return true
})
expected := []int{1, 2, 3, 4, 5, 6, 7}
if len(expected) != len(ints) {
t.Errorf("Expected nodes %#v, not %#v", expected, ints)
return
}
for i, v := range ints {
if expected[i] != v {
t.Errorf("Expected %v, not %v", expected[i], v)
}
}
}
func TestTreeVisitDescending(t *testing.T) {
nt := NewTree()
nt.root = buildTree(7)
ints := make([]int, 0)
nt.VisitDescending(func(d Key) bool {
ints = append(ints, int(d.(testKey)))
return true
})
expected := []int{7, 6, 5, 4, 3, 2, 1}
if len(expected) != len(ints) {
t.Errorf("Expected nodes %#v, not %#v", expected, ints)
return
}
for i, v := range ints {
if expected[i] != v {
t.Errorf("Expected %v, not %v", expected[i], v)
}
}
}
func TestMax(t *testing.T) {
m := max(3, 2)
if m != 3 {
t.Errorf("Max should be 3, not %v", m)
}
m = max(2, 3)
if m != 3 {
t.Errorf("Max should be 2, not %v", m)
}
m = max(3, 3)
if m != 3 {
t.Errorf("Max should be 3, not %v", m)
}
}
func TestHeight(t *testing.T) {
h := height(nil)
if h != -1 {
t.Errorf("Height should be -1, not %v", h)
}
h = height(&node{height: 2})
if h != 2 {
t.Errorf("Height should be 2, not %v", h)
}
}
func TestUpdateHeight(t *testing.T) {
n := &node{height: 10}
updateHeight(n)
if n.height != 0 {
t.Errorf("Height should be 0, not %v", n.height)
}
n.right = &node{height: 3}
n.left = &node{height: 4}
updateHeight(n)
if n.height != 5 {
t.Errorf("Height should be 5, not %v", n.height)
}
}
func TestBalanceFactor(t *testing.T) {
n := &node{}
n.right = &node{height: 6}
n.left = &node{height: 4}
b := balanceFactor(n)
if b != -2 {
t.Errorf("Balance should be -2, not %v", b)
}
n.left.height = 8
b = balanceFactor(n)
if b != 2 {
t.Errorf("Balance should be 2, not %v", b)
}
}
func TestInsertRR(t *testing.T) {
nt := &Tree{}
n := nt.insert(nil, testKey(10))
if n.key.(testKey) != 10 {
t.Errorf("Insert should have inserted 10")
}
n = nt.insert(n, testKey(15))
n = nt.insert(n, testKey(20))
if n.key.(testKey) != 15 {
t.Errorf("Expected 5, not %v", n.key.(testKey))
}
if n.left.key.(testKey) != 10 {
t.Errorf("Expected 10, not %v", n.key.(testKey))
}
if n.right.key.(testKey) != 20 {
t.Errorf("Expected 20, not %v", n.key.(testKey))
}
}
func TestInsertLL(t *testing.T) {
nt := &Tree{}
n := nt.insert(nil, testKey(20))
if n.key.(testKey) != 20 {
t.Errorf("Insert should have inserted 20")
}
n = nt.insert(n, testKey(15))
n = nt.insert(n, testKey(10))
if n.key.(testKey) != 15 {
t.Errorf("Expected 15, not %v", n.key.(testKey))
}
if n.left.key.(testKey) != 10 {
t.Errorf("Expected 10, not %v", n.key.(testKey))
}
if n.right.key.(testKey) != 20 {
t.Errorf("Expected 20, not %v", n.key.(testKey))
}
}
func TestInsertRL(t *testing.T) {
nt := &Tree{}
n := nt.insert(nil, testKey(10))
if n.key.(testKey) != 10 {
t.Errorf("Insert should have inserted 10")
}
n = nt.insert(n, testKey(20))
n = nt.insert(n, testKey(15))
if n.key.(testKey) != 15 {
t.Errorf("Expected 15, not %v", n.key.(testKey))
}
if n.left.key.(testKey) != 10 {
t.Errorf("Expected 10, not %v", n.key.(testKey))
}
if n.right.key.(testKey) != 20 {
t.Errorf("Expected 20, not %v", n.key.(testKey))
}
}
func TestInsertLR(t *testing.T) {
nt := &Tree{}
n := nt.insert(nil, testKey(20))
if n.key.(testKey) != 20 {
t.Errorf("Insert should have inserted 20")
}
n = nt.insert(n, testKey(10))
n = nt.insert(n, testKey(15))
if n.key.(testKey) != 15 {
t.Errorf("Expected 15, not %v", n.key.(testKey))
}
if n.left.key.(testKey) != 10 {
t.Errorf("Expected 10, not %v", n.key.(testKey))
}
if n.right.key.(testKey) != 20 {
t.Errorf("Expected 20, not %v", n.key.(testKey))
}
}
func TestLookup(t *testing.T) {
nt := &Tree{}
n := nt.insert(nil, testKey(20))
n = nt.insert(n, testKey(30))
n = nt.insert(n, testKey(40))
n = nt.insert(n, testKey(50))
n = nt.insert(n, testKey(60))
v := lookup(n, testKey(70))
if v != nil {
t.Errorf("Lookup should fail")
}
v = lookup(n, testKey(50))
if v == nil {
t.Errorf("Lookup should be successful")
}
if v != nil && v.key.(testKey) != 50 {
t.Errorf("Lookup should return 50")
}
v = lookup(n, testKey(20))
if v == nil {
t.Errorf("Lookup should be successful")
}
if v != nil && v.key.(testKey) != 20 {
t.Errorf("Lookup should return 20")
}
}
func TestRemoveNonExisting(t *testing.T) {
nt := &Tree{}
nt.root = buildTree(7)
nt.root = nt.remove(nt.root, testKey(20))
nodes := []int{1, 2, 3, 4, 5, 6, 7}
for _, err := range verifyTree(nt.root, nodes) {
t.Error(err.Error())
}
}
func TestRemoveLeaf(t *testing.T) {
nt := &Tree{}
nt.root = buildTree(7)
nt.root = nt.remove(nt.root, testKey(7))
nodes := []int{1, 2, 3, 4, 5, 6}
for _, err := range verifyTree(nt.root, nodes) {
t.Error(err.Error())
}
}
func TestRemoveLeftChild(t *testing.T) {
nt := &Tree{}
nt.root = buildTree(7)
nt.root = nt.remove(nt.root, testKey(7))
nt.root = nt.remove(nt.root, testKey(6))
nodes := []int{1, 2, 3, 4, 5}
for _, err := range verifyTree(nt.root, nodes) {
t.Error(err.Error())
}
}
func TestRemoveRightChild(t *testing.T) {
nt := &Tree{}
nt.root = buildTree(7)
nt.root = nt.remove(nt.root, testKey(5))
nt.root = nt.remove(nt.root, testKey(6))
nodes := []int{1, 2, 3, 4, 7}
for _, err := range verifyTree(nt.root, nodes) {
t.Error(err.Error())
}
}
func TestRemoveRoot(t *testing.T) {
nt := &Tree{}
nt.root = buildTree(7)
nt.root = nt.remove(nt.root, testKey(4))
nodes := []int{1, 2, 3, 5, 6, 7}
for _, err := range verifyTree(nt.root, nodes) {
t.Error(err.Error())
}
}
func TestRebalance(t *testing.T) {
n := rebalance(nil)
if n != nil {
t.Errorf("Rebalance should return nil")
}
}
func TestVisitAscending(t *testing.T) {
n := buildTree(7)
ints := make([]int, 0)
expected := []int{1, 2, 3, 4, 5, 6, 7}
visitAscending(n, func(d Key) bool {
ints = append(ints, int(d.(testKey)))
return true
})
if len(expected) != len(ints) {
t.Errorf("Expected nodes %#v, not %#v", expected, ints)
return
}
for i, v := range ints {
if expected[i] != v {
t.Errorf("Expected %v, not %v", expected[i], v)
}
}
}
func TestVisitDescending(t *testing.T) {
n := buildTree(7)
ints := make([]int, 0)
expected := []int{7, 6, 5, 4, 3, 2, 1}
visitDescending(n, func(d Key) bool {
ints = append(ints, int(d.(testKey)))
return true
})
if len(expected) != len(ints) {
t.Errorf("Expected nodes %#v, not %#v", expected, ints)
return
}
for i, v := range ints {
if expected[i] != v {
t.Errorf("Expected %v, not %v", expected[i], v)
}
}
}
func TestDescendingExit(t *testing.T) {
n := buildTree(7)
ints := make([]int, 0)
visitDescending(n, func(d Key) bool {
ints = append(ints, int(d.(testKey)))
return false
})
if 1 != len(ints) {
t.Errorf("Expected nodes %#v, not %#v", 1, ints)
return
}
}
func TestAscendingExit(t *testing.T) {
n := buildTree(7)
ints := make([]int, 0)
visitAscending(n, func(d Key) bool {
ints = append(ints, int(d.(testKey)))
return false
})
if 1 != len(ints) {
t.Errorf("Expected nodes %#v, not %#v", 1, ints)
return
}
}
func buildTree(max int) *node {
nt := &Tree{}
if max < 1 {
return nil
}
for i := 1; i <= max; i++ {
nt.root = nt.insert(nt.root, testKey(i))
}
return nt.root
}
func verifyTree(n *node, vals []int) []error {
var errors []error
ints := make([]int, 0)
visitAscending(n, func(d Key) bool {
ints = append(ints, int(d.(testKey)))
return true
})
intsLen := len(ints)
valsLen := len(vals)
if valsLen != intsLen {
msg := "Expected tree size %v, not %v"
err := fmt.Errorf(msg, valsLen, intsLen)
errors = append(errors, err)
return errors
}
for i, v := range vals {
if ints[i] != v {
msg := "Incorrect traversal at: %v, %v"
err := fmt.Errorf(msg, i, ints[i])
errors = append(errors, err)
}
}
return errors
}
type testKey int
func (tk testKey) Less(rhs Key) bool {
return tk < rhs.(testKey)
}
func (tk testKey) Equals(rhs Key) bool {
return tk == rhs.(testKey)
}