@@ -37,6 +37,48 @@ public class RBTNode{
37
37
return self . parent!. left!
38
38
}
39
39
}
40
+ public func replaceNode( n1: RBTNode , n2: RBTNode ) {
41
+ var temp = n1. data
42
+ var temp_color = n1. color
43
+ n1. data = n2. data
44
+ n1. color = n2. color
45
+ n2. data = temp
46
+ n2. color = temp_color
47
+ }
48
+ public func minimum( var node: RBTNode ) -> RBTNode {
49
+ while node. left !== nil {
50
+ node = node. left
51
+ }
52
+ return node
53
+ }
54
+ public func successor( var node: RBTNode ) -> RBTNode ? {
55
+ if node. right !== nil {
56
+ return minimum ( node. right)
57
+ }
58
+ var successor = node. parent
59
+ while successor !== nil && node === successor. right {
60
+ node = successor
61
+ successor = successor. parent
62
+ }
63
+ return successor
64
+ }
65
+ public func predecessor( var node: RBTNode ) -> RBTNode {
66
+ if node. left !== nil {
67
+ return minimum ( node. left)
68
+ }
69
+ var successor = node. parent
70
+ while successor !== nil && node === successor. left {
71
+ node = successor
72
+ successor = successor. parent
73
+ }
74
+ return successor
75
+ }
76
+ public func maximum( var rootNode: RBTNode ) -> RBTNode {
77
+ while rootNode. right !== nil {
78
+ rootNode = rootNode. right
79
+ }
80
+ return rootNode
81
+ }
40
82
}
41
83
public class RBTree {
42
84
@@ -118,7 +160,7 @@ public func insertFixup(value: Int){
118
160
print ( inserted!. data)
119
161
insertCase1 ( inserted)
120
162
}
121
- public func insertCase1( inserted: RBTNode ? ) {
163
+ private func insertCase1( inserted: RBTNode ? ) {
122
164
let myroot = self . root!
123
165
if myroot === inserted!{
124
166
self . root!. color = false
@@ -127,14 +169,14 @@ public func insertCase1(inserted: RBTNode?){
127
169
}
128
170
insertCase2 ( inserted!)
129
171
}
130
- public func insertCase2( inserted: RBTNode ? ) {
172
+ private func insertCase2( inserted: RBTNode ? ) {
131
173
if inserted!. parent!. color == false {
132
174
print ( " Insert case 2: parent is black so nothing to change " )
133
175
return
134
176
}
135
177
insertCase3 ( inserted!)
136
178
}
137
- public func insertCase3( inserted: RBTNode ? ) {
179
+ private func insertCase3( inserted: RBTNode ? ) {
138
180
if ( inserted!. parent!. sibling ( ) != nil && inserted!. parent!. sibling ( ) !. color == true ) {
139
181
print ( " insert case3: If both the parent P and the uncle U are red, then both of them can be repainted black and the grandparent G " )
140
182
inserted!. parent!. color = false
@@ -148,7 +190,7 @@ public func insertCase3(inserted: RBTNode?){
148
190
insertCase4 ( inserted)
149
191
}
150
192
//THIS ONE MAY BE BROKEN!?!?!?!?
151
- public func insertCase4( var inserted: RBTNode ? ) {
193
+ private func insertCase4( var inserted: RBTNode ? ) {
152
194
if ( ( inserted! === inserted!. parent!. right) && ( inserted!. grandparent ( ) !. left === inserted!. parent!) ) {
153
195
print ( " case 4 " )
154
196
rightRotate ( inserted!. parent)
@@ -161,7 +203,7 @@ public func insertCase4(var inserted: RBTNode?){
161
203
}
162
204
insertCase5 ( inserted)
163
205
}
164
- public func insertCase5( inserted: RBTNode ? ) {
206
+ private func insertCase5( inserted: RBTNode ? ) {
165
207
if ( ( inserted!. parent!. color == true && ( inserted!. parent!. sibling ( ) ? . color == false || inserted!. parent!. sibling ( ) == nil ) ) ) {
166
208
if ( inserted! === inserted!. parent!. left && inserted!. grandparent ( ) !. left === inserted!. parent!) {
167
209
print ( " insert case 5: The parent P is red but the uncle U is black/nil, the current node N is the left child of P, and P is the left child of its parent G " )
@@ -179,10 +221,8 @@ public func insertCase5(inserted: RBTNode?){
179
221
180
222
print ( " Its on the right " )
181
223
leftRotate ( inserted!. grandparent ( ) !)
182
-
183
224
}
184
225
}
185
-
186
226
print ( " weve reached the end boys " )
187
227
}
188
228
@@ -232,6 +272,17 @@ public func insertCase5(inserted: RBTNode?){
232
272
return nil
233
273
}
234
274
}
275
+
276
+ //DELETION HELPER FUNCTIONS
277
+ public func remove( value: Int ) {
278
+ let toRemove = find ( value)
279
+ if ( toRemove == nil ) {
280
+ return
281
+ }
282
+ }
283
+
284
+
285
+
235
286
}
236
287
237
288
var tree = RBTree ( rootData: 8 )
@@ -242,7 +293,12 @@ tree.insert(4)
242
293
tree. insert ( 40 )
243
294
tree. insert ( 400 )
244
295
tree. insert ( 430 )
245
-
296
+ var t = tree. root!
297
+ var min = t. minimum ( t)
298
+ print ( " MIN: \( min. data) " )
299
+ var x = tree. root!. left
300
+ tree. inOrder ( )
301
+ t. replaceNode ( t, n2: x)
246
302
tree. inOrder ( )
247
303
248
304
//print("_________________________")
@@ -264,7 +320,7 @@ tree.inOrder()
264
320
//how you access certain elements
265
321
//let root = tree.root!.left!
266
322
//if(root.parent === nil){
267
- // print("niggs ")
323
+ // print("nil ")
268
324
//}
269
325
//else{
270
326
// print(root.parent.data)
0 commit comments