@@ -32,17 +32,16 @@ private enum RotationDirection {
32
32
33
33
// MARK: - RBNode
34
34
35
- public class RBTreeNode < Key : Comparable > : Equatable {
36
- public typealias RBNode = RBTreeNode < Key >
35
+ public class RBTreeNode < T : Comparable > : Equatable {
36
+ public typealias RBNode = RBTreeNode < T >
37
37
38
38
fileprivate var color : RBTreeColor = . black
39
- fileprivate var key : Key ?
40
- fileprivate var isNullLeaf = true
39
+ fileprivate var key : T ?
41
40
var leftChild : RBNode ?
42
41
var rightChild : RBNode ?
43
42
fileprivate weak var parent : RBNode ?
44
43
45
- public init ( key: Key ? , leftChild: RBNode ? , rightChild: RBNode ? , parent: RBNode ? ) {
44
+ public init ( key: T ? , leftChild: RBNode ? , rightChild: RBNode ? , parent: RBNode ? ) {
46
45
self . key = key
47
46
self . leftChild = leftChild
48
47
self . rightChild = rightChild
@@ -52,16 +51,14 @@ public class RBTreeNode<Key: Comparable>: Equatable {
52
51
self . rightChild? . parent = self
53
52
}
54
53
55
- public convenience init ( key: Key ? ) {
54
+ public convenience init ( key: T ? ) {
56
55
self . init ( key: key, leftChild: RBNode ( ) , rightChild: RBNode ( ) , parent: RBNode ( ) )
57
- self . isNullLeaf = false
58
56
}
59
57
60
58
// For initialising the nullLeaf
61
59
public convenience init ( ) {
62
60
self . init ( key: nil , leftChild: nil , rightChild: nil , parent: nil )
63
61
self . color = . black
64
- self . isNullLeaf = true
65
62
}
66
63
67
64
var isRoot : Bool {
@@ -72,7 +69,11 @@ public class RBTreeNode<Key: Comparable>: Equatable {
72
69
return rightChild == nil && leftChild == nil
73
70
}
74
71
75
- var isLeftCild : Bool {
72
+ var isNullLeaf : Bool {
73
+ return key == nil && isLeaf && color == . black
74
+ }
75
+
76
+ var isLeftChild : Bool {
76
77
return parent? . leftChild === self
77
78
}
78
79
@@ -85,7 +86,7 @@ public class RBTreeNode<Key: Comparable>: Equatable {
85
86
}
86
87
87
88
var sibling : RBNode ? {
88
- if isLeftCild {
89
+ if isLeftChild {
89
90
return parent? . rightChild
90
91
} else {
91
92
return parent? . leftChild
@@ -99,8 +100,8 @@ public class RBTreeNode<Key: Comparable>: Equatable {
99
100
100
101
// MARK: - RedBlackTree
101
102
102
- public class RedBlackTree < Key : Comparable > {
103
- public typealias RBNode = RBTreeNode < Key >
103
+ public class RedBlackTree < T : Comparable > {
104
+ public typealias RBNode = RBTreeNode < T >
104
105
105
106
fileprivate( set) var root : RBNode
106
107
fileprivate( set) var size = 0
@@ -180,14 +181,14 @@ extension RedBlackTree {
180
181
/*
181
182
* Returns the node with the given key |input| if existing
182
183
*/
183
- public func search( input: Key ) -> RBNode ? {
184
+ public func search( input: T ) -> RBNode ? {
184
185
return search ( key: input, node: root)
185
186
}
186
187
187
188
/*
188
189
* Returns the node with given |key| in subtree of |node|
189
190
*/
190
- fileprivate func search( key: Key , node: RBNode ? ) -> RBNode ? {
191
+ fileprivate func search( key: T , node: RBNode ? ) -> RBNode ? {
191
192
// If node nil -> key not found
192
193
guard let node = node else {
193
194
return nil
@@ -215,23 +216,21 @@ extension RedBlackTree {
215
216
/*
216
217
* Returns the minimum key value of the whole tree
217
218
*/
218
- public func minValue( ) -> Key ? {
219
- if let minNode = root. minimum ( ) {
220
- return minNode. key
221
- } else {
219
+ public func minValue( ) -> T ? {
220
+ guard let minNode = root. minimum ( ) else {
222
221
return nil
223
222
}
223
+ return minNode. key
224
224
}
225
225
226
226
/*
227
227
* Returns the maximum key value of the whole tree
228
228
*/
229
- public func maxValue( ) -> Key ? {
230
- if let maxNode = root. maximum ( ) {
231
- return maxNode. key
232
- } else {
233
- return nil
229
+ public func maxValue( ) -> T ? {
230
+ guard let maxNode = root. maximum ( ) else {
231
+ return nil
234
232
}
233
+ return maxNode. key
235
234
}
236
235
}
237
236
@@ -244,7 +243,7 @@ extension RedBlackTree {
244
243
* 2. Fix red-black properties
245
244
* Runntime: O(log n)
246
245
*/
247
- public func insert( key: Key ) {
246
+ public func insert( key: T ) {
248
247
if root. isNullLeaf {
249
248
root = RBNode ( key: key)
250
249
} else {
@@ -332,18 +331,18 @@ extension RedBlackTree {
332
331
else {
333
332
var zNew = z
334
333
// Case 2.a: z right child -> rotate
335
- if parentZ. isLeftCild && z. isRightChild {
334
+ if parentZ. isLeftChild && z. isRightChild {
336
335
zNew = parentZ
337
336
leftRotate ( node: zNew)
338
- } else if parentZ. isRightChild && z. isLeftCild {
337
+ } else if parentZ. isRightChild && z. isLeftChild {
339
338
zNew = parentZ
340
339
rightRotate ( node: zNew)
341
340
}
342
341
// Case 2.b: z left child -> recolor + rotate
343
342
zNew. parent? . color = . black
344
343
if let grandparentZnew = zNew. grandparent {
345
344
grandparentZnew. color = . red
346
- if z. isLeftCild {
345
+ if z. isLeftChild {
347
346
rightRotate ( node: grandparentZnew)
348
347
} else {
349
348
leftRotate ( node: grandparentZnew)
@@ -365,7 +364,7 @@ extension RedBlackTree {
365
364
* 2. Fix red-black properties
366
365
* Runntime: O(log n)
367
366
*/
368
- public func delete( key: Key ) {
367
+ public func delete( key: T ) {
369
368
if size == 1 {
370
369
root = nullLeaf
371
370
size -= 1
@@ -408,7 +407,7 @@ extension RedBlackTree {
408
407
if parentY. isNullLeaf {
409
408
root = nodeX
410
409
} else {
411
- if nodeY. isLeftCild {
410
+ if nodeY. isLeftChild {
412
411
parentY. leftChild = nodeX
413
412
} else {
414
413
parentY. rightChild = nodeX
@@ -450,7 +449,7 @@ extension RedBlackTree {
450
449
if let parentX = x. parent {
451
450
parentX. color = . red
452
451
// Rotation
453
- if x. isLeftCild {
452
+ if x. isLeftChild {
454
453
leftRotate ( node: parentX)
455
454
} else {
456
455
rightRotate ( node: parentX)
@@ -472,7 +471,7 @@ extension RedBlackTree {
472
471
// We have a valid red-black-tree
473
472
} else {
474
473
// Case 3: a. Sibling black with one black child to the right
475
- if x. isLeftCild && sibling. rightChild? . color == . black {
474
+ if x. isLeftChild && sibling. rightChild? . color == . black {
476
475
// Recolor
477
476
sibling. leftChild? . color = . black
478
477
sibling. color = . red
@@ -501,7 +500,7 @@ extension RedBlackTree {
501
500
sibling. color = parentX. color
502
501
parentX. color = . black
503
502
// a. x left and sibling with red right child
504
- if x. isLeftCild {
503
+ if x. isLeftChild {
505
504
sibling. rightChild? . color = . black
506
505
// Rotate
507
506
leftRotate ( node: parentX)
@@ -581,7 +580,7 @@ extension RedBlackTree {
581
580
if let node = nodeY {
582
581
root = node
583
582
}
584
- } else if x. isLeftCild {
583
+ } else if x. isLeftChild {
585
584
x. parent? . leftChild = nodeY
586
585
} else if x. isRightChild {
587
586
x. parent? . rightChild = nodeY
0 commit comments