-
-
Notifications
You must be signed in to change notification settings - Fork 283
/
rbtree.go
475 lines (399 loc) · 9.54 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
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
package types
import (
"fmt"
"github.com/c9s/bbgo/pkg/fixedpoint"
)
type RBTree struct {
Root *RBNode
size int
}
func NewRBTree() *RBTree {
var root = NewNil()
root.parent = NewNil()
return &RBTree{
Root: root,
}
}
func (tree *RBTree) Delete(key fixedpoint.Value) bool {
var deleting = tree.Search(key)
if deleting == nil {
return false
}
// y = the node to be deleted
// x (the child of the deleted node)
var x, y *RBNode
// fmt.Printf("neel = %p %+v\n", neel, neel)
// fmt.Printf("deleting = %+v\n", deleting)
// the deleting node has only one child, it's easy,
// we just connect the child the parent of the deleting node
if deleting.left.isNil() || deleting.right.isNil() {
y = deleting
// fmt.Printf("y = deleting = %+v\n", y)
} else {
// if both children are not NIL (neel), we need to find the successor
// and copy the successor to the memory location of the deleting node.
// since it's successor, it always has no child connecting to it.
y = tree.Successor(deleting)
// fmt.Printf("y = successor = %+v\n", y)
}
// y.left or y.right could be neel
if y.left.isNil() {
x = y.right
} else {
x = y.left
}
// fmt.Printf("x = %+v\n", y)
x.parent = y.parent
if y.parent.isNil() {
tree.Root = x
} else if y == y.parent.left {
y.parent.left = x
} else {
y.parent.right = x
}
// copy the data from the successor to the memory location of the deleting node
if y != deleting {
deleting.key = y.key
deleting.value = y.value
}
if y.color == Black {
tree.DeleteFixup(x)
}
tree.size--
return true
}
func (tree *RBTree) DeleteFixup(current *RBNode) {
for current != tree.Root && current.color == Black {
if current == current.parent.left {
sibling := current.parent.right
if sibling.color == Red {
sibling.color = Black
current.parent.color = Red
tree.RotateLeft(current.parent)
sibling = current.parent.right
}
// if both are black nodes
if sibling.left.color == Black && sibling.right.color == Black {
sibling.color = Red
current = current.parent
} else {
// only one of the child is black
if sibling.right.color == Black {
sibling.left.color = Black
sibling.color = Red
tree.RotateRight(sibling)
sibling = current.parent.right
}
sibling.color = current.parent.color
current.parent.color = Black
sibling.right.color = Black
tree.RotateLeft(current.parent)
current = tree.Root
}
} else { // if current is right child
sibling := current.parent.left
if sibling.color == Red {
sibling.color = Black
current.parent.color = Red
tree.RotateRight(current.parent)
sibling = current.parent.left
}
if sibling.left.color == Black && sibling.right.color == Black {
sibling.color = Red
current = current.parent
} else { // if only one of child is Black
// the left child of sibling is black, and right child is red
if sibling.left.color == Black {
sibling.right.color = Black
sibling.color = Red
tree.RotateLeft(sibling)
sibling = current.parent.left
}
sibling.color = current.parent.color
current.parent.color = Black
sibling.left.color = Black
tree.RotateRight(current.parent)
current = tree.Root
}
}
}
current.color = Black
}
func (tree *RBTree) Upsert(key, val fixedpoint.Value) {
var y = NewNil()
var x = tree.Root
var node = &RBNode{
key: key,
value: val,
color: Red,
left: NewNil(),
right: NewNil(),
parent: NewNil(),
}
for !x.isNil() {
y = x
if node.key == x.key {
// found node, skip insert and fix
x.value = val
return
} else if node.key.Compare(x.key) < 0 {
x = x.left
} else {
x = x.right
}
}
node.parent = y
if y.isNil() {
tree.Root = node
} else if node.key.Compare(y.key) < 0 {
y.left = node
} else {
y.right = node
}
tree.InsertFixup(node)
}
func (tree *RBTree) Insert(key, val fixedpoint.Value) {
var y = NewNil()
var x = tree.Root
var node = &RBNode{
key: key,
value: val,
color: Red,
left: NewNil(),
right: NewNil(),
parent: NewNil(),
}
for !x.isNil() {
y = x
if node.key.Compare(x.key) < 0 {
x = x.left
} else {
x = x.right
}
}
node.parent = y
if y.isNil() {
tree.Root = node
} else if node.key.Compare(y.key) < 0 {
y.left = node
} else {
y.right = node
}
tree.size++
tree.InsertFixup(node)
}
func (tree *RBTree) Search(key fixedpoint.Value) *RBNode {
var current = tree.Root
for !current.isNil() && key != current.key {
if key.Compare(current.key) < 0 {
current = current.left
} else {
current = current.right
}
}
if current.isNil() {
return nil
}
return current
}
func (tree *RBTree) Size() int {
return tree.size
}
func (tree *RBTree) InsertFixup(current *RBNode) {
// A red node can't have a red parent, we need to fix it up
for current.parent.color == Red {
if current.parent == current.parent.parent.left {
uncle := current.parent.parent.right
if uncle.color == Red {
current.parent.color = Black
uncle.color = Black
current.parent.parent.color = Red
current = current.parent.parent
} else { // if uncle is black
if current == current.parent.right {
current = current.parent
tree.RotateLeft(current)
}
current.parent.color = Black
current.parent.parent.color = Red
tree.RotateRight(current.parent.parent)
}
} else {
uncle := current.parent.parent.left
if uncle.color == Red {
current.parent.color = Black
uncle.color = Black
current.parent.parent.color = Red
current = current.parent.parent
} else {
if current == current.parent.left {
current = current.parent
tree.RotateRight(current)
}
current.parent.color = Black
current.parent.parent.color = Red
tree.RotateLeft(current.parent.parent)
}
}
}
// ensure that root is black
tree.Root.color = Black
}
// RotateLeft
// x is the axes of rotation, y is the node that will be replace x's position.
// we need to:
// 1. move y's left child to the x's right child
// 2. change y's parent to x's parent
// 3. change x's parent to y
func (tree *RBTree) RotateLeft(x *RBNode) {
var y = x.right
x.right = y.left
if !y.left.isNil() {
y.left.parent = x
}
y.parent = x.parent
if x.parent.isNil() {
tree.Root = y
} else if x == x.parent.left {
x.parent.left = y
} else {
x.parent.right = y
}
y.left = x
x.parent = y
}
func (tree *RBTree) RotateRight(y *RBNode) {
x := y.left
y.left = x.right
if !x.right.isNil() {
if x.right == nil {
panic(fmt.Errorf("x.right is nil: node = %+v, left = %+v, right = %+v, parent = %+v", x, x.left, x.right, x.parent))
}
x.right.parent = y
}
x.parent = y.parent
if y.parent.isNil() {
tree.Root = x
} else if y == y.parent.left {
y.parent.left = x
} else {
y.parent.right = x
}
x.right = y
y.parent = x
}
func (tree *RBTree) Rightmost() *RBNode {
return tree.RightmostOf(tree.Root)
}
func (tree *RBTree) RightmostOf(current *RBNode) *RBNode {
if current.isNil() || current == nil {
return nil
}
for !current.right.isNil() {
current = current.right
}
return current
}
func (tree *RBTree) Leftmost() *RBNode {
return tree.LeftmostOf(tree.Root)
}
func (tree *RBTree) LeftmostOf(current *RBNode) *RBNode {
if current.isNil() || current == nil {
return nil
}
for !current.left.isNil() {
current = current.left
}
return current
}
func (tree *RBTree) Successor(current *RBNode) *RBNode {
if !current.right.isNil() {
return tree.LeftmostOf(current.right)
}
var newNode = current.parent
for !newNode.isNil() && current == newNode.right {
current = newNode
newNode = newNode.parent
}
return newNode
}
func (tree *RBTree) Preorder(cb func(n *RBNode)) {
tree.PreorderOf(tree.Root, cb)
}
func (tree *RBTree) PreorderOf(current *RBNode, cb func(n *RBNode)) {
if !current.isNil() && current != nil {
cb(current)
tree.PreorderOf(current.left, cb)
tree.PreorderOf(current.right, cb)
}
}
// Inorder traverses the tree in ascending order
func (tree *RBTree) Inorder(cb func(n *RBNode) bool) {
tree.InorderOf(tree.Root, cb)
}
func (tree *RBTree) InorderOf(current *RBNode, cb func(n *RBNode) bool) {
if !current.isNil() && current != nil {
tree.InorderOf(current.left, cb)
if !cb(current) {
return
}
tree.InorderOf(current.right, cb)
}
}
// InorderReverse traverses the tree in descending order
func (tree *RBTree) InorderReverse(cb func(n *RBNode) bool) {
tree.InorderReverseOf(tree.Root, cb)
}
func (tree *RBTree) InorderReverseOf(current *RBNode, cb func(n *RBNode) bool) {
if !current.isNil() && current != nil {
tree.InorderReverseOf(current.right, cb)
if !cb(current) {
return
}
tree.InorderReverseOf(current.left, cb)
}
}
func (tree *RBTree) Postorder(cb func(n *RBNode) bool) {
tree.PostorderOf(tree.Root, cb)
}
func (tree *RBTree) PostorderOf(current *RBNode, cb func(n *RBNode) bool) {
if !current.isNil() && current != nil {
tree.PostorderOf(current.left, cb)
tree.PostorderOf(current.right, cb)
if !cb(current) {
return
}
}
}
func (tree *RBTree) CopyInorderReverse(limit int) *RBTree {
cnt := 0
newTree := NewRBTree()
tree.InorderReverse(func(n *RBNode) bool {
if cnt >= limit {
return false
}
newTree.Insert(n.key, n.value)
cnt++
return true
})
return newTree
}
func (tree *RBTree) CopyInorder(limit int) *RBTree {
cnt := 0
newTree := NewRBTree()
tree.Inorder(func(n *RBNode) bool {
if limit > 0 && cnt >= limit {
return false
}
newTree.Insert(n.key, n.value)
cnt++
return true
})
return newTree
}
func (tree *RBTree) Print() {
tree.Inorder(func(n *RBNode) bool {
fmt.Printf("%v -> %v\n", n.key, n.value)
return true
})
}