-
Notifications
You must be signed in to change notification settings - Fork 1
/
avl_test.go
150 lines (136 loc) · 3.75 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
package main
import (
"fmt"
"testing"
)
type DummyData struct {
data int
}
func (d DummyData) String() string {
return fmt.Sprintf("%d", d.data)
}
func TestAvlGetAll(t *testing.T) {
// Should produce an in-order traversal
var root *AvlNode
root = &AvlNode{key: 10, left: nil, right: nil, height: 1, data: &DummyData{}}
root = AvlInsert(root, 20, &DummyData{data: 20}, false)
root = AvlInsert(root, 30, &DummyData{data: 30}, false)
nodes := AvlGetAll(root)
if nodes[0].key != 10 && nodes[1].key != 20 && nodes[2].key != 30 {
t.Fail()
}
}
func TestAvlInsertDup(t *testing.T) {
// Should only have one element
var root *AvlNode
root = &AvlNode{key: 10, left: nil, right: nil, height: 1, data: &DummyData{}}
root = AvlInsert(root, 10, &DummyData{data: 10}, false)
root = AvlInsert(root, 10, &DummyData{data: 10}, false)
nodes := AvlGetAll(root)
if len(nodes) != 1 {
t.Fail()
}
}
func TestAvlSearch(t *testing.T) {
var root *AvlNode
root = &AvlNode{key: 10, left: nil, right: nil, height: 1}
var i uint64
for i = 0; i < 1000; i++ {
root = AvlInsert(root, i, &DummyData{data: int(i)}, false)
}
tmp1 := AvlSearch(root, 578)
tmp2 := AvlSearch(root, 1200)
if tmp1.String() != "578" || tmp2 != nil {
t.Fail()
}
}
func TestAvlInsert(t *testing.T) {
var root *AvlNode
root = &AvlNode{key: 10, left: nil, right: nil, height: 1, data: &DummyData{}}
var i uint64
for i = 0; i < 1000; i++ {
root = AvlInsert(root, i, nil, false)
}
// Check balanced
b := GetBalanceFactor(root)
if b > 1 || b < -1 {
t.Fail()
}
}
func TestAvlDelete(t *testing.T) {
var root *AvlNode
root = &AvlNode{key: 10, left: nil, right: nil, height: 1}
var i uint64
for i = 0; i < 1000; i++ {
root = AvlInsert(root, i, nil, false)
}
// Should have roughly 0-499 in left subtree and 500-999 in right subtree
// Try deleting all nodes from 999-500 (worst case scenario)
for i = 999; i > 499; i-- {
root = AvlDelete(root, i)
}
b := GetBalanceFactor(root)
if b > 1 || b < -1 {
t.Fail()
}
}
func TestAvlGetHeight(t *testing.T) {
var root *AvlNode
root = &AvlNode{key: 2, left: nil, right: nil, height: 1}
root = AvlInsert(root, 10, nil, false)
root = AvlInsert(root, 20, nil, false)
if GetHeight(root) != 2 {
t.Fail()
}
AvlInsert(root, 30, nil, false)
if GetHeight(root) != 3 {
t.Fail()
}
}
func TestAvlRightRotate(t *testing.T) {
var root *AvlNode
root = &AvlNode{key: 10, left: nil, right: nil, height: 1}
root = AvlInsert(root, 9, nil, false)
root = AvlInsert(root, 8, nil, false)
if root.left.key != 8 || root.right.key != 10 || root.key != 9 {
t.Fail()
}
}
func TestAvlLeftRotate(t *testing.T) {
var root *AvlNode
root = &AvlNode{key: 10, left: nil, right: nil, height: 1}
root = AvlInsert(root, 11, nil, false)
root = AvlInsert(root, 12, nil, false)
if root.left.key != 10 || root.right.key != 12 || root.key != 11 {
t.Fail()
}
}
func TestAvlLeftRight(t *testing.T) {
var root *AvlNode
root = &AvlNode{key: 10, left: nil, right: nil, height: 1}
root = AvlInsert(root, 5, nil, false)
root = AvlInsert(root, 9, nil, false)
if root.left.key != 5 || root.right.key != 10 || root.key != 9 {
t.Fail()
}
}
func TestAvlRightLeft(t *testing.T) {
var root *AvlNode
root = &AvlNode{key: 10, left: nil, right: nil, height: 1}
root = AvlInsert(root, 15, nil, false)
root = AvlInsert(root, 13, nil, false)
if root.left.key != 10 || root.right.key != 15 || root.key != 13 {
t.Fail()
}
}
func TestAvlOverwrite(t *testing.T) {
var root *AvlNode
root = &AvlNode{key: 10, left: nil, right: nil, height: 1}
root = AvlInsert(root, 12, &DummyData{data: 10}, false)
root = AvlInsert(root, 15, &DummyData{data: 10}, false)
root = AvlInsert(root, 15, &DummyData{data: 12}, true)
nodes := AvlGetAll(root)
if len(nodes) != 3 || nodes[2].data.String() != "12" {
t.Fail()
}
}