-
Notifications
You must be signed in to change notification settings - Fork 21
/
rbtree.go
367 lines (336 loc) · 6.62 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
// Copyright 2020 The golang.design Initiative authors.
// All rights reserved. Use of this source code is governed
// by a MIT license that can be found in the LICENSE file.
package lockfree
import "fmt"
type color uint32
const (
red color = iota
black
)
type rbnode struct {
c color
left *rbnode
right *rbnode
parent *rbnode
k, v interface{}
}
func (n *rbnode) color() color {
if n == nil {
return black
}
return n.c
}
func (n *rbnode) grandparent() *rbnode {
return n.parent.parent
}
func (n *rbnode) uncle() *rbnode {
if n.parent == n.grandparent().left {
return n.grandparent().right
}
return n.grandparent().left
}
func (n *rbnode) sibling() *rbnode {
if n == n.parent.left {
return n.parent.right
}
return n.parent.left
}
func (n *rbnode) maximumNode() *rbnode {
for n.right != nil {
n = n.right
}
return n
}
// RBTree is a red-black tree
// TODO: FIXME: This implementation is not a non-blocking implementation.
type RBTree struct {
root *rbnode
len int
less Less
}
// NewRBTree creates a red-black tree
func NewRBTree(less Less) *RBTree {
return &RBTree{less: less}
}
// Len returns the size of the tree
func (t *RBTree) Len() int {
return t.len
}
// Put stores the value by given key
func (t *RBTree) Put(key, value interface{}) {
var insertedNode *rbnode
new := &rbnode{k: key, v: value, c: red}
if t.root != nil {
node := t.root
LOOP:
for {
switch {
case t.less(key, node.k):
if node.left == nil {
node.left = new
insertedNode = node.left
break LOOP
}
node = node.left
case t.less(node.k, key):
if node.right == nil {
node.right = new
insertedNode = node.right
break LOOP
}
node = node.right
default: // =
node.k = key
node.v = value
return
}
}
insertedNode.parent = node
} else {
t.root = new
insertedNode = t.root
}
t.insertCase1(insertedNode)
t.len++
}
func (t *RBTree) insertCase1(n *rbnode) {
if n.parent == nil {
n.c = black
return
}
t.insertCase2(n)
}
func (t *RBTree) insertCase2(n *rbnode) {
if n.parent.color() == black {
return
}
t.insertCase3(n)
}
func (t *RBTree) insertCase3(n *rbnode) {
if n.uncle().color() == red {
n.parent.c = black
n.uncle().c = black
n.grandparent().c = red
t.insertCase1(n.grandparent())
return
}
t.insertCase4(n)
}
func (t *RBTree) insertCase4(n *rbnode) {
if n == n.parent.right && n.parent == n.grandparent().left {
t.rotateLeft(n.parent)
n = n.left
} else if n == n.parent.left && n.parent == n.grandparent().right {
t.rotateRight(n.parent)
n = n.right
}
t.insertCase5(n)
}
func (t *RBTree) insertCase5(n *rbnode) {
n.parent.c = black
n.grandparent().c = red
if n == n.parent.left && n.parent == n.grandparent().left {
t.rotateRight(n.grandparent())
return
} else if n == n.parent.right && n.parent == n.grandparent().right {
t.rotateLeft(n.grandparent())
}
}
func (t *RBTree) replace(old, new *rbnode) {
if old.parent == nil {
t.root = new
} else {
if old == old.parent.left {
old.parent.left = new
} else {
old.parent.right = new
}
}
if new != nil {
new.parent = old.parent
}
}
func (t *RBTree) rotateLeft(n *rbnode) {
right := n.right
t.replace(n, right)
n.right = right.left
if right.left != nil {
right.left.parent = n
}
right.left = n
n.parent = right
}
func (t *RBTree) rotateRight(n *rbnode) {
left := n.left
t.replace(n, left)
n.left = left.right
if left.right != nil {
left.right.parent = n
}
left.right = n
n.parent = left
}
// Get returns the stored value by given key
func (t *RBTree) Get(key interface{}) interface{} {
n := t.find(key)
if n == nil {
return nil
}
return n.v
}
func (t *RBTree) find(key interface{}) *rbnode {
n := t.root
for n != nil {
switch {
case t.less(key, n.k):
n = n.left
case t.less(n.k, key):
n = n.right
default:
return n
}
}
return nil
}
// Del deletes the stored value by given key
func (t *RBTree) Del(key interface{}) {
var child *rbnode
n := t.find(key)
if n == nil {
return
}
if n.left != nil && n.right != nil {
pred := n.left.maximumNode()
n.k = pred.k
n.v = pred.v
n = pred
}
if n.left == nil || n.right == nil {
if n.right == nil {
child = n.left
} else {
child = n.right
}
if n.c == black {
n.c = child.color()
t.delCase1(n)
}
t.replace(n, child)
if n.parent == nil && child != nil {
child.c = black
}
}
t.len--
}
func (t *RBTree) delCase1(n *rbnode) {
if n.parent == nil {
return
}
t.delCase2(n)
}
func (t *RBTree) delCase2(n *rbnode) {
sibling := n.sibling()
if sibling.color() == red {
n.parent.c = red
sibling.c = black
if n == n.parent.left {
t.rotateLeft(n.parent)
} else {
t.rotateRight(n.parent)
}
}
t.delCase3(n)
}
func (t *RBTree) delCase3(n *rbnode) {
sibling := n.sibling()
if n.parent.color() == black &&
sibling.color() == black &&
sibling.left.color() == black &&
sibling.right.color() == black {
sibling.c = red
t.delCase1(n.parent)
return
}
t.delCase4(n)
}
func (t *RBTree) delCase4(n *rbnode) {
sibling := n.sibling()
if n.parent.color() == red &&
sibling.color() == black &&
sibling.left.color() == black &&
sibling.right.color() == black {
sibling.c = red
n.parent.c = black
return
}
t.delCase5(n)
}
func (t *RBTree) delCase5(n *rbnode) {
sibling := n.sibling()
if n == n.parent.left &&
sibling.color() == black &&
sibling.left.color() == red &&
sibling.right.color() == black {
sibling.c = red
sibling.left.c = black
t.rotateRight(sibling)
} else if n == n.parent.right &&
sibling.color() == black &&
sibling.right.color() == red &&
sibling.left.color() == black {
sibling.c = red
sibling.right.c = black
t.rotateLeft(sibling)
}
t.delCase6(n)
}
func (t *RBTree) delCase6(n *rbnode) {
sibling := n.sibling()
sibling.c = n.parent.color()
n.parent.c = black
if n == n.parent.left && sibling.right.color() == red {
sibling.right.c = black
t.rotateLeft(n.parent)
return
}
sibling.left.c = black
t.rotateRight(n.parent)
}
func (t *RBTree) String() string {
str := "RBTree\n"
if t.Len() != 0 {
t.root.output("", true, &str)
}
return str
}
func (n *rbnode) String() string {
return fmt.Sprintf("%v", n.k)
}
func (n *rbnode) output(prefix string, isTail bool, str *string) {
if n.right != nil {
newPrefix := prefix
if isTail {
newPrefix += "│ "
} else {
newPrefix += " "
}
n.right.output(newPrefix, false, str)
}
*str += prefix
if isTail {
*str += "└── "
} else {
*str += "┌── "
}
*str += n.String() + "\n"
if n.left != nil {
newPrefix := prefix
if isTail {
newPrefix += " "
} else {
newPrefix += "│ "
}
n.left.output(newPrefix, true, str)
}
}