Skip to content

Commit ba9b1bb

Browse files
author
Ashwin
committed
added delete helpers
1 parent 692e49d commit ba9b1bb

File tree

2 files changed

+181
-27
lines changed

2 files changed

+181
-27
lines changed

Red Black Trees/RBTree.swift

Lines changed: 54 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,37 @@ public class RBTNode{
3737
return self.parent!.left!
3838
}
3939
}
40+
public func replaceNode(n1: RBTNode, n2: RBTNode){
41+
var temp = n1.data
42+
var temp_color = n1.color
43+
n1.data = n2.data
44+
n1.color = n2.color
45+
n2.data = temp
46+
n2.color = temp_color
47+
}
48+
public func minimum(var node: RBTNode)->RBTNode {
49+
while node.left !== nil{
50+
node = node.left
51+
}
52+
return node
53+
}
54+
public func successorOfNode(var node: RBTNode) -> RBTNode? {
55+
if node.right !== nil {
56+
return minimum(node.right)
57+
}
58+
var successor = node.parent
59+
while successor !== nil && node === successor.right {
60+
node = successor
61+
successor = successor.parent
62+
}
63+
return successor
64+
}
65+
public func maximum(var rootNode: RBTNode) -> RBTNode{
66+
while rootNode.right !== nil {
67+
rootNode = rootNode.right
68+
}
69+
return rootNode
70+
}
4071
}
4172
public class RBTree {
4273

@@ -118,7 +149,7 @@ public func insertFixup(value: Int){
118149
print(inserted!.data)
119150
insertCase1(inserted)
120151
}
121-
public func insertCase1(inserted: RBTNode?){
152+
private func insertCase1(inserted: RBTNode?){
122153
let myroot = self.root!
123154
if myroot === inserted!{
124155
self.root!.color = false
@@ -127,14 +158,14 @@ public func insertCase1(inserted: RBTNode?){
127158
}
128159
insertCase2(inserted!)
129160
}
130-
public func insertCase2(inserted: RBTNode?){
161+
private func insertCase2(inserted: RBTNode?){
131162
if inserted!.parent!.color == false{
132163
print("Insert case 2: parent is black so nothing to change")
133164
return
134165
}
135166
insertCase3(inserted!)
136167
}
137-
public func insertCase3(inserted: RBTNode?){
168+
private func insertCase3(inserted: RBTNode?){
138169
if(inserted!.parent!.sibling() != nil && inserted!.parent!.sibling()!.color == true){
139170
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 ")
140171
inserted!.parent!.color = false
@@ -148,7 +179,7 @@ public func insertCase3(inserted: RBTNode?){
148179
insertCase4(inserted)
149180
}
150181
//THIS ONE MAY BE BROKEN!?!?!?!?
151-
public func insertCase4(var inserted: RBTNode?){
182+
private func insertCase4(var inserted: RBTNode?){
152183
if((inserted! === inserted!.parent!.right) && (inserted!.grandparent()!.left === inserted!.parent!)){
153184
print("case 4")
154185
rightRotate(inserted!.parent)
@@ -161,7 +192,7 @@ public func insertCase4(var inserted: RBTNode?){
161192
}
162193
insertCase5(inserted)
163194
}
164-
public func insertCase5(inserted: RBTNode?){
195+
private func insertCase5(inserted: RBTNode?){
165196
if((inserted!.parent!.color == true && (inserted!.parent!.sibling()?.color == false || inserted!.parent!.sibling() == nil ))){
166197
if(inserted! === inserted!.parent!.left && inserted!.grandparent()!.left === inserted!.parent!){
167198
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")
@@ -179,10 +210,8 @@ public func insertCase5(inserted: RBTNode?){
179210

180211
print("Its on the right")
181212
leftRotate(inserted!.grandparent()!)
182-
183213
}
184214
}
185-
186215
print("weve reached the end boys")
187216
}
188217

@@ -232,6 +261,17 @@ public func insertCase5(inserted: RBTNode?){
232261
return nil
233262
}
234263
}
264+
265+
//DELETION HELPER FUNCTIONS
266+
public func remove(value: Int){
267+
let toRemove = find(value)
268+
if(toRemove == nil){
269+
return
270+
}
271+
}
272+
273+
274+
235275
}
236276

237277
var tree = RBTree(rootData: 8)
@@ -242,7 +282,12 @@ tree.insert(4)
242282
tree.insert(40)
243283
tree.insert(400)
244284
tree.insert(430)
245-
285+
var t = tree.root!
286+
var min = t.minimum(t)
287+
print("MIN: \(min.data)")
288+
var x = tree.root!.left
289+
tree.inOrder()
290+
t.replaceNode(t,n2:x)
246291
tree.inOrder()
247292

248293
//print("_________________________")
@@ -264,7 +309,7 @@ tree.inOrder()
264309
//how you access certain elements
265310
//let root = tree.root!.left!
266311
//if(root.parent === nil){
267-
// print("niggs")
312+
// print("nil")
268313
//}
269314
//else{
270315
// print(root.parent.data)

Red Black Trees/RBTree.swift~

Lines changed: 127 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
21
public class RBTNode{
32
private(set) public var color: Bool
43
private(set) public var left: RBTNode!
@@ -14,17 +13,37 @@ public class RBTNode{
1413
}
1514
init(rootData: Int){
1615
self.data = rootData
17-
self.color = true
16+
self.color = true //0is black 1 is red
1817
self.left = nil
1918
self.right = nil
2019
self.parent = nil
2120
}
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+
}
2240
}
2341
public class RBTree {
2442

2543
private(set) public var root: RBTNode?
2644
init(rootData: Int) {
2745
root = RBTNode(rootData: rootData)
46+
root!.color = false
2847
}
2948
init(){
3049
root = nil
@@ -47,7 +66,6 @@ public class RBTree {
4766
}
4867
private func inOrder(root: RBTNode?){
4968
if(root == nil){
50-
print("nil")
5169
return
5270
}
5371
inOrder(root!.left)
@@ -74,7 +92,7 @@ public class RBTree {
7492
}
7593
private func rightRotate(x: RBTNode) {
7694
let newRoot = x.left
77-
x.left = newRoot.right
95+
x.left = newRoot.right
7896
if newRoot.right !== nil {
7997
newRoot.right.parent = x
8098
}
@@ -90,14 +108,94 @@ public class RBTree {
90108
x.parent = newRoot
91109
}
92110

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+
}
93188

94-
private func insertFixup(){}
95189
public func insert(value: Int) {
96190
insert(value, parent: root!)
97-
//insertFixUp()
191+
insertFixup(value)
98192
}
99193
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 {
101199
if let left = parent.left {
102200
insert(value, parent: left)
103201
} else {
@@ -116,6 +214,7 @@ public class RBTree {
116214
public func find(data: Int)->RBTNode?{
117215
return find(root!,data: data)
118216
}
217+
119218
private func find(root: RBTNode, data: Int)->RBTNode?{
120219
if data == root.data{
121220
return root
@@ -140,19 +239,28 @@ tree.insert(9)
140239
tree.insert(6)
141240
tree.insert(5)
142241
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+
150246
tree.inOrder()
151247

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+
156264
//how you access certain elements
157265
//let root = tree.root!.left!
158266
//if(root.parent === nil){
@@ -165,3 +273,4 @@ if t == nil{
165273
//print(tree.root!.data)
166274
//print(tree.root!.right!.data)
167275
//print(tree.root!.left!.data)
276+

0 commit comments

Comments
 (0)