Skip to content

Commit afde1e0

Browse files
committed
Merge branch 'master' of github.com:axptwig/swift-algorithm-club
2 parents ab3f24a + 6ab41aa commit afde1e0

File tree

4 files changed

+290
-27
lines changed

4 files changed

+290
-27
lines changed

Radix-Sort/radixSort

19.8 KB
Binary file not shown.

Radix-Sort/radixSort.swift

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
func radixSort(inout arr: [Int] ) {
2+
3+
let radix = 10
4+
var done = false
5+
var index: Int
6+
var digit = 1
7+
8+
while !done {
9+
done = true
10+
11+
var buckets: [[Int]] = []
12+
13+
for _ in 1...radix {
14+
buckets.append([])
15+
}
16+
17+
18+
for number in arr {
19+
index = number / digit
20+
buckets[index % radix].append(number)
21+
if done && index > 0 {
22+
done = false
23+
}
24+
}
25+
26+
var i = 0
27+
28+
for j in 0..<radix {
29+
let bucket = buckets[j]
30+
for number in bucket {
31+
arr[i] = number
32+
i += 1
33+
}
34+
}
35+
36+
digit *= radix
37+
}
38+
}
39+
40+
var a: [Int] = [0, 69, 28, 14, 32, 1, 1, 1111, 1111111, 55, 123, 236626456256, 9393, 23, 66]
41+
radixSort(&a)
42+
print(a)

Red Black Trees/RBTree.swift

Lines changed: 65 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,48 @@ 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 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+
}
4082
}
4183
public class RBTree {
4284

@@ -118,7 +160,7 @@ public func insertFixup(value: Int){
118160
print(inserted!.data)
119161
insertCase1(inserted)
120162
}
121-
public func insertCase1(inserted: RBTNode?){
163+
private func insertCase1(inserted: RBTNode?){
122164
let myroot = self.root!
123165
if myroot === inserted!{
124166
self.root!.color = false
@@ -127,14 +169,14 @@ public func insertCase1(inserted: RBTNode?){
127169
}
128170
insertCase2(inserted!)
129171
}
130-
public func insertCase2(inserted: RBTNode?){
172+
private func insertCase2(inserted: RBTNode?){
131173
if inserted!.parent!.color == false{
132174
print("Insert case 2: parent is black so nothing to change")
133175
return
134176
}
135177
insertCase3(inserted!)
136178
}
137-
public func insertCase3(inserted: RBTNode?){
179+
private func insertCase3(inserted: RBTNode?){
138180
if(inserted!.parent!.sibling() != nil && inserted!.parent!.sibling()!.color == true){
139181
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 ")
140182
inserted!.parent!.color = false
@@ -148,7 +190,7 @@ public func insertCase3(inserted: RBTNode?){
148190
insertCase4(inserted)
149191
}
150192
//THIS ONE MAY BE BROKEN!?!?!?!?
151-
public func insertCase4(var inserted: RBTNode?){
193+
private func insertCase4(var inserted: RBTNode?){
152194
if((inserted! === inserted!.parent!.right) && (inserted!.grandparent()!.left === inserted!.parent!)){
153195
print("case 4")
154196
rightRotate(inserted!.parent)
@@ -161,7 +203,7 @@ public func insertCase4(var inserted: RBTNode?){
161203
}
162204
insertCase5(inserted)
163205
}
164-
public func insertCase5(inserted: RBTNode?){
206+
private func insertCase5(inserted: RBTNode?){
165207
if((inserted!.parent!.color == true && (inserted!.parent!.sibling()?.color == false || inserted!.parent!.sibling() == nil ))){
166208
if(inserted! === inserted!.parent!.left && inserted!.grandparent()!.left === inserted!.parent!){
167209
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 +221,8 @@ public func insertCase5(inserted: RBTNode?){
179221

180222
print("Its on the right")
181223
leftRotate(inserted!.grandparent()!)
182-
183224
}
184225
}
185-
186226
print("weve reached the end boys")
187227
}
188228

@@ -232,6 +272,17 @@ public func insertCase5(inserted: RBTNode?){
232272
return nil
233273
}
234274
}
275+
276+
//DELETION HELPER FUNCTIONS
277+
public func remove(value: Int){
278+
let toRemove = find(value)
279+
if(toRemove == nil){
280+
return
281+
}
282+
}
283+
284+
285+
235286
}
236287

237288
var tree = RBTree(rootData: 8)
@@ -242,7 +293,12 @@ tree.insert(4)
242293
tree.insert(40)
243294
tree.insert(400)
244295
tree.insert(430)
245-
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)
246302
tree.inOrder()
247303

248304
//print("_________________________")
@@ -264,7 +320,7 @@ tree.inOrder()
264320
//how you access certain elements
265321
//let root = tree.root!.left!
266322
//if(root.parent === nil){
267-
// print("niggs")
323+
// print("nil")
268324
//}
269325
//else{
270326
// print(root.parent.data)

0 commit comments

Comments
 (0)