-
Notifications
You must be signed in to change notification settings - Fork 5
/
llrb.go
582 lines (528 loc) · 12.5 KB
/
llrb.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
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
// Implements Left-Leaning Red Black trees.
// Structure is not thread safe.
package llrbtree
import (
"util/compare"
)
type (
LLRBMode byte
color bool //represents the color of a Node
)
const (
LLRB234 = LLRBMode(0)
LLRB23 = LLRBMode(1)
// Red as false give us the defined behaviour that new nodes are red. Although this
// is incorrect for the root node, that is resolved on the first insertion.
red color = false
black color = true
)
func (c color) String() string {
if c {
return "Black"
}
return "Red"
}
// A Node represents a node in the LLRB tree.
type node struct {
mode LLRBMode
color color
key compare.Comparable
value interface{}
left, right *node
}
// A Tree manages the root node of an LLRB tree. Public methods are exposed through this type.
type Tree struct {
mode LLRBMode
root *node // Root node of the tree.
count int // Number of elements stored.
}
func New(mode LLRBMode) *Tree {
return &Tree{mode: mode}
}
func (t *Tree) Len() int {
return t.count
}
func (t *Tree) Empty() bool {
return t.count == 0
}
func (t *Tree) Clear() {
t.root = nil
t.count = 0
}
// Get returns the first match of q in the Tree. If insertion without
// replacement is used, this is probably not what you want.
func (t *Tree) Get(key compare.Comparable) (compare.Comparable, interface{}) {
if t.root == nil {
return nil, nil
}
n := t.root.search(key)
if n == nil {
return nil, nil
}
return n.key, n.value
}
func (n *node) search(key compare.Comparable) *node {
for n != nil {
switch c := key.Compare(n.key); {
case c == 0:
return n
case c < 0:
n = n.left
default:
n = n.right
}
}
return n
}
func (t *Tree) Insert(key compare.Comparable, value interface{}) {
var d int
t.root, d = t.root.insert(key, value, t.mode)
t.count += d
t.root.color = black
}
func (n *node) insert(key compare.Comparable, value interface{}, mode LLRBMode) (root *node, d int) {
if n == nil {
return &node{
mode: mode,
key: key,
value: value,
}, 1
} else if n.key == nil {
n.key, n.value, n.mode = key, value, mode
return n, 1
}
if mode == LLRB234 {
if n.left.getColor() == red && n.right.getColor() == red {
n.flipColors()
}
}
switch c := key.Compare(n.key); {
case c == 0:
n.key, n.value, n.mode = key, value, mode
case c < 0:
n.left, d = n.left.insert(key, value, mode)
default:
n.right, d = n.right.insert(key, value, mode)
}
if n.right.getColor() == red && n.left.getColor() == black {
n = n.rotateLeft()
}
if n.left.getColor() == red && n.left.left.getColor() == red {
n = n.rotateRight()
}
if mode == LLRB23 {
if n.left.getColor() == red && n.right.getColor() == red {
n.flipColors()
}
}
root = n
return
}
func (t *Tree) DeleteMin() {
if t.root == nil {
return
}
var d int
t.root, d = t.root.deleteMin()
t.count += d
if t.root == nil {
return
}
t.root.color = black
}
func (n *node) deleteMin() (root *node, d int) {
if n.left == nil {
return nil, -1
}
if n.left.getColor() == black && n.left.left.getColor() == black {
n = n.moveRedLeft()
}
n.left, d = n.left.deleteMin()
root = n.fixUp()
return
}
func (t *Tree) DeleteMax() {
if t.root == nil {
return
}
var d int
t.root, d = t.root.deleteMax()
t.count += d
if t.root == nil {
return
}
t.root.color = black
}
func (n *node) deleteMax() (root *node, d int) {
if n.left != nil && n.left.getColor() == red {
n = n.rotateRight()
}
if n.right == nil {
return nil, -1
}
if n.right.getColor() == black && n.right.left.getColor() == black {
n = n.moveRedRight()
}
n.right, d = n.right.deleteMax()
root = n.fixUp()
return
}
func (t *Tree) Delete(key compare.Comparable) {
if t.root == nil {
return
}
var d int
t.root, d = t.root.delete(key)
t.count += d
if t.root == nil {
return
}
t.root.color = black
}
func (n *node) delete(key compare.Comparable) (root *node, d int) {
if key.Compare(n.key) < 0 {
if n.left != nil {
if n.left.getColor() == black && n.left.left.getColor() == black {
n = n.moveRedLeft()
}
n.left, d = n.left.delete(key)
}
} else {
if n.left.getColor() == red {
n = n.rotateRight()
}
if n.right == nil && key.Compare(n.key) == 0 {
return nil, -1
}
if n.right != nil {
if n.right.getColor() == black && n.right.left.getColor() == black {
n = n.moveRedRight()
}
if key.Compare(n.key) == 0 {
min := n.right.min()
n.key, n.value = min.key, min.value
n.right, d = n.right.deleteMin()
} else {
n.right, d = n.right.delete(key)
}
}
}
root = n.fixUp()
return
}
func (t *Tree) Min() (compare.Comparable, interface{}) {
if t.root == nil {
return nil, nil
}
min := t.root.min()
if min == nil {
return nil, nil
}
return min.key, min.value
}
func (n *node) min() *node {
for ; n.left != nil; n = n.left {
}
return n
}
func (t *Tree) Max() (compare.Comparable, interface{}) {
if t.root == nil {
return nil, nil
}
max := t.root.max()
if max == nil {
return nil, nil
}
return max.key, max.value
}
func (n *node) max() *node {
for ; n.right != nil; n = n.right {
}
return n
}
// Floor returns the greatest value equal to or less than the query key according to key.Compare().
func (t *Tree) Floor(key compare.Comparable) (compare.Comparable, interface{}) {
if t.root == nil {
return nil, nil
}
n := t.root.floor(key)
if n == nil {
return nil, nil
}
return n.key, n.value
}
func (n *node) floor(key compare.Comparable) *node {
if n == nil {
return nil
}
switch c := key.Compare(n.key); {
case c == 0:
return n
case c < 0:
return n.left.floor(key)
default:
if r := n.right.floor(key); r != nil {
return r
}
}
return n
}
// Ceil returns the smallest value equal to or greater than the query q according to q.Compare().
func (t *Tree) Ceil(key compare.Comparable) (compare.Comparable, interface{}) {
if t.root == nil {
return nil, nil
}
n := t.root.ceil(key)
if n == nil {
return nil, nil
}
return n.key, n.value
}
func (n *node) ceil(key compare.Comparable) *node {
if n == nil {
return nil
}
switch c := key.Compare(n.key); {
case c == 0:
return n
case c > 0:
return n.right.ceil(key)
default:
if l := n.left.ceil(key); l != nil {
return l
}
}
return n
}
func (t *Tree) Keys() []compare.Comparable {
keys := make([]compare.Comparable, 0, t.count)
t.Do(func(key compare.Comparable, value interface{}) bool {
keys = append(keys, key)
return false
})
return keys
}
func (t *Tree) Values() []interface{} {
values := make([]interface{}, 0, t.count)
t.Do(func(key compare.Comparable, value interface{}) bool {
values = append(values, value)
return false
})
return values
}
// An Operation is a function that operates on a Comparable. If done is returned true, the
// Operation is indicating that no further work needs to be done and so the Do function should
// traverse no further.
type Operation func(key compare.Comparable, value interface{}) (done bool)
// Do performs fn on all values stored in the tree. A boolean is returned indicating whether the
// Do traversal was interrupted by an Operation returning true. If fn alters stored values' sort
// relationships, future tree operation behaviors are undefined.
func (t *Tree) Do(fn Operation) bool {
if t.root == nil {
return false
}
return t.root.do(fn)
}
func (n *node) do(fn Operation) (done bool) {
if n.left != nil {
done = n.left.do(fn)
if done {
return
}
}
done = fn(n.key, n.value)
if done {
return
}
if n.right != nil {
done = n.right.do(fn)
}
return
}
// DoReverse performs fn on all values stored in the tree, but in reverse of sort order. A boolean
// is returned indicating whether the Do traversal was interrupted by an Operation returning true.
// If fn alters stored values' sort relationships, future tree operation behaviors are undefined.
func (t *Tree) DoReverse(fn Operation) bool {
if t.root == nil {
return false
}
return t.root.doReverse(fn)
}
func (n *node) doReverse(fn Operation) (done bool) {
if n.right != nil {
done = n.right.doReverse(fn)
if done {
return
}
}
done = fn(n.key, n.value)
if done {
return
}
if n.left != nil {
done = n.left.doReverse(fn)
}
return
}
// DoRange performs fn on all values stored in the tree over the interval [from, to) from left
// to right. If to is less than from DoRange will panic. A boolean is returned indicating whether
// the Do traversal was interrupted by an Operation returning true. If fn alters stored values'
// sort relationships future tree operation behaviors are undefined.
func (t *Tree) DoRange(fn Operation, from, to compare.Comparable) bool {
if t.root == nil {
return false
}
if from.Compare(to) > 0 {
panic("llrb: inverted range")
}
return t.root.doRange(fn, from, to)
}
func (n *node) doRange(fn Operation, lo, hi compare.Comparable) (done bool) {
lc, hc := lo.Compare(n.key), hi.Compare(n.key)
if lc <= 0 && n.left != nil {
done = n.left.doRange(fn, lo, hi)
if done {
return
}
}
if lc <= 0 && hc > 0 {
done = fn(n.key, n.value)
if done {
return
}
}
if hc > 0 && n.right != nil {
done = n.right.doRange(fn, lo, hi)
}
return
}
// DoRangeReverse performs fn on all values stored in the tree over the interval (to, from] from
// right to left. If from is less than to DoRange will panic. A boolean is returned indicating
// whether the Do traversal was interrupted by an Operation returning true. If fn alters stored
// values' sort relationships future tree operation behaviors are undefined.
func (t *Tree) DoRangeReverse(fn Operation, from, to compare.Comparable) bool {
if t.root == nil {
return false
}
if from.Compare(to) < 0 {
panic("llrb: inverted range")
}
return t.root.doRangeReverse(fn, from, to)
}
func (n *node) doRangeReverse(fn Operation, hi, lo compare.Comparable) (done bool) {
lc, hc := lo.Compare(n.key), hi.Compare(n.key)
if hc > 0 && n.right != nil {
done = n.right.doRangeReverse(fn, hi, lo)
if done {
return
}
}
if lc <= 0 && hc > 0 {
done = fn(n.key, n.value)
if done {
return
}
}
if lc <= 0 && n.left != nil {
done = n.left.doRangeReverse(fn, hi, lo)
}
return
}
// DoMatch performs fn on all values stored in the tree that match q according to Compare, with
// q.Compare() used to guide tree traversal, so DoMatching() will out perform Do() with a called
// conditional function if the condition is based on sort order, but can not be reliably used if
// the condition is independent of sort order. A boolean is returned indicating whether the Do
// traversal was interrupted by an Operation returning true. If fn alters stored values' sort
// relationships, future tree operation behaviors are undefined.
func (t *Tree) DoMatching(fn Operation, q compare.Comparable) bool {
if t.root == nil {
return false
}
return t.root.doMatch(fn, q)
}
func (n *node) doMatch(fn Operation, q compare.Comparable) (done bool) {
c := q.Compare(n.key)
if c <= 0 && n.left != nil {
done = n.left.doMatch(fn, q)
if done {
return
}
}
if c == 0 {
done = fn(n.key, n.value)
if done {
return
}
}
if c >= 0 && n.right != nil {
done = n.right.doMatch(fn, q)
}
return
}
func (n *node) getColor() color {
if n == nil {
return black
}
return n.color
}
// (a,c)b -rotL-> ((a,)b,)c
func (n *node) rotateLeft() (root *node) {
root = n.right
n.right = root.left
root.left = n
root.color = n.color
n.color = red
return
}
// (a,c)b -rotR-> (,(,c)b)a
func (n *node) rotateRight() (root *node) {
root = n.left
n.left = root.right
root.right = n
root.color = n.color
n.color = red
return
}
// (aR,cR)bB -flipC-> (aB,cB)bR | (aB,cB)bR -flipC-> (aR,cR)bB
func (n *node) flipColors() {
n.color = !n.color
n.left.color = !n.left.color
n.right.color = !n.right.color
}
// fixUp ensures that black link balance is correct, that red nodes lean left,
// and that 4 nodes are split in the case of BU23 and properly balanced in TD234.
func (n *node) fixUp() *node {
if n.right.getColor() == red {
if n.mode == LLRB234 && n.right.left.getColor() == red {
n.right = n.right.rotateRight()
}
n = n.rotateLeft()
}
if n.left.getColor() == red && n.left.left.getColor() == red {
n = n.rotateRight()
}
if n.mode == LLRB23 && n.left.getColor() == red && n.right.getColor() == red {
n.flipColors()
}
return n
}
func (n *node) moveRedLeft() *node {
n.flipColors()
if n.right.left.getColor() == red {
n.right = n.right.rotateRight()
n = n.rotateLeft()
n.flipColors()
if n.mode == LLRB234 && n.right.right.getColor() == red {
n.right = n.right.rotateLeft()
}
}
return n
}
func (n *node) moveRedRight() *node {
n.flipColors()
if n.left.left.getColor() == red {
n = n.rotateRight()
n.flipColors()
}
return n
}