@@ -13,11 +13,14 @@ public class RBTNode{
13
13
}
14
14
init ( rootData: Int ) {
15
15
self . data = rootData
16
- self . color = true //0is black 1 is red
16
+ self . color = true //0 is black 1 is red
17
17
self . left = nil
18
18
self . right = nil
19
19
self . parent = nil
20
20
}
21
+ deinit {
22
+ print ( " Node: \( self . data) is bein deinitialized " )
23
+ }
21
24
public func grandparent( ) -> RBTNode ? {
22
25
if ( self . parent === nil || self . parent. parent === nil ) {
23
26
return nil
@@ -37,48 +40,9 @@ public class RBTNode{
37
40
return self . parent!. left!
38
41
}
39
42
}
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
- }
43
+
44
+
45
+
82
46
}
83
47
public class RBTree {
84
48
@@ -189,16 +153,16 @@ private func insertCase3(inserted: RBTNode?){
189
153
}
190
154
insertCase4 ( inserted)
191
155
}
192
- //THIS ONE MAY BE BROKEN!?!?!?!?
156
+
193
157
private func insertCase4( var inserted: RBTNode ? ) {
194
158
if ( ( inserted! === inserted!. parent!. right) && ( inserted!. grandparent ( ) !. left === inserted!. parent!) ) {
195
159
print ( " case 4 " )
196
- rightRotate ( inserted!. parent)
160
+ leftRotate ( inserted!. parent)
197
161
inserted! = inserted!. left
198
162
}
199
163
else if ( ( inserted! === inserted!. parent!. left) && ( inserted!. grandparent ( ) !. right === inserted!. parent!) ) {
200
164
print ( " case 4 " )
201
- leftRotate ( inserted!. parent)
165
+ rightRotate ( inserted!. parent)
202
166
inserted! = inserted!. right
203
167
}
204
168
insertCase5 ( inserted)
@@ -280,26 +244,174 @@ private func insertCase5(inserted: RBTNode?){
280
244
return
281
245
}
282
246
}
247
+ public func replaceNode( n1: RBTNode , n2: RBTNode ) {
248
+ var temp = n1. data
249
+ var temp_color = n1. color
250
+ n1. data = n2. data
251
+ n1. color = n2. color
252
+ n2. data = temp
253
+ n2. color = temp_color
254
+ }
255
+ public func minimum( var node: RBTNode ) -> RBTNode {
256
+ while node. left !== nil {
257
+ node = node. left
258
+ }
259
+ return node
260
+ }
261
+ public func successor( var node: RBTNode ) -> RBTNode ? {
262
+ if node. right !== nil {
263
+ return minimum ( node. right)
264
+ }
265
+ var successor = node. parent
266
+ while successor !== nil && node === successor. right {
267
+ node = successor
268
+ successor = successor. parent
269
+ }
270
+ return successor
271
+ }
272
+ public func predecessor( var node: RBTNode ) -> RBTNode {
273
+ if node. left !== nil {
274
+ return minimum ( node. left)
275
+ }
276
+ var successor = node. parent
277
+ while successor !== nil && node === successor. left {
278
+ node = successor
279
+ successor = successor. parent
280
+ }
281
+ return successor
282
+ }
283
+ public func maximum( var rootNode: RBTNode ) -> RBTNode {
284
+ while rootNode. right !== nil {
285
+ rootNode = rootNode. right
286
+ }
287
+ return rootNode
288
+ }
289
+ private func deletionFixup( x: RBTNode ) { }
290
+
291
+ public func delete( x: Int ) {
292
+ let toDel = find ( x)
293
+ deleteNode ( toDel!)
294
+ }
295
+ private func deleteNode( var toDel: RBTNode ? ) {
296
+ print ( " DELETE CASE 0 " )
297
+ if ( toDel!. left == nil && toDel!. right == nil && toDel!. color == true ) {
298
+ print ( " IM IN HERE " )
299
+ //DEINIT NOT BEING CALLED AGAIN?
300
+ toDel = nil
301
+ return
302
+ }
303
+ if ( toDel!. left !== nil && toDel!. right !== nil ) {
304
+ var pred = maximum ( toDel!. left!)
305
+ toDel!. data = pred. data
306
+ toDel! = pred
307
+ }
308
+ var child : RBTNode ? = nil
283
309
310
+ if ( toDel!. right === nil ) {
311
+ child = toDel!. left
312
+ }
313
+ else {
314
+ child = toDel!. right
315
+ }
316
+
317
+ if ( toDel!. color == false ) {
318
+ toDel!. color = child!. color
319
+ deleteCase1 ( toDel!)
320
+ }
284
321
322
+ if ( child !== nil ) {
323
+ replaceNode ( toDel!, n2: child!)
285
324
325
+ if ( toDel!. parent === nil && child !== nil ) {
326
+ child!. color = false
327
+ }
328
+ }
329
+ print ( " This statement isnt actually deleting the object??? V " )
330
+ toDel = nil
331
+ }
332
+ private func deleteCase1( var toDel: RBTNode ? ) {
333
+ print ( " DELETE CASE 1 " )
334
+ if ( toDel? . parent === nil ) {
335
+ return
336
+ }
337
+ else {
338
+ deleteCase2 ( toDel)
339
+ }
340
+ }
341
+ private func deleteCase2( var toDel: RBTNode ? ) {
342
+ print ( " DELETE CASE 2 " )
343
+ var sibling = toDel!. sibling ( )
344
+ if ( sibling? . color == true ) {
345
+ toDel!. parent. color = true ;
346
+ sibling? . color = false
347
+ if ( toDel! === toDel!. parent. left) {
348
+ leftRotate ( toDel!. parent)
349
+ }
350
+ else {
351
+ rightRotate ( toDel!. parent)
352
+ }
353
+ deleteCase3 ( toDel!)
354
+ }
355
+ }
356
+ private func deleteCase3( var toDel: RBTNode ? ) {
357
+ print ( " DELETE CASE 3 " )
358
+ if ( toDel!. parent? . color == false && toDel!. sibling ( ) ? . color == false && toDel!. sibling ( ) ? . left!. color == false && toDel!. sibling ( ) ? . right!. color == false ) {
359
+ toDel!. sibling ( ) ? . color = true
360
+ toDel!. parent? . color = false
361
+ }
362
+ else {
363
+ deleteCase4 ( toDel)
364
+ }
365
+ }
366
+ private func deleteCase4( var toDel: RBTNode ? ) {
367
+ print ( " DELETE CASE 4 " )
368
+ if ( toDel!. parent? . color == true && toDel!. sibling ( ) ? . color == false && toDel!. sibling ( ) ? . left!. color == false && toDel!. sibling ( ) ? . right!. color == false ) {
369
+ toDel!. sibling ( ) ? . color = true
370
+ toDel!. parent. color = false
371
+ }
372
+ else {
373
+ deleteCase5 ( toDel)
374
+ }
375
+ }
376
+ private func deleteCase5( var toDel: RBTNode ? ) {
377
+ print ( " DELETE CASE 5 " )
378
+ if ( toDel! === toDel!. parent? . left && toDel!. sibling ( ) ? . color == false && toDel!. sibling ( ) ? . left. color == true && toDel!. sibling ( ) ? . right. color == false ) {
379
+ toDel!. sibling ( ) ? . color = true
380
+ toDel!. sibling ( ) ? . left? . color = false
381
+ rightRotate ( toDel!. sibling ( ) !)
382
+ }
383
+ else if ( toDel! === toDel!. parent? . right && toDel!. sibling ( ) ? . color == false && toDel!. sibling ( ) ? . left. color == false && toDel!. sibling ( ) ? . right. color == true ) {
384
+ toDel!. sibling ( ) ? . color = true
385
+ toDel!. sibling ( ) ? . right? . color = false
386
+ leftRotate ( toDel!. sibling ( ) !)
387
+ }
388
+ }
389
+ private func deleteCase6( var toDel: RBTNode ? ) {
390
+ print ( " DELETE CASE 6 " )
391
+ let color = toDel!. sibling ( ) ? . color
392
+ toDel!. parent? . color = color!
393
+ toDel!. parent? . color = false
394
+ if ( toDel! === toDel!. parent. left) {
395
+ toDel!. sibling ( ) ? . right? . color = false
396
+ leftRotate ( toDel!. parent!)
397
+ }
398
+ else {
399
+ toDel!. sibling ( ) ? . left? . color = false
400
+ rightRotate ( toDel!. parent!)
401
+ }
402
+ }
286
403
}
287
404
405
+
406
+
288
407
var tree = RBTree ( rootData: 8 )
289
- tree . insert ( 9 )
408
+
290
409
tree. insert ( 6 )
291
- tree. insert ( 5 )
292
- tree. insert ( 4 )
293
- tree. insert ( 40 )
294
- tree. insert ( 400 )
295
- tree. insert ( 430 )
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)
410
+ tree. delete ( 6 )
302
411
tree. inOrder ( )
412
+ print ( " _______ " )
413
+
414
+
303
415
304
416
//print("_________________________")
305
417
//let x = tree.root!
0 commit comments