forked from TheAlgorithms/Go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
rbtree.go
424 lines (369 loc) · 8.39 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
// Red-Black Tree is a kind of self-balancing binary search tree.
// Each node stores "color" ("red" or "black"), used to ensure that the tree remains balanced during insertions and deletions.
//
// For more details check out those links below here:
// Programiz article : https://www.programiz.com/dsa/red-black-tree
// Wikipedia article: https://en.wikipedia.org/wiki/Red_black_tree
// authors [guuzaa](https://github.com/guuzaa)
// see rbtree.go
package tree
import "github.com/TheAlgorithms/Go/constraints"
type Color byte
const (
Red Color = iota
Black
)
// Verify Interface Compliance
var _ Node[int] = &RBNode[int]{}
// RBNode represents a single node in the RB.
type RBNode[T constraints.Ordered] struct {
key T
parent *RBNode[T]
left *RBNode[T]
right *RBNode[T]
color Color
}
func (n *RBNode[T]) Key() T {
return n.key
}
func (n *RBNode[T]) Parent() Node[T] {
return n.parent
}
func (n *RBNode[T]) Left() Node[T] {
return n.left
}
func (n *RBNode[T]) Right() Node[T] {
return n.right
}
// RB represents a Red-Black tree.
// By default, _NIL = leaf, a dummy variable.
type RB[T constraints.Ordered] struct {
Root *RBNode[T]
_NIL *RBNode[T] // a sentinel value for nil
}
// NewRB creates a new Red-Black Tree
func NewRB[T constraints.Ordered]() *RB[T] {
leaf := &RBNode[T]{color: Black, left: nil, right: nil}
leaf.parent = leaf
return &RB[T]{
Root: leaf,
_NIL: leaf,
}
}
// Empty determines the Red-Black tree is empty
func (t *RB[T]) Empty() bool {
return t.Root == t._NIL
}
// Push a chain of Node's into the Red-Black Tree
func (t *RB[T]) Push(keys ...T) {
for _, key := range keys {
t.pushHelper(t.Root, key)
}
}
// Delete a node of Red-Black Tree
// Returns false if the node does not exist, otherwise returns true.
func (t *RB[T]) Delete(data T) bool {
return t.deleteHelper(t.Root, data)
}
// Get a Node from the Red-Black Tree
func (t *RB[T]) Get(key T) (Node[T], bool) {
return searchTreeHelper[T](t.Root, t._NIL, key)
}
// Has Determines the tree has the node of Key
func (t *RB[T]) Has(key T) bool {
_, ok := searchTreeHelper[T](t.Root, t._NIL, key)
return ok
}
// PreOrder Traverses the tree in the following order Root --> Left --> Right
func (t *RB[T]) PreOrder() []T {
traversal := make([]T, 0)
preOrderRecursive[T](t.Root, t._NIL, &traversal)
return traversal
}
// InOrder Traverses the tree in the following order Left --> Root --> Right
func (t *RB[T]) InOrder() []T {
return inOrderHelper[T](t.Root, t._NIL)
}
// PostOrder traverses the tree in the following order Left --> Right --> Root
func (t *RB[T]) PostOrder() []T {
traversal := make([]T, 0)
postOrderRecursive[T](t.Root, t._NIL, &traversal)
return traversal
}
// LevelOrder returns the level order traversal of the tree
func (t *RB[T]) LevelOrder() []T {
traversal := make([]T, 0)
levelOrderHelper[T](t.Root, t._NIL, &traversal)
return traversal
}
// AccessNodesByLayer accesses nodes layer by layer (2-D array), instead of printing the results as 1-D array.
func (t *RB[T]) AccessNodesByLayer() [][]T {
return accessNodeByLayerHelper[T](t.Root, t._NIL)
}
// Depth returns the calculated depth of a Red-Black tree
func (t *RB[T]) Depth() int {
return calculateDepth[T](t.Root, t._NIL, 0)
}
// Max returns the Max value of the tree
func (t *RB[T]) Max() (T, bool) {
ret := maximum[T](t.Root, t._NIL)
if ret == t._NIL {
var dft T
return dft, false
}
return ret.Key(), true
}
// Min returns the Min value of the tree
func (t *RB[T]) Min() (T, bool) {
ret := minimum[T](t.Root, t._NIL)
if ret == t._NIL {
var dft T
return dft, false
}
return ret.Key(), true
}
// Predecessor returns the Predecessor of the node of Key
// if there is no predecessor, return default value of type T and false
// otherwise return the Key of predecessor and true
func (t *RB[T]) Predecessor(key T) (T, bool) {
node, ok := searchTreeHelper[T](t.Root, t._NIL, key)
if !ok {
var dft T
return dft, ok
}
return predecessorHelper[T](node, t._NIL)
}
// Successor returns the Successor of the node of Key
// if there is no successor, return default value of type T and false
// otherwise return the Key of successor and true
func (t *RB[T]) Successor(key T) (T, bool) {
node, ok := searchTreeHelper[T](t.Root, t._NIL, key)
if !ok {
var dft T
return dft, ok
}
return successorHelper[T](node, t._NIL)
}
func (t *RB[T]) pushHelper(x *RBNode[T], key T) {
y := t._NIL
for x != t._NIL {
y = x
switch {
case key < x.Key():
x = x.left
case key > x.Key():
x = x.right
default:
return
}
}
node := &RBNode[T]{
key: key,
left: t._NIL,
right: t._NIL,
parent: y,
color: Red,
}
if y == t._NIL {
t.Root = node
} else if node.key < y.key {
y.left = node
} else {
y.right = node
}
if node.parent == t._NIL {
node.color = Black
return
}
if node.parent.parent == t._NIL {
return
}
t.pushFix(node)
}
func (t *RB[T]) leftRotate(x *RBNode[T]) {
y := x.right
x.right = y.left
if y.left != t._NIL {
y.left.parent = x
}
y.parent = x.parent
if x.parent == t._NIL {
t.Root = y
} else if x == x.parent.left {
x.parent.left = y
} else {
x.parent.right = y
}
y.left = x
x.parent = y
}
func (t *RB[T]) rightRotate(x *RBNode[T]) {
y := x.left
x.left = y.right
if y.right != t._NIL {
y.right.parent = x
}
y.parent = x.parent
if x.parent == t._NIL {
t.Root = y
} else if x == y.parent.right {
y.parent.right = y
} else {
y.parent.left = y
}
y.right = x
x.parent = y
}
func (t *RB[T]) pushFix(k *RBNode[T]) {
for k.parent.color == Red {
if k.parent == k.parent.parent.right {
u := k.parent.parent.left
if u.color == Red {
u.color = Black
k.parent.color = Black
k.parent.parent.color = Red
k = k.parent.parent
} else {
if k == k.parent.left {
k = k.parent
t.rightRotate(k)
}
k.parent.color = Black
k.parent.parent.color = Red
t.leftRotate(k.parent.parent)
}
} else {
u := k.parent.parent.right
if u.color == Red {
u.color = Black
k.parent.color = Black
k.parent.parent.color = Red
k = k.parent.parent
} else {
if k == k.parent.right {
k = k.parent
t.leftRotate(k)
}
k.parent.color = Black
k.parent.parent.color = Red
t.rightRotate(k.parent.parent)
}
}
if k == t.Root {
break
}
}
t.Root.color = Black
}
func (t *RB[T]) deleteHelper(node *RBNode[T], key T) bool {
z := t._NIL
for node != t._NIL {
switch {
case node.key == key:
z = node
fallthrough
case node.key <= key:
node = node.right
case node.key > key:
node = node.left
}
}
if z == t._NIL {
return false
}
var x *RBNode[T]
y := z
yOriginColor := y.color
if z.left == t._NIL {
x = z.right
t.transplant(z, z.right)
} else if z.right == t._NIL {
x = z.left
t.transplant(z, z.left)
} else {
y = minimum[T](z.right, t._NIL).(*RBNode[T])
yOriginColor = y.color
x = y.right
if y.parent == z {
x.parent = y
} else {
t.transplant(y, y.right)
y.right = z.right
y.right.parent = y
}
t.transplant(z, y)
y.left = z.left
y.left.parent = y
y.color = z.color
}
if yOriginColor == Black {
t.deleteFix(x)
}
return true
}
func (t *RB[T]) deleteFix(x *RBNode[T]) {
var s *RBNode[T]
for x != t.Root && x.color == Black {
if x == x.parent.left {
s = x.parent.right
if s.color == Red {
s.color = Black
x.parent.color = Red
t.leftRotate(x.parent)
s = x.parent.right
}
if s.left.color == Black && s.right.color == Black {
s.color = Red
x = x.parent
} else {
if s.right.color == Black {
s.left.color = Black
s.color = Red
t.rightRotate(s)
s = x.parent.right
}
s.color = x.parent.color
x.parent.color = Black
s.right.color = Black
t.leftRotate(x.parent)
x = t.Root
}
} else {
s = x.parent.left
if s.color == Red {
s.color = Black
x.parent.color = Red
t.rightRotate(x.parent)
s = x.parent.left
}
if s.right.color == Black && s.left.color == Black {
s.color = Red
x = x.parent
} else {
if s.left.color == Black {
s.right.color = Black
s.color = Red
t.leftRotate(s)
s = x.parent.left
}
s.color = x.parent.color
x.parent.color = Black
s.left.color = Black
t.rightRotate(x.parent)
x = t.Root
}
}
}
x.color = Black
}
func (t *RB[T]) transplant(u, v *RBNode[T]) {
switch {
case u.parent == t._NIL:
t.Root = v
case u == u.parent.left:
u.parent.left = v
default:
u.parent.right = v
}
v.parent = u.parent
}