Skip to content

Commit 6644f8b

Browse files
author
Ashwin
committed
added delete
1 parent d13f9d0 commit 6644f8b

File tree

1 file changed

+170
-58
lines changed

1 file changed

+170
-58
lines changed

Red Black Trees/RBTree.swift

Lines changed: 170 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -13,11 +13,14 @@ public class RBTNode{
1313
}
1414
init(rootData: Int){
1515
self.data = rootData
16-
self.color = true //0is black 1 is red
16+
self.color = true //0 is black 1 is red
1717
self.left = nil
1818
self.right = nil
1919
self.parent = nil
2020
}
21+
deinit{
22+
print("Node: \(self.data) is bein deinitialized")
23+
}
2124
public func grandparent()->RBTNode?{
2225
if(self.parent === nil || self.parent.parent === nil){
2326
return nil
@@ -37,48 +40,9 @@ public class RBTNode{
3740
return self.parent!.left!
3841
}
3942
}
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 successor(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 predecessor(var node: RBTNode) -> RBTNode{
66-
if node.left !== nil {
67-
return minimum(node.left)
68-
}
69-
var successor = node.parent
70-
while successor !== nil && node === successor.left {
71-
node = successor
72-
successor = successor.parent
73-
}
74-
return successor
75-
}
76-
public func maximum(var rootNode: RBTNode) -> RBTNode{
77-
while rootNode.right !== nil {
78-
rootNode = rootNode.right
79-
}
80-
return rootNode
81-
}
43+
44+
45+
8246
}
8347
public class RBTree {
8448

@@ -189,16 +153,16 @@ private func insertCase3(inserted: RBTNode?){
189153
}
190154
insertCase4(inserted)
191155
}
192-
//THIS ONE MAY BE BROKEN!?!?!?!?
156+
193157
private func insertCase4(var inserted: RBTNode?){
194158
if((inserted! === inserted!.parent!.right) && (inserted!.grandparent()!.left === inserted!.parent!)){
195159
print("case 4")
196-
rightRotate(inserted!.parent)
160+
leftRotate(inserted!.parent)
197161
inserted! = inserted!.left
198162
}
199163
else if((inserted! === inserted!.parent!.left) && (inserted!.grandparent()!.right === inserted!.parent!)){
200164
print("case 4")
201-
leftRotate(inserted!.parent)
165+
rightRotate(inserted!.parent)
202166
inserted! = inserted!.right
203167
}
204168
insertCase5(inserted)
@@ -280,26 +244,174 @@ private func insertCase5(inserted: RBTNode?){
280244
return
281245
}
282246
}
247+
public func replaceNode(n1: RBTNode, n2: RBTNode){
248+
var temp = n1.data
249+
var temp_color = n1.color
250+
n1.data = n2.data
251+
n1.color = n2.color
252+
n2.data = temp
253+
n2.color = temp_color
254+
}
255+
public func minimum(var node: RBTNode)->RBTNode {
256+
while node.left !== nil{
257+
node = node.left
258+
}
259+
return node
260+
}
261+
public func successor(var node: RBTNode) -> RBTNode? {
262+
if node.right !== nil {
263+
return minimum(node.right)
264+
}
265+
var successor = node.parent
266+
while successor !== nil && node === successor.right {
267+
node = successor
268+
successor = successor.parent
269+
}
270+
return successor
271+
}
272+
public func predecessor(var node: RBTNode) -> RBTNode{
273+
if node.left !== nil {
274+
return minimum(node.left)
275+
}
276+
var successor = node.parent
277+
while successor !== nil && node === successor.left {
278+
node = successor
279+
successor = successor.parent
280+
}
281+
return successor
282+
}
283+
public func maximum(var rootNode: RBTNode) -> RBTNode{
284+
while rootNode.right !== nil {
285+
rootNode = rootNode.right
286+
}
287+
return rootNode
288+
}
289+
private func deletionFixup(x: RBTNode) {}
290+
291+
public func delete(x: Int){
292+
let toDel = find(x)
293+
deleteNode(toDel!)
294+
}
295+
private func deleteNode(var toDel: RBTNode?){
296+
print("DELETE CASE 0")
297+
if(toDel!.left == nil && toDel!.right == nil && toDel!.color == true){
298+
print("IM IN HERE")
299+
//DEINIT NOT BEING CALLED AGAIN?
300+
toDel = nil
301+
return
302+
}
303+
if(toDel!.left !== nil && toDel!.right !== nil){
304+
var pred = maximum(toDel!.left!)
305+
toDel!.data = pred.data
306+
toDel! = pred
307+
}
308+
var child: RBTNode? = nil
283309

310+
if(toDel!.right === nil){
311+
child = toDel!.left
312+
}
313+
else{
314+
child = toDel!.right
315+
}
316+
317+
if(toDel!.color == false){
318+
toDel!.color = child!.color
319+
deleteCase1(toDel!)
320+
}
284321

322+
if(child !== nil){
323+
replaceNode(toDel!, n2: child!)
285324

325+
if(toDel!.parent === nil && child !== nil){
326+
child!.color = false
327+
}
328+
}
329+
print("This statement isnt actually deleting the object??? V")
330+
toDel = nil
331+
}
332+
private func deleteCase1(var toDel: RBTNode?){
333+
print("DELETE CASE 1")
334+
if(toDel?.parent === nil){
335+
return
336+
}
337+
else{
338+
deleteCase2(toDel)
339+
}
340+
}
341+
private func deleteCase2(var toDel: RBTNode?){
342+
print("DELETE CASE 2")
343+
var sibling = toDel!.sibling()
344+
if(sibling?.color == true){
345+
toDel!.parent.color = true;
346+
sibling?.color = false
347+
if(toDel! === toDel!.parent.left){
348+
leftRotate(toDel!.parent)
349+
}
350+
else{
351+
rightRotate(toDel!.parent)
352+
}
353+
deleteCase3(toDel!)
354+
}
355+
}
356+
private func deleteCase3(var toDel: RBTNode?){
357+
print("DELETE CASE 3")
358+
if(toDel!.parent?.color == false && toDel!.sibling()?.color == false && toDel!.sibling()?.left!.color == false && toDel!.sibling()?.right!.color == false){
359+
toDel!.sibling()?.color = true
360+
toDel!.parent?.color = false
361+
}
362+
else{
363+
deleteCase4(toDel)
364+
}
365+
}
366+
private func deleteCase4(var toDel: RBTNode?){
367+
print("DELETE CASE 4")
368+
if(toDel!.parent?.color == true && toDel!.sibling()?.color == false && toDel!.sibling()?.left!.color == false && toDel!.sibling()?.right!.color == false){
369+
toDel!.sibling()?.color = true
370+
toDel!.parent.color = false
371+
}
372+
else{
373+
deleteCase5(toDel)
374+
}
375+
}
376+
private func deleteCase5(var toDel:RBTNode?){
377+
print("DELETE CASE 5")
378+
if(toDel! === toDel!.parent?.left && toDel!.sibling()?.color == false && toDel!.sibling()?.left.color == true && toDel!.sibling()?.right.color == false){
379+
toDel!.sibling()?.color = true
380+
toDel!.sibling()?.left?.color = false
381+
rightRotate(toDel!.sibling()!)
382+
}
383+
else if(toDel! === toDel!.parent?.right && toDel!.sibling()?.color == false && toDel!.sibling()?.left.color == false && toDel!.sibling()?.right.color == true){
384+
toDel!.sibling()?.color = true
385+
toDel!.sibling()?.right?.color = false
386+
leftRotate(toDel!.sibling()!)
387+
}
388+
}
389+
private func deleteCase6(var toDel: RBTNode?){
390+
print("DELETE CASE 6")
391+
let color = toDel!.sibling()?.color
392+
toDel!.parent?.color = color!
393+
toDel!.parent?.color = false
394+
if(toDel! === toDel!.parent.left){
395+
toDel!.sibling()?.right?.color = false
396+
leftRotate(toDel!.parent!)
397+
}
398+
else{
399+
toDel!.sibling()?.left?.color = false
400+
rightRotate(toDel!.parent!)
401+
}
402+
}
286403
}
287404

405+
406+
288407
var tree = RBTree(rootData: 8)
289-
tree.insert(9)
408+
290409
tree.insert(6)
291-
tree.insert(5)
292-
tree.insert(4)
293-
tree.insert(40)
294-
tree.insert(400)
295-
tree.insert(430)
296-
var t = tree.root!
297-
var min = t.minimum(t)
298-
print("MIN: \(min.data)")
299-
var x = tree.root!.left
300-
tree.inOrder()
301-
t.replaceNode(t,n2:x)
410+
tree.delete(6)
302411
tree.inOrder()
412+
print("_______")
413+
414+
303415

304416
//print("_________________________")
305417
//let x = tree.root!

0 commit comments

Comments
 (0)