11
22private enum RBTColor {
3- case Red
4- case Black
5- case DoubleBlack
3+ case red
4+ case black
5+ case doubleBlack
66}
77
88public class RBTNode < T: Comparable > {
9- fileprivate var color : RBTColor = . Red
9+ fileprivate var color : RBTColor = . red
1010 public var value : T ! = nil
1111 public var right : RBTNode < T > !
1212 public var left : RBTNode < T > !
@@ -49,15 +49,15 @@ public class RBTNode<T: Comparable> {
4949 }
5050
5151 fileprivate var isRed : Bool {
52- return color == . Red
52+ return color == . red
5353 }
5454
5555 fileprivate var isBlack : Bool {
56- return color == . Black
56+ return color == . black
5757 }
5858
5959 fileprivate var isDoubleBlack : Bool {
60- return color == . DoubleBlack
60+ return color == . doubleBlack
6161 }
6262}
6363
@@ -67,7 +67,7 @@ public class RBTree<T: Comparable> {
6767
6868 public init ( ) {
6969 nullLeaf = RBTNode < T > ( )
70- nullLeaf. color = . Black
70+ nullLeaf. color = . black
7171 root = nullLeaf
7272 }
7373
@@ -175,7 +175,7 @@ public class RBTree<T: Comparable> {
175175 // if node is root change color to black, else move on
176176 private func insertCase1( n: RBTNode < T > ) {
177177 if n === root {
178- n. color = . Black
178+ n. color = . black
179179 } else {
180180 insertCase2 ( n: n)
181181 }
@@ -192,9 +192,9 @@ public class RBTree<T: Comparable> {
192192 private func insertCase3( n: RBTNode < T > ) {
193193 if n. uncle. isRed { // node must have grandparent as children of root have a black parent
194194 // both parent and uncle are red, so grandparent must be black.
195- n. parent. color = . Black
196- n. uncle. color = . Black
197- n. grandparent. color = . Red
195+ n. parent. color = . black
196+ n. uncle. color = . black
197+ n. grandparent. color = . red
198198 // now both parent and uncle are black and grandparent is red.
199199 // we repeat for the grandparent
200200 insertCase1 ( n: n. grandparent)
@@ -226,8 +226,8 @@ public class RBTree<T: Comparable> {
226226 private func insertCase5( n: RBTNode < T > ) {
227227 // swap color of parent and grandparent
228228 // parent is red grandparent is black
229- n. parent. color = . Black
230- n. grandparent. color = . Red
229+ n. parent. color = . black
230+ n. grandparent. color = . red
231231
232232 if n. isLeftChild { // left left case
233233 rightRotate ( n: n. grandparent)
@@ -269,7 +269,7 @@ public class RBTree<T: Comparable> {
269269 }
270270
271271 if toDel. isRed || child. isRed {
272- child. color = . Black
272+ child. color = . black
273273
274274 if toDel. isLeftChild {
275275 toDel. parent. left = child
@@ -292,7 +292,7 @@ public class RBTree<T: Comparable> {
292292 if child !== nullLeaf {
293293 child. parent = toDel. parent
294294 }
295- child. color = . DoubleBlack
295+ child. color = . doubleBlack
296296
297297 while child. isDoubleBlack || ( child. parent !== nullLeaf && child. parent != nil ) {
298298 if sibling!. isBlack {
@@ -307,70 +307,70 @@ public class RBTree<T: Comparable> {
307307 }
308308
309309 if leftRedChild != nil || rightRedChild != nil { // at least one of sibling's children are red
310- child. color = . Black
310+ child. color = . black
311311 if sibling!. isLeftChild {
312312 if leftRedChild != nil { // left left case
313- sibling!. left. color = . Black
313+ sibling!. left. color = . black
314314 let tempColor = sibling!. parent. color
315315 sibling!. parent. color = sibling!. color
316316 sibling!. color = tempColor
317317 rightRotate ( n: sibling!. parent)
318318 } else { // left right case
319319 if sibling!. parent. isRed {
320- sibling!. parent. color = . Black
320+ sibling!. parent. color = . black
321321 } else {
322- sibling!. right. color = . Black
322+ sibling!. right. color = . black
323323 }
324324 leftRotate ( n: sibling!)
325325 rightRotate ( n: sibling!. grandparent)
326326 }
327327 } else {
328328 if rightRedChild != nil { // right right case
329- sibling!. right. color = . Black
329+ sibling!. right. color = . black
330330 let tempColor = sibling!. parent. color
331331 sibling!. parent. color = sibling!. color
332332 sibling!. color = tempColor
333333 leftRotate ( n: sibling!. parent)
334334 } else { // right left case
335335 if sibling!. parent. isRed {
336- sibling!. parent. color = . Black
336+ sibling!. parent. color = . black
337337 } else {
338- sibling!. left. color = . Black
338+ sibling!. left. color = . black
339339 }
340340 rightRotate ( n: sibling!)
341341 leftRotate ( n: sibling!. grandparent)
342342 }
343343 }
344344 break
345345 } else { // both sibling's children are black
346- child. color = . Black
347- sibling!. color = . Red
346+ child. color = . black
347+ sibling!. color = . red
348348 if sibling!. parent. isRed {
349- sibling!. parent. color = . Black
349+ sibling!. parent. color = . black
350350 break
351351 }
352- sibling!. parent. color = . DoubleBlack
352+ sibling!. parent. color = . doubleBlack
353353 child = sibling!. parent
354354 sibling = child. sibling
355355 }
356356 } else { // sibling is red
357- sibling!. color = . Black
357+ sibling!. color = . black
358358
359359 if sibling!. isLeftChild { // left case
360360 rightRotate ( n: sibling!. parent)
361361 sibling = sibling!. right. left
362- sibling!. parent. color = . Red
362+ sibling!. parent. color = . red
363363 } else { // right case
364364 leftRotate ( n: sibling!. parent)
365365 sibling = sibling!. left. right
366- sibling!. parent. color = . Red
366+ sibling!. parent. color = . red
367367 }
368368 }
369369
370370 // sibling check is here for when child is a nullLeaf and thus does not have a parent.
371371 // child is here as sibling can become nil when child is the root
372372 if ( sibling != nil && sibling!. parent === nullLeaf) || ( child !== nullLeaf && child. parent === nullLeaf) {
373- child. color = . Black
373+ child. color = . black
374374 }
375375 }
376376 }
0 commit comments