-
Notifications
You must be signed in to change notification settings - Fork 0
/
rbtree.go
394 lines (356 loc) · 7.1 KB
/
rbtree.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
package rbtree
const (
// RED color
RED = true
// BLACK color with initial value since a nil node is black
BLACK = false
)
type (
// KeyType is the type with CompareTo behavior.
KeyType interface {
CompareTo(c interface{}) int
}
// KeyTypeInt is the int that implements the KeyType interface.
KeyTypeInt int
// Node is the red-black tree node
Node struct {
Key KeyType
Val interface{}
Color bool
Left *Node
Right *Node
Parent *Node
}
// RBT is the red-black tree
RBT struct {
root *Node
size uint64
}
)
// CompareTo implementation for type KeyTypeInt
func (k KeyTypeInt) CompareTo(c interface{}) int {
return int(k) - int(c.(KeyTypeInt))
}
// isRed returns whether a node is in red color, nil is black
func isRed(n *Node) bool {
if n == nil {
return BLACK
}
return n.Color
}
// NewRBT returns a red-black tree
func NewRBT() *RBT {
return &RBT{}
}
// Size returns the number of nodes that the red-black tree hold
func (t *RBT) Size() uint64 {
return t.size
}
// min find the minimum node in n's sub-tree
func min(n *Node) *Node {
for n.Left != nil {
n = n.Left
}
return n
}
// max find the maximum node in n's sub-tree
func max(n *Node) *Node {
for n.Right != nil {
n = n.Right
}
return n
}
// predecessor returns n's predecessor node
func (t *RBT) predecessor(n *Node) *Node {
if n == nil {
return nil
}
if n.Left != nil {
return max(n.Left)
}
p := n.Parent
lc := n
for p != nil && lc == p.Left {
lc = p
p = p.Parent
}
return p
}
// unused in this code
// successor returns n's successor node
func (t *RBT) successor(n *Node) *Node {
if n == nil {
return nil
}
if n.Right != nil {
return min(n.Right)
}
p := n.Parent
lc := n
for p != nil && lc == p.Right {
lc = p
p = p.Parent
}
return p
}
// LeftRotate left rotate the node n, acting on a red link
//
// p p
// | |
// n m
// / \ / \
// nl m ==> n mr
// / \ / \
// ml mr nl ml
//
func (t *RBT) leftRotate(n *Node) {
m := n.Right
n.Right = m.Left
if m.Left != nil {
m.Left.Parent = n
}
m.Parent = n.Parent
if n.Parent == nil {
t.root = m
} else if n == n.Parent.Left {
n.Parent.Left = m
} else {
n.Parent.Right = m
}
m.Left = n
n.Parent = m
// repaint color
m.Color = n.Color
n.Color = RED
}
// RightRotate right rotate the node n, acting on a red link
//
// p p
// | |
// n m
// / \ / \
// m nr ==> ml n
// / \ / \
// ml mr mr nr
//
func (t *RBT) rightRotate(n *Node) {
m := n.Left
n.Left = m.Right
if m.Right != nil {
m.Right.Parent = n
}
m.Parent = n.Parent
if n.Parent == nil {
t.root = m
} else if n == n.Parent.Left {
n.Parent.Left = m
} else {
n.Parent.Right = m
}
m.Right = n
n.Parent = m
m.Color = n.Color
n.Color = RED
}
func (t *RBT) flipColors(n *Node) {
n.Color = RED
n.Left.Color = BLACK
n.Right.Color = BLACK
}
// Search returns the node by the given key if it exists
func (t *RBT) Search(key KeyType) interface{} {
if n := t.search(t.root, key); n != nil {
return n.Val
}
return nil
}
func (t *RBT) search(r *Node, key KeyType) *Node {
for r != nil {
cmp := key.CompareTo(r.Key)
if cmp < 0 {
r = r.Left
} else if cmp > 0 {
r = r.Right
} else {
break
}
}
return r
}
// Insert inserts a key with associated data(val) to a place in the red-black
// tree by compare the keys, and if the key exists, update it with the new val.
func (t *RBT) Insert(key KeyType, val interface{}) {
if t.root == nil {
t.root = &Node{Key: key, Val: val}
t.size = 1
return
}
// find parent node to attach
root := t.root
var p *Node
for root != nil {
p = root
cmp := key.CompareTo(root.Key)
if cmp < 0 {
root = root.Left
} else if cmp > 0 {
root = root.Right
} else {
root.Val = val
return
}
}
newnode := &Node{Key: key,
Val: val,
Color: RED,
Parent: p,
}
cmp := key.CompareTo(p.Key)
if cmp < 0 {
p.Left = newnode
} else {
p.Right = newnode
}
t.insertFix(newnode)
t.size++
}
func (t *RBT) insertFix(n *Node) {
var u *Node // n's uncle node
for n.Parent != nil && isRed(n.Parent) {
if n.Parent == n.Parent.Parent.Left {
u = n.Parent.Parent.Right
if u != nil && isRed(u) {
n = n.Parent.Parent
t.flipColors(n)
} else {
if n == n.Parent.Right {
n = n.Parent
t.leftRotate(n)
}
t.rightRotate(n.Parent.Parent)
}
} else {
u = n.Parent.Parent.Left
if u != nil && isRed(u) {
n = n.Parent.Parent
t.flipColors(n)
} else {
if n == n.Parent.Left {
n = n.Parent
t.rightRotate(n)
}
t.leftRotate(n.Parent.Parent)
}
}
}
// root should be black node at the end
t.root.Color = BLACK
}
// Remove delete a node by the given key if it exits
func (t *RBT) Remove(key KeyType) {
d := t.search(t.root, key)
if d == nil {
return // not found
}
// find replacement node
// in our code, if finally the rpl hold a child node, the rpl must be a
// predecessor or successor
rpl := d
if rpl.Left != nil && rpl.Right != nil {
// rpl = t.successor(rpl)
rpl = t.predecessor(rpl)
} else {
if rpl.Left != nil {
rpl = rpl.Left
} else if rpl.Right != nil {
rpl = rpl.Right
}
}
// delete replacement node
if rpl != t.root {
if d != rpl { // d is not a leaf node
d.Key = rpl.Key
d.Val = rpl.Val
}
if rpl.Left != nil { // rpl is a predecessor
if !isRed(rpl) {
rpl.Left.Color = BLACK
}
rpl.Left.Parent = rpl.Parent
if rpl == rpl.Parent.Left {
rpl.Parent.Left = rpl.Left
} else {
rpl.Parent.Right = rpl.Left
}
// unlink rpl
rpl.Parent = nil
rpl.Left = nil
} else {
// rpl is a leaf node
// fix
if !isRed(rpl) {
t.removeFix(rpl)
}
// then delete
if rpl == rpl.Parent.Left {
rpl.Parent.Left = nil
} else {
rpl.Parent.Right = nil
}
// unlink rpl
rpl.Parent = nil
}
} else { // single node tree
t.root = nil
}
t.size--
}
// fixAfterRemove do fix if the deleted node is in black color
func (t *RBT) removeFix(n *Node) {
for n != t.root && !isRed(n) {
if n == n.Parent.Left {
rBro := n.Parent.Right
// find real brother node
if isRed(rBro) {
t.leftRotate(n.Parent)
rBro = n.Parent.Right
}
if !isRed(rBro.Left) && !isRed(rBro.Right) { // can't borrow
rBro.Color = RED
n = n.Parent
} else { // can borrow
// 3-node
if !isRed(rBro.Right) {
t.rightRotate(rBro)
rBro = n.Parent.Right
}
t.leftRotate(n.Parent)
// since we pull down n's parent to replace n and
// we borrow two n's bro's childern, we need repaint
n.Parent.Color = BLACK
rBro.Right.Color = BLACK
n = t.root
}
} else {
lBro := n.Parent.Left
if isRed(lBro) {
t.rightRotate(n.Parent)
lBro = n.Parent.Left
}
if !isRed(lBro.Left) && !isRed(lBro.Right) {
lBro.Color = RED
n = n.Parent
} else {
if !isRed(lBro.Left) {
t.leftRotate(lBro)
lBro = n.Parent.Left
}
t.rightRotate(n.Parent)
n.Parent.Color = BLACK
lBro.Left.Color = BLACK
n = t.root
}
}
}
n.Color = BLACK
}