1
-
2
1
public class RBTNode{
3
2
private(set) public var color: Bool
4
3
private(set) public var left: RBTNode!
@@ -14,17 +13,37 @@ public class RBTNode{
14
13
}
15
14
init(rootData: Int){
16
15
self.data = rootData
17
- self.color = true
16
+ self.color = true //0is black 1 is red
18
17
self.left = nil
19
18
self.right = nil
20
19
self.parent = nil
21
20
}
21
+ public func grandparent()->RBTNode?{
22
+ if(self.parent === nil || self.parent.parent === nil){
23
+ return nil
24
+ }
25
+ else{
26
+ return self.parent.parent
27
+ }
28
+ }
29
+ public func sibling()->RBTNode?{
30
+ if(self.parent === nil || self.parent.right === nil || self.parent.left === nil){
31
+ return nil
32
+ }
33
+ if(self === self.parent!.left!){
34
+ return self.parent!.right!
35
+ }
36
+ else{
37
+ return self.parent!.left!
38
+ }
39
+ }
22
40
}
23
41
public class RBTree {
24
42
25
43
private(set) public var root: RBTNode?
26
44
init(rootData: Int) {
27
45
root = RBTNode(rootData: rootData)
46
+ root!.color = false
28
47
}
29
48
init(){
30
49
root = nil
@@ -47,7 +66,6 @@ public class RBTree {
47
66
}
48
67
private func inOrder(root: RBTNode?){
49
68
if(root == nil){
50
- print("nil")
51
69
return
52
70
}
53
71
inOrder(root!.left)
@@ -74,7 +92,7 @@ public class RBTree {
74
92
}
75
93
private func rightRotate(x: RBTNode) {
76
94
let newRoot = x.left
77
- x.left = newRoot.right
95
+ x.left = newRoot.right
78
96
if newRoot.right !== nil {
79
97
newRoot.right.parent = x
80
98
}
@@ -90,14 +108,94 @@ public class RBTree {
90
108
x.parent = newRoot
91
109
}
92
110
111
+ public func insertFixup(value: Int){
112
+ let inserted = find(value)
113
+
114
+ if(inserted == nil){
115
+ print("INSERTEDNILLLLLLLL")
116
+ }
117
+ print("inserted: ")
118
+ print(inserted!.data)
119
+ insertCase1(inserted)
120
+ }
121
+ public func insertCase1(inserted: RBTNode?){
122
+ let myroot = self.root!
123
+ if myroot === inserted!{
124
+ self.root!.color = false
125
+ print("ITS THE ROOT")
126
+ print("insert case 1: The root was nil and we inserted a black node at root")
127
+ }
128
+ insertCase2(inserted!)
129
+ }
130
+ public func insertCase2(inserted: RBTNode?){
131
+ if inserted!.parent!.color == false{
132
+ print("Insert case 2: parent is black so nothing to change")
133
+ return
134
+ }
135
+ insertCase3(inserted!)
136
+ }
137
+ public func insertCase3(inserted: RBTNode?){
138
+ if(inserted!.parent!.sibling() != nil && inserted!.parent!.sibling()!.color == true){
139
+ print("insert case3: If both the parent P and the uncle U are red, then both of them can be repainted black and the grandparent G ")
140
+ inserted!.parent!.color = false
141
+ inserted!.parent!.sibling()!.color = false
142
+ let g = inserted!.grandparent
143
+ g()!.color = true
144
+ if(g()!.parent == nil){
145
+ g()!.color = false
146
+ }
147
+ }
148
+ insertCase4(inserted)
149
+ }
150
+ //THIS ONE MAY BE BROKEN!?!?!?!?
151
+ public func insertCase4(var inserted: RBTNode?){
152
+ if((inserted! === inserted!.parent!.right) && (inserted!.grandparent()!.left === inserted!.parent!)){
153
+ print("case 4")
154
+ rightRotate(inserted!.parent)
155
+ inserted! = inserted!.left
156
+ }
157
+ else if((inserted! === inserted!.parent!.left) && (inserted!.grandparent()!.right === inserted!.parent!)){
158
+ print("case 4")
159
+ leftRotate(inserted!.parent)
160
+ inserted! = inserted!.right
161
+ }
162
+ insertCase5(inserted)
163
+ }
164
+ public func insertCase5(inserted: RBTNode?){
165
+ if((inserted!.parent!.color == true && (inserted!.parent!.sibling()?.color == false || inserted!.parent!.sibling() == nil ))){
166
+ if(inserted! === inserted!.parent!.left && inserted!.grandparent()!.left === inserted!.parent!){
167
+ print("insert case 5: The parent P is red but the uncle U is black/nil, the current node N is the left child of P, and P is the left child of its parent G")
168
+ inserted!.parent.color = false
169
+ inserted!.grandparent()?.color = true
170
+ if(inserted! === inserted!.parent!.left){
171
+ print("its on the left")
172
+ rightRotate(inserted!.grandparent()!)
173
+ }
174
+ }
175
+ else if(inserted! === inserted!.parent!.right && inserted!.grandparent()!.right === inserted!.parent!){
176
+ print("insert case 5: RIGHT RIGHT")
177
+ inserted!.parent.color = false
178
+ inserted!.grandparent()?.color = true
179
+
180
+ print("Its on the right")
181
+ leftRotate(inserted!.grandparent()!)
182
+
183
+ }
184
+ }
185
+
186
+ print("weve reached the end boys")
187
+ }
93
188
94
- private func insertFixup(){}
95
189
public func insert(value: Int) {
96
190
insert(value, parent: root!)
97
- //insertFixUp( )
191
+ insertFixup(value )
98
192
}
99
193
private func insert(value: Int, parent: RBTNode) {
100
- if value < parent.data {
194
+ if self.root == nil{
195
+ self.root = RBTNode(rootData: value)
196
+ return
197
+ }
198
+ else if value < parent.data {
101
199
if let left = parent.left {
102
200
insert(value, parent: left)
103
201
} else {
@@ -116,6 +214,7 @@ public class RBTree {
116
214
public func find(data: Int)->RBTNode?{
117
215
return find(root!,data: data)
118
216
}
217
+
119
218
private func find(root: RBTNode, data: Int)->RBTNode?{
120
219
if data == root.data{
121
220
return root
@@ -140,19 +239,28 @@ tree.insert(9)
140
239
tree.insert(6)
141
240
tree.insert(5)
142
241
tree.insert(4)
143
- tree.insert(3)
144
- tree.inOrder()
145
- print("_________________________")
146
- let x = tree.root!
147
- print("root: \(tree.root!.data)")
148
- tree.rightRotate(x)
149
- print("root: \(tree.root!.data)")
242
+ tree.insert(40)
243
+ tree.insert(400)
244
+ tree.insert(430)
245
+
150
246
tree.inOrder()
151
247
152
- let t = tree.find(234)
153
- if t == nil{
154
- print("ASDASD")
155
- }
248
+ //print("_________________________")
249
+ //let x = tree.root!
250
+ //print("sibling: \(tree.root!.left!.sibling()!.data)")
251
+ //print("root: \(tree.root!.data)")
252
+ //tree.rightRotate(x)
253
+ //print("root: \(tree.root!.data)")
254
+ //tree.inOrder()
255
+
256
+ //let l = tree.root!.left!
257
+ //let r = tree.root!.right!
258
+ //print(l.data)
259
+ //print(r.data)
260
+ //if tree.root!.parent == nil{
261
+ // print("ASDASD")
262
+ //}
263
+
156
264
//how you access certain elements
157
265
//let root = tree.root!.left!
158
266
//if(root.parent === nil){
@@ -165,3 +273,4 @@ if t == nil{
165
273
//print(tree.root!.data)
166
274
//print(tree.root!.right!.data)
167
275
//print(tree.root!.left!.data)
276
+
0 commit comments