@@ -5,13 +5,34 @@ private enum RBTColor {
5
5
case doubleBlack
6
6
}
7
7
8
- public class RBTNode < T: Comparable > {
8
+ public class RBTNode < T: Comparable > : CustomStringConvertible {
9
9
fileprivate var color : RBTColor = . red
10
10
public var value : T ! = nil
11
11
public var right : RBTNode < T > !
12
12
public var left : RBTNode < T > !
13
13
public var parent : RBTNode < T > !
14
14
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
+
15
36
init ( tree: RBTree < T > ) {
16
37
right = tree. nullLeaf
17
38
left = tree. nullLeaf
@@ -61,10 +82,14 @@ public class RBTNode<T: Comparable> {
61
82
}
62
83
}
63
84
64
- public class RBTree < T: Comparable > {
85
+ public class RBTree < T: Comparable > : CustomStringConvertible {
65
86
public var root : RBTNode < T >
66
87
fileprivate let nullLeaf : RBTNode < T >
67
88
89
+ public var description : String {
90
+ return root. description
91
+ }
92
+
68
93
public init ( ) {
69
94
nullLeaf = RBTNode < T > ( )
70
95
nullLeaf. color = . black
0 commit comments