Skip to content

Commit 692e49d

Browse files
committed
Merge branch 'master' of github.com:axptwig/swift-algorithm-club
2 parents c6d3403 + 6f88629 commit 692e49d

39 files changed

+2427
-86
lines changed

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+

Red Black Trees/RBTree.swift~

Lines changed: 167 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,167 @@
1+
2+
public class RBTNode{
3+
private(set) public var color: Bool
4+
private(set) public var left: RBTNode!
5+
private(set) public var right: RBTNode!
6+
private(set) public var parent: RBTNode!
7+
private(set) public var data: Int
8+
init(){
9+
self.data = -1
10+
self.color = true
11+
self.left = nil
12+
self.right = nil
13+
self.parent = nil
14+
}
15+
init(rootData: Int){
16+
self.data = rootData
17+
self.color = true
18+
self.left = nil
19+
self.right = nil
20+
self.parent = nil
21+
}
22+
}
23+
public class RBTree {
24+
25+
private(set) public var root: RBTNode?
26+
init(rootData: Int) {
27+
root = RBTNode(rootData: rootData)
28+
}
29+
init(){
30+
root = nil
31+
}
32+
public func depth()->Int{
33+
let n = depth(root!)
34+
return n
35+
}
36+
private func depth(rooty:RBTNode?)->Int{
37+
if(rooty == nil){
38+
return 0
39+
}
40+
else{
41+
return 1+(max(depth(root!.left),depth(root!.right)))
42+
}
43+
}
44+
45+
public func inOrder(){
46+
inOrder(root)
47+
}
48+
private func inOrder(root: RBTNode?){
49+
if(root == nil){
50+
print("nil")
51+
return
52+
}
53+
inOrder(root!.left)
54+
print("Data: \(root!.data) Color: \(root!.color)")
55+
inOrder(root!.right)
56+
}
57+
58+
private func leftRotate(x: RBTNode) {
59+
let newRoot = x.right
60+
x.right = newRoot.left
61+
if (newRoot.left !== nil) {
62+
newRoot.left.parent = x
63+
}
64+
newRoot.parent = x.parent
65+
if(x.parent === nil) {
66+
root = newRoot
67+
} else if (x === x.parent.left) {
68+
x.parent.left = newRoot
69+
} else {
70+
x.parent.right = newRoot
71+
}
72+
newRoot.left = x
73+
x.parent = newRoot
74+
}
75+
private func rightRotate(x: RBTNode) {
76+
let newRoot = x.left
77+
x.left = newRoot.right
78+
if newRoot.right !== nil {
79+
newRoot.right.parent = x
80+
}
81+
newRoot.parent = x.parent
82+
if x.parent === nil {
83+
root = newRoot
84+
} else if x === x.parent.right {
85+
x.parent.right = newRoot
86+
} else {
87+
x.parent.left = newRoot
88+
}
89+
newRoot.right = x
90+
x.parent = newRoot
91+
}
92+
93+
94+
private func insertFixup(){}
95+
public func insert(value: Int) {
96+
insert(value, parent: root!)
97+
//insertFixUp()
98+
}
99+
private func insert(value: Int, parent: RBTNode) {
100+
if value < parent.data {
101+
if let left = parent.left {
102+
insert(value, parent: left)
103+
} else {
104+
parent.left = RBTNode(rootData: value)
105+
parent.left?.parent = parent
106+
}
107+
} else {
108+
if let right = parent.right {
109+
insert(value, parent: right)
110+
} else {
111+
parent.right = RBTNode(rootData: value)
112+
parent.right?.parent = parent
113+
}
114+
}
115+
}
116+
public func find(data: Int)->RBTNode?{
117+
return find(root!,data: data)
118+
}
119+
private func find(root: RBTNode, data: Int)->RBTNode?{
120+
if data == root.data{
121+
return root
122+
}
123+
if root.data != data && root.right == nil && root.left == nil {
124+
return nil
125+
}
126+
else if data > root.data{
127+
return find(root.right, data: data)
128+
}
129+
else if data < root.data{
130+
return find(root.left, data: data)
131+
}
132+
else{
133+
return nil
134+
}
135+
}
136+
}
137+
138+
var tree = RBTree(rootData: 8)
139+
tree.insert(9)
140+
tree.insert(6)
141+
tree.insert(5)
142+
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)")
150+
tree.inOrder()
151+
152+
let t = tree.find(234)
153+
if t == nil{
154+
print("ASDASD")
155+
}
156+
//how you access certain elements
157+
//let root = tree.root!.left!
158+
//if(root.parent === nil){
159+
// print("niggs")
160+
//}
161+
//else{
162+
// print(root.parent.data)
163+
//}
164+
165+
//print(tree.root!.data)
166+
//print(tree.root!.right!.data)
167+
//print(tree.root!.left!.data)

Trie/.build/debug/Trie

101 KB
Binary file not shown.

Trie/.build/debug/Trie.a

122 KB
Binary file not shown.

Trie/.build/debug/Trie.o/Trie/Queue.d

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
/home/christian/Desktop/swift-algorithm-club/Trie/.build/debug/Trie.o/sources/Queue.swift.o : /home/christian/Desktop/swift-algorithm-club/Trie/sources/main.swift /home/christian/Desktop/swift-algorithm-club/Trie/sources/trie.swift /home/christian/Desktop/swift-algorithm-club/Trie/sources/Queue.swift /home/christian/swiftv2/usr/lib/swift/linux/x86_64/Swift.swiftmodule /home/christian/swiftv2/usr/lib/swift/shims/UnicodeShims.h /home/christian/swiftv2/usr/lib/swift/shims/SwiftStdint.h /home/christian/swiftv2/usr/lib/swift/shims/SwiftStddef.h /home/christian/swiftv2/usr/lib/swift/shims/LibcShims.h /home/christian/swiftv2/usr/lib/swift/shims/RuntimeStubs.h /home/christian/swiftv2/usr/lib/swift/shims/RuntimeShims.h /home/christian/swiftv2/usr/lib/swift/shims/RefCount.h /home/christian/swiftv2/usr/lib/swift/shims/HeapObject.h /home/christian/swiftv2/usr/lib/swift/shims/GlobalObjects.h /home/christian/swiftv2/usr/lib/swift/shims/FoundationShims.h /home/christian/swiftv2/usr/lib/swift/shims/CoreFoundationShims.h /home/christian/swiftv2/usr/lib/swift/shims/module.map
2+
/home/christian/Desktop/swift-algorithm-club/Trie/.build/debug/Trie.o/Trie/Queue~partial.swiftmodule : /home/christian/Desktop/swift-algorithm-club/Trie/sources/main.swift /home/christian/Desktop/swift-algorithm-club/Trie/sources/trie.swift /home/christian/Desktop/swift-algorithm-club/Trie/sources/Queue.swift /home/christian/swiftv2/usr/lib/swift/linux/x86_64/Swift.swiftmodule /home/christian/swiftv2/usr/lib/swift/shims/UnicodeShims.h /home/christian/swiftv2/usr/lib/swift/shims/SwiftStdint.h /home/christian/swiftv2/usr/lib/swift/shims/SwiftStddef.h /home/christian/swiftv2/usr/lib/swift/shims/LibcShims.h /home/christian/swiftv2/usr/lib/swift/shims/RuntimeStubs.h /home/christian/swiftv2/usr/lib/swift/shims/RuntimeShims.h /home/christian/swiftv2/usr/lib/swift/shims/RefCount.h /home/christian/swiftv2/usr/lib/swift/shims/HeapObject.h /home/christian/swiftv2/usr/lib/swift/shims/GlobalObjects.h /home/christian/swiftv2/usr/lib/swift/shims/FoundationShims.h /home/christian/swiftv2/usr/lib/swift/shims/CoreFoundationShims.h /home/christian/swiftv2/usr/lib/swift/shims/module.map
3+
/home/christian/Desktop/swift-algorithm-club/Trie/.build/debug/Trie.o/Trie/Queue~partial.swiftdoc : /home/christian/Desktop/swift-algorithm-club/Trie/sources/main.swift /home/christian/Desktop/swift-algorithm-club/Trie/sources/trie.swift /home/christian/Desktop/swift-algorithm-club/Trie/sources/Queue.swift /home/christian/swiftv2/usr/lib/swift/linux/x86_64/Swift.swiftmodule /home/christian/swiftv2/usr/lib/swift/shims/UnicodeShims.h /home/christian/swiftv2/usr/lib/swift/shims/SwiftStdint.h /home/christian/swiftv2/usr/lib/swift/shims/SwiftStddef.h /home/christian/swiftv2/usr/lib/swift/shims/LibcShims.h /home/christian/swiftv2/usr/lib/swift/shims/RuntimeStubs.h /home/christian/swiftv2/usr/lib/swift/shims/RuntimeShims.h /home/christian/swiftv2/usr/lib/swift/shims/RefCount.h /home/christian/swiftv2/usr/lib/swift/shims/HeapObject.h /home/christian/swiftv2/usr/lib/swift/shims/GlobalObjects.h /home/christian/swiftv2/usr/lib/swift/shims/FoundationShims.h /home/christian/swiftv2/usr/lib/swift/shims/CoreFoundationShims.h /home/christian/swiftv2/usr/lib/swift/shims/module.map

0 commit comments

Comments
 (0)