@@ -5,13 +5,34 @@ private enum RBTColor {
55 case doubleBlack
66}
77
8- public class RBTNode < T: Comparable > {
8+ public class RBTNode < T: Comparable > : CustomStringConvertible {
99 fileprivate var color : RBTColor = . red
1010 public var value : T ! = nil
1111 public var right : RBTNode < T > !
1212 public var left : RBTNode < T > !
1313 public var parent : RBTNode < T > !
1414
15+ public var description : String {
16+ if self . value == nil {
17+ return " null "
18+ } else {
19+ var nodeValue : String
20+
21+ // If the value is encapsulated by parentheses it is red
22+ // If the value is encapsulated by pipes it is black
23+ // If the value is encapsulated by double pipes it is double black (This should not occur in a verified RBTree)
24+ if self . isRed {
25+ nodeValue = " ( \( self . value!) ) "
26+ } else if self . isBlack{
27+ nodeValue = " | \( self . value!) | "
28+ } else {
29+ nodeValue = " || \( self . value!) || "
30+ }
31+
32+ return " ( \( self . left. description) <- \( nodeValue) -> \( self . right. description) ) "
33+ }
34+ }
35+
1536 init ( tree: RBTree < T > ) {
1637 right = tree. nullLeaf
1738 left = tree. nullLeaf
@@ -61,10 +82,14 @@ public class RBTNode<T: Comparable> {
6182 }
6283}
6384
64- public class RBTree < T: Comparable > {
85+ public class RBTree < T: Comparable > : CustomStringConvertible {
6586 public var root : RBTNode < T >
6687 fileprivate let nullLeaf : RBTNode < T >
6788
89+ public var description : String {
90+ return root. description
91+ }
92+
6893 public init ( ) {
6994 nullLeaf = RBTNode < T > ( )
7095 nullLeaf. color = . black
0 commit comments