-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathredBlack.go
522 lines (469 loc) · 10.8 KB
/
redBlack.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
package gotree
import (
"fmt"
"runtime"
)
// Color is the used to maintain the redblack tree balance.
type color bool
const (
red color = false // we rely on default for node initializations
black color = true
)
// Pretty output for errors, debugging, etc.
func (c color) String() string {
var s string
switch c {
case red:
s = "red"
case black:
s = "black"
}
return s
}
// A RBNode is the type manipulated within the tree. It holds the inserted elements.
// It is exposed whenever the tree traversal functions are used.
type RBNode struct {
Elem Interface
left, right *RBNode
color color
}
// A RBTree is our main type our redblack tree methods are defined on.
type RBTree struct {
height int // height from root to leaf
size int // Number of inserted elements
first, last *RBNode
iterNext func() Interface // initially nil
root *RBNode
}
// Height returns the max depth of any branch of the tree
func (t *RBTree) Height() int {
return t.height
}
// Size returns the number of elements currently inserted in the tree.
func (t *RBTree) Size() int {
return t.size
}
func (t *RBTree) Clear() {
t.root = nil
t.last = nil
t.first = nil
t.size = 0
t.height = 0
t.iterNext = nil
runtime.GC()
}
// Min returns the smallest inserted element if possible. If the smallest value is not
// found(empty tree), then Min returns a nil.
func (t *RBTree) Min() Interface {
if t.first != nil {
return t.first.Elem
}
return nil
}
// Max returns the largest inserted element if possible. If the largest value is not
// found(empty tree), then Max returns a nil.
func (t *RBTree) Max() Interface {
if t.last != nil {
return t.last.Elem
}
return nil
}
// Next is called when individual elements are wanted to be traversed over.
// Prior to a call to Next, a call to IterInit needs to be made to set up the necessary
// data to allow for traversal of the tree. Example:
//
// sum := 0
// for i, n := 0, tree.IterInit(InOrder); n != nil; i, n = i+1, tree.Next() {
// elem := n.(exInt) // (exInt is simple int type)
// sum += int(elem) + i
// }
// Note: If one was to break out of the loop prior to a complete traversal,
// and start another loop without calling IterInit, then the previously uncompleted iterator is continued again.
func (t *RBTree) Next() Interface {
if t.iterNext == nil {
return nil
}
return t.iterNext() // func set by call to IterInit(TravOrder)
}
// IterInit is the initializer which setups the tree for iterating over it's elements in
// a specific order. It setups the internal data, and then returns the first RBNode to be looked at. See Next for an example.
func (t *RBTree) IterInit(order TravOrder) Interface {
current := t.root
stack := []*RBNode{}
switch order {
case InOrder:
t.iterNext = func() (out Interface) {
for len(stack) > 0 || current != nil {
if current != nil {
stack = append(stack, current)
current = current.left
} else {
// pop
stackIndex := len(stack) - 1
current = stack[stackIndex]
out = current.Elem
stack = stack[0:stackIndex]
current = current.right
break
}
}
// last node, reset
if out == nil {
t.iterNext = nil
}
return out
}
case RevOrder:
t.iterNext = func() (out Interface) {
for len(stack) > 0 || current != nil {
if current != nil {
stack = append(stack, current)
current = current.right
} else {
// pop
stackIndex := len(stack) - 1
current = stack[stackIndex]
out = current.Elem
stack = stack[0:stackIndex]
current = current.left
break
}
}
// last node, reset
if out == nil {
t.iterNext = nil
}
return out
}
case PreOrder:
t.iterNext = func() (out Interface) {
for len(stack) > 0 || current != nil {
if current != nil {
out = current.Elem
stack = append(stack, current.right)
current = current.left
break
} else {
// pop
stackIndex := len(stack) - 1
current = stack[stackIndex]
stack = stack[0:stackIndex]
}
}
// last node, reset
if out == nil {
t.iterNext = nil
}
return out
}
case PostOrder:
stack = append(stack, current)
var prevRBNode *RBNode
t.iterNext = func() (out Interface) {
for len(stack) > 0 {
// peek
stackIndex := len(stack) - 1
current = stack[stackIndex]
if (prevRBNode == nil) ||
(prevRBNode.left == current) ||
(prevRBNode.right == current) {
if current.left != nil {
stack = append(stack, current.left)
} else if current.right != nil {
stack = append(stack, current.right)
}
} else if current.left == prevRBNode {
if current.right != nil {
stack = append(stack, current.right)
}
} else {
out = current.Elem
// pop, but no assignment
stackIndex := len(stack) - 1
stack = stack[0:stackIndex]
prevRBNode = current
break
}
prevRBNode = current
}
// last node, reset
if out == nil {
t.iterNext = nil
}
return out
}
default:
s := fmt.Sprintf("rbTree has not implemented %s for iteration.", order)
panic(s)
}
// return our first node
return t.iterNext()
}
// Map is a more performance orientated way to iterate over the elements of the tree.
// Given a TravOrder and a function which conforms to the IterFunc type:
//
// type IterFunc func(Interface)
//
// Map calls the function for each RBNode in the specified order.
func (t *RBTree) Map(order TravOrder, f IterFunc) {
n := t.root
switch order {
case InOrder:
var inorder func(node *RBNode)
inorder = func(node *RBNode) {
if node == nil {
return
}
inorder(node.left)
f(node.Elem)
inorder(node.right)
}
inorder(n)
case PreOrder:
var preorder func(node *RBNode)
preorder = func(node *RBNode) {
if node == nil {
return
}
f(node.Elem)
preorder(node.left)
preorder(node.right)
}
preorder(n)
case PostOrder:
var postorder func(node *RBNode)
postorder = func(node *RBNode) {
if node == nil {
return
}
postorder(node.left)
postorder(node.right)
f(node.Elem)
}
postorder(n)
default:
s := fmt.Sprintf("rbTree has not implemented %s.", order)
panic(s)
}
}
// Search returns the matching item if found, otherwise nil is returned.
func (t *RBTree) Search(item Interface) (found Interface) {
if item == nil {
return
}
x := t.root
for x != nil {
switch x.Elem.Compare(item) {
case EQ:
found = x.Elem
return
case GT:
x = x.left
case LT:
x = x.right
}
}
return
}
// Insert will either insert a new entry into the tree, and return nil. Or if there was a previous entry already inserted, then in addition to inserting the new item, the previously inserted item will be returned.
func (t *RBTree) Insert(item Interface) (old Interface) {
if item == nil {
return
}
if t.root == nil {
t.size++
t.root = &RBNode{Elem: item, left: nil, right: nil}
t.first = t.root
t.last = t.root
} else {
t.root, old = t.insert(t.root, item)
}
if t.root.color == red {
t.height++
}
t.root.color = black // maintain rb invariants
return
}
func (t *RBTree) insert(h *RBNode, item Interface) (root *RBNode, old Interface) {
if h == nil {
t.size++
// base case, insert do stuff on new node
n := &RBNode{Elem: item, left: nil, right: nil}
// set Min
switch t.first.Elem.Compare(item) {
case GT:
t.first = n
}
// set Max
switch t.last.Elem.Compare(item) {
case LT:
t.last = n
}
root = n
return
}
switch h.Elem.Compare(item) {
case GT:
h.left, old = t.insert(h.left, item)
case LT:
h.right, old = t.insert(h.right, item)
case EQ:
old = h.Elem
h.Elem = item
}
if h.right.isred() && !(h.left.isred()) {
h = h.rotateLeft()
}
if h.left.isred() && h.left.left.isred() {
h = h.rotateRight()
}
if h.left.isred() && h.right.isred() {
h.colorFlip()
}
root = h
return
}
// Remove looks for a matching entry, and if found, the item is removed from the tree and old is populated with the removed item. If the item is not matched in the tree, nil is returned.
func (t *RBTree) Remove(item Interface) (old Interface) {
if item == nil || t.root == nil {
return
}
t.root, old = t.remove(t.root, item)
if old != nil {
if t.root == nil {
t.first = nil
t.last = nil
} else {
// set Min
switch t.first.Elem.Compare(old) {
case EQ:
t.first = t.root.min()
}
// set Max
switch t.last.Elem.Compare(old) {
case EQ:
t.last = t.root.max()
}
}
} else {
}
if t.root != nil && t.root.color == red {
t.root.color = black // maintain rb invariants
t.height--
} else if t.root == nil {
t.height--
}
return
}
func (t *RBTree) remove(h *RBNode, item Interface) (root *RBNode, old Interface) {
switch h.Elem.Compare(item) {
case LT, EQ:
if h.left.isred() {
h = h.rotateRight()
}
if result := h.Elem.Compare(item); result == EQ && h.right == nil {
t.size--
old = h.Elem
h = nil
root = nil
return
}
if h.right != nil {
if !h.right.isred() && !(h.right.left.isred()) {
h = h.moveredRight()
}
if result := h.Elem.Compare(item); result == EQ {
old = h.Elem
t.size--
x := h.right.min()
h.Elem = x.Elem
h.right = h.right.removeMin()
} else {
h.right, old = t.remove(h.right, item)
}
}
case GT:
if h.left != nil {
if !h.left.isred() && !(h.left.left.isred()) {
h = h.moveredLeft()
}
h.left, old = t.remove(h.left, item)
}
}
root = h.fixUp()
return
}
// Left Leaning red black Tree functions and helpers to maintain public methods
func (h *RBNode) rotateLeft() (x *RBNode) {
x = h.right
h.right = x.left
x.left = h
x.color = h.color
h.color = red
return
}
func (h *RBNode) rotateRight() (x *RBNode) {
x = h.left
h.left = x.right
x.right = h
x.color = h.color
h.color = red
return
}
func (h *RBNode) isred() bool {
return h != nil && h.color == red
}
func (h *RBNode) moveredLeft() *RBNode {
h.colorFlip()
if h.right.left.isred() {
h.right = h.right.rotateRight()
h = h.rotateLeft()
h.colorFlip()
}
return h
}
func (h *RBNode) moveredRight() *RBNode {
h.colorFlip()
if h.left.left.isred() {
h = h.rotateRight()
h.colorFlip()
}
return h
}
func (h *RBNode) colorFlip() {
h.color = !h.color
h.left.color = !h.left.color
h.right.color = !h.right.color
}
func (h *RBNode) fixUp() *RBNode {
if h.right.isred() {
h = h.rotateLeft()
}
if h.left.isred() && h.left.left.isred() {
h = h.rotateRight()
}
if h.left.isred() && h.right.isred() {
h.colorFlip()
}
return h
}
func (h *RBNode) min() *RBNode {
for ; h.left != nil; h = h.left {
}
return h
}
func (h *RBNode) max() *RBNode {
for ; h.right != nil; h = h.right {
}
return h
}
func (h *RBNode) removeMin() *RBNode {
if h.left == nil {
return nil
}
if !h.left.isred() && !h.left.left.isred() {
h = h.moveredLeft()
}
h.left = h.left.removeMin()
return h.fixUp()
}