-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrb_tree_test.go
255 lines (225 loc) · 5.21 KB
/
rb_tree_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
package rb_tree
import (
"math/rand"
"testing"
)
func TestRbTreeBasicRight(t *testing.T) {
r := Create(intLess)
for i := 0; i < 10; i++ {
r.Add(i)
validateTree(r, t)
}
for i := 0; i < 10; i++ {
r.Remove(i)
validateTree(r, t)
}
}
func TestRbTreeBasicLeft(t *testing.T) {
r := Create(intLess)
for i := 0; i < 10; i++ {
r.Add(9 - i)
validateTree(r, t)
}
for i := 0; i < 10; i++ {
r.Remove(9 - i)
validateTree(r, t)
}
}
func TestRbTreeInverseRight(t *testing.T) {
r := Create(intLess)
for i := 0; i < 10; i++ {
r.Add(i)
validateTree(r, t)
}
for i := 0; i < 10; i++ {
r.Remove(9 - i)
validateTree(r, t)
}
}
func TestRbTreeInverseLeft(t *testing.T) {
r := Create(intLess)
for i := 0; i < 10; i++ {
r.Add(9 - i)
validateTree(r, t)
}
for i := 0; i < 10; i++ {
r.Remove(i)
validateTree(r, t)
}
}
func TestRbTreeInner(t *testing.T) {
r := Create(intLess)
// Adding nodes to the "middle" of the tree covers add case 4: n's parent
// is red but uncle is black, and n is the "inner" child of p.
for i := 0; i < 5; i++ {
r.Add(i)
validateTree(r, t)
r.Add(9 - i)
validateTree(r, t)
}
// Removing nodes the same way we added them covers delete case 5's first
// branch: n is the left child of p, s's left child is red and right child
// is black.
for i := 0; i < 5; i++ {
r.Remove(i)
validateTree(r, t)
r.Remove(9 - i)
validateTree(r, t)
}
}
func TestRbTreeInnerRemoveReverse(t *testing.T) {
r := Create(intLess)
// Adding nodes to the "middle" of the tree covers add case 4: n's parent
// is red but uncle is black, and n is the "inner" child of p.
for i := 0; i < 5; i++ {
r.Add(i)
validateTree(r, t)
r.Add(9 - i)
validateTree(r, t)
}
// Removing nodes the same way they were added, but starting with a higher
// node covers delete case 5's second branch: n is the right child of p,
// s's right child is red and left child is black.
for i := 0; i < 5; i++ {
r.Remove(9 - i)
validateTree(r, t)
r.Remove(i)
validateTree(r, t)
}
}
func TestRbTreeOuter(t *testing.T) {
r := Create(intLess)
for i := 0; i < 5; i++ {
r.Add(5 - i)
validateTree(r, t)
r.Add(5 + i)
validateTree(r, t)
}
// Removing nodes from the outside first covers the case where a black node
// is deleted and replaced by a red child.
// This circumvents the complex rebalancing on delete.
for i := 0; i < 5; i++ {
r.Remove(5 - i)
validateTree(r, t)
r.Remove(5 + i)
validateTree(r, t)
}
}
func TestRbTreeRemoveMissing(t *testing.T) {
r := Create(intLess)
r.Add(0)
for i := 0; i < 10; i++ {
r.Remove(i)
validateTree(r, t)
}
}
func TestRbTreeFind(t *testing.T) {
r := Create(intLess)
for i := 0; i < 10; i++ {
if r.Find(i) != nil {
t.Errorf("Found %v in empty tree", i)
}
validateTree(r, t)
}
for i := 0; i < 10; i++ {
r.Add(i)
validateTree(r, t)
}
for i := 0; i < 10; i++ {
if r.Find(i) != i {
t.Errorf("Did not find %v in tree", i)
}
validateTree(r, t)
}
for i := 10; i < 20; i++ {
if r.Find(i) != nil {
t.Errorf("Found non-existent element %v in tree", i)
}
validateTree(r, t)
}
}
func BenchmarkAdd(b *testing.B) {
r := Create(intLess)
for i := 0; i < b.N; i++ {
r.Add(rand.Int())
}
}
func BenchmarkRemove(b *testing.B) {
r := Create(intLess)
vals := make([]int, b.N)
for i := 0; i < b.N; i++ {
val := rand.Int()
r.Add(val)
vals[i] = val
}
b.ResetTimer()
for i := 0; i < b.N; i++ {
r.Remove(vals[i])
}
}
func intLess(a, b interface{}) bool {
return a.(int) < b.(int)
}
func validateTree(tree *RbTree, t *testing.T) {
if !tree.root.black() {
t.Errorf("Root %p is not black", tree.root)
}
validateNode(tree.root, t)
}
func validateNode(n *node, t *testing.T) int {
if n.parent != nil {
validateHasChild(n.parent, n, t)
}
if n.leaf {
if n.red() {
t.Errorf("Leaf node %p is not black", n)
}
return 1 // No children to check.
}
// Validate children, value ordering, count of black nodes along paths.
leftBlackCount := 0
rightBlackCount := 0
if n.left != nil {
validateHasParent(n.left, n, t)
if !n.left.leaf && n.less(n.val, n.left.val) {
t.Errorf("Node %p (%v) has value < left child %p (%v)",
n, n.val, n.left, n.left.val)
}
if n.red() && n.left.red() {
t.Errorf("Red node %p has red left child %p", n, n.left)
}
leftBlackCount = validateNode(n.left, t)
}
if n.right != nil {
validateHasParent(n.right, n, t)
if !n.right.leaf && n.less(n.right.val, n.val) {
t.Errorf("Node %p (%v) has value > right child %p (%v)",
n, n.val, n.right, n.right.val)
}
if n.red() && n.right.red() {
t.Errorf("Red node %p has red right child %p", n, n.right)
}
rightBlackCount = validateNode(n.right, t)
}
if leftBlackCount != rightBlackCount {
t.Errorf(
"Not all paths from %p have equal numbers of black nodes. "+
"Left (%p) = %v, right (%p) = %v",
n, n.left, leftBlackCount, n.right, rightBlackCount)
}
if n.black() {
return leftBlackCount + 1
} else {
return leftBlackCount
}
}
func validateHasChild(n, child *node, t *testing.T) {
if n.left != child && n.right != child {
t.Errorf("Node %p does not have child %p", n, child)
}
}
func validateHasParent(n, parent *node, t *testing.T) {
if n.parent != parent {
t.Errorf("Node %p does not have parent %p", n, parent)
}
}