@@ -55,13 +55,11 @@ public class RBTNode<T: Comparable>: CustomStringConvertible {
55
55
return parent. parent
56
56
}
57
57
58
- public var sibling : RBTNode < T > ! {
58
+ public var sibling : RBTNode < T > {
59
59
if isLeftChild {
60
60
return self . parent. right
61
- } else if isRightChild {
62
- return self . parent. left
63
61
} else {
64
- return nil
62
+ return self . parent . left
65
63
}
66
64
}
67
65
@@ -245,6 +243,8 @@ public class RBTree<T: Comparable>: CustomStringConvertible {
245
243
} else if n. parent. isRightChild && n. isLeftChild { // right left case
246
244
rightRotate ( n: n. parent)
247
245
insertCase5 ( n: n. right)
246
+ } else {
247
+ insertCase5 ( n: n)
248
248
}
249
249
}
250
250
@@ -320,81 +320,91 @@ public class RBTree<T: Comparable>: CustomStringConvertible {
320
320
child. color = . doubleBlack
321
321
322
322
while child. isDoubleBlack || ( child. parent !== nullLeaf && child. parent != nil ) {
323
- if sibling! . isBlack {
323
+ if sibling. isBlack {
324
324
325
325
var leftRedChild : RBTNode < T > ! = nil
326
- if sibling! . left. isRed {
327
- leftRedChild = sibling! . left
326
+ if sibling. left. isRed {
327
+ leftRedChild = sibling. left
328
328
}
329
329
var rightRedChild : RBTNode < T > ! = nil
330
- if sibling! . right. isRed {
331
- rightRedChild = sibling! . right
330
+ if sibling. right. isRed {
331
+ rightRedChild = sibling. right
332
332
}
333
333
334
334
if leftRedChild != nil || rightRedChild != nil { // at least one of sibling's children are red
335
335
child. color = . black
336
- if sibling! . isLeftChild {
336
+ if sibling. isLeftChild {
337
337
if leftRedChild != nil { // left left case
338
- sibling! . left. color = . black
339
- let tempColor = sibling! . parent. color
340
- sibling! . parent. color = sibling! . color
341
- sibling! . color = tempColor
342
- rightRotate ( n: sibling! . parent)
338
+ sibling. left. color = . black
339
+ let tempColor = sibling. parent. color
340
+ sibling. parent. color = sibling. color
341
+ sibling. color = tempColor
342
+ rightRotate ( n: sibling. parent)
343
343
} else { // left right case
344
- if sibling! . parent. isRed {
345
- sibling! . parent. color = . black
344
+ if sibling. parent. isRed {
345
+ sibling. parent. color = . black
346
346
} else {
347
- sibling! . right. color = . black
347
+ sibling. right. color = . black
348
348
}
349
- leftRotate ( n: sibling! )
350
- rightRotate ( n: sibling! . grandparent)
349
+ leftRotate ( n: sibling)
350
+ rightRotate ( n: sibling. grandparent)
351
351
}
352
352
} else {
353
353
if rightRedChild != nil { // right right case
354
- sibling! . right. color = . black
355
- let tempColor = sibling! . parent. color
356
- sibling! . parent. color = sibling! . color
357
- sibling! . color = tempColor
358
- leftRotate ( n: sibling! . parent)
354
+ sibling. right. color = . black
355
+ let tempColor = sibling. parent. color
356
+ sibling. parent. color = sibling. color
357
+ sibling. color = tempColor
358
+ leftRotate ( n: sibling. parent)
359
359
} else { // right left case
360
- if sibling! . parent. isRed {
361
- sibling! . parent. color = . black
360
+ if sibling. parent. isRed {
361
+ sibling. parent. color = . black
362
362
} else {
363
- sibling! . left. color = . black
363
+ sibling. left. color = . black
364
364
}
365
- rightRotate ( n: sibling! )
366
- leftRotate ( n: sibling! . grandparent)
365
+ rightRotate ( n: sibling)
366
+ leftRotate ( n: sibling. grandparent)
367
367
}
368
368
}
369
369
break
370
370
} else { // both sibling's children are black
371
371
child. color = . black
372
- sibling! . color = . red
373
- if sibling! . parent. isRed {
374
- sibling! . parent. color = . black
372
+ sibling. color = . red
373
+ if sibling. parent. isRed {
374
+ sibling. parent. color = . black
375
375
break
376
376
}
377
- sibling!. parent. color = . doubleBlack
378
- child = sibling!. parent
377
+ /*
378
+ sibling.parent.color = .doubleBlack
379
+ child = sibling.parent
379
380
sibling = child.sibling
381
+ */
382
+ if sibling. parent. parent === nullLeaf { // parent of child is root
383
+ break
384
+ } else {
385
+ sibling. parent. color = . doubleBlack
386
+ child = sibling. parent
387
+ sibling = child. sibling // can become nill if child is root as parent is nullLeaf
388
+ }
389
+ //---------------
380
390
}
381
391
} else { // sibling is red
382
- sibling! . color = . black
392
+ sibling. color = . black
383
393
384
- if sibling! . isLeftChild { // left case
385
- rightRotate ( n: sibling! . parent)
386
- sibling = sibling! . right. left
387
- sibling! . parent. color = . red
394
+ if sibling. isLeftChild { // left case
395
+ rightRotate ( n: sibling. parent)
396
+ sibling = sibling. right. left
397
+ sibling. parent. color = . red
388
398
} else { // right case
389
- leftRotate ( n: sibling! . parent)
390
- sibling = sibling! . left. right
391
- sibling! . parent. color = . red
399
+ leftRotate ( n: sibling. parent)
400
+ sibling = sibling. left. right
401
+ sibling. parent. color = . red
392
402
}
393
403
}
394
404
395
405
// sibling check is here for when child is a nullLeaf and thus does not have a parent.
396
406
// child is here as sibling can become nil when child is the root
397
- if ( sibling != nil && sibling! . parent === nullLeaf) || ( child !== nullLeaf && child. parent === nullLeaf) {
407
+ if ( sibling. parent === nullLeaf) || ( child !== nullLeaf && child. parent === nullLeaf) {
398
408
child. color = . black
399
409
}
400
410
}
0 commit comments