Skip to content

Commit 89e4842

Browse files
authored
Merge pull request kodecocodes#1 from raywenderlich/master
update
2 parents ca8e7ff + 7dfe6b2 commit 89e4842

File tree

11 files changed

+145
-128
lines changed

11 files changed

+145
-128
lines changed

Breadth-First Search/Tests/Graph.swift

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ public class Node: CustomStringConvertible, Equatable {
3939
}
4040

4141
public func remove(edge: Edge) {
42-
neighbors.removeAtIndex(neighbors.indexOf { $0 === edge }!)
42+
neighbors.remove(at: neighbors.index { $0 === edge }!)
4343
}
4444
}
4545

@@ -56,13 +56,13 @@ public class Graph: CustomStringConvertible, Equatable {
5656
self.nodes = []
5757
}
5858

59-
public func addNode(label: String) -> Node {
59+
public func addNode(_ label: String) -> Node {
6060
let node = Node(label: label)
6161
nodes.append(node)
6262
return node
6363
}
6464

65-
public func addEdge(source: Node, neighbor: Node) {
65+
public func addEdge(_ source: Node, neighbor: Node) {
6666
let edge = Edge(neighbor: neighbor)
6767
source.neighbors.append(edge)
6868
}
@@ -78,15 +78,15 @@ public class Graph: CustomStringConvertible, Equatable {
7878
return description
7979
}
8080

81-
public func findNodeWithLabel(label: String) -> Node {
81+
public func findNodeWithLabel(_ label: String) -> Node {
8282
return nodes.filter { $0.label == label }.first!
8383
}
8484

8585
public func duplicate() -> Graph {
8686
let duplicated = Graph()
8787

8888
for node in nodes {
89-
duplicated.addNode(node.label)
89+
let _ = duplicated.addNode(node.label)
9090
}
9191

9292
for node in nodes {

Breadth-First Search/Tests/Queue.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ public struct Queue<T> {
1313
return array.count
1414
}
1515

16-
public mutating func enqueue(element: T) {
16+
public mutating func enqueue(_ element: T) {
1717
array.append(element)
1818
}
1919

Breadth-First Search/Tests/Tests.xcodeproj/project.pbxproj

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -89,11 +89,12 @@
8989
isa = PBXProject;
9090
attributes = {
9191
LastSwiftUpdateCheck = 0720;
92-
LastUpgradeCheck = 0720;
92+
LastUpgradeCheck = 0820;
9393
ORGANIZATIONNAME = "Swift Algorithm Club";
9494
TargetAttributes = {
9595
7B2BBC7F1C779D720067B71D = {
9696
CreatedOnToolsVersion = 7.2;
97+
LastSwiftMigration = 0820;
9798
};
9899
};
99100
};
@@ -230,6 +231,7 @@
230231
PRODUCT_BUNDLE_IDENTIFIER = swift.algorithm.club.Tests;
231232
PRODUCT_NAME = "$(TARGET_NAME)";
232233
SWIFT_OPTIMIZATION_LEVEL = "-Onone";
234+
SWIFT_VERSION = 3.0;
233235
};
234236
name = Debug;
235237
};
@@ -242,6 +244,7 @@
242244
LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/../Frameworks @loader_path/../Frameworks";
243245
PRODUCT_BUNDLE_IDENTIFIER = swift.algorithm.club.Tests;
244246
PRODUCT_NAME = "$(TARGET_NAME)";
247+
SWIFT_VERSION = 3.0;
245248
};
246249
name = Release;
247250
};

Breadth-First Search/Tests/Tests.xcodeproj/xcshareddata/xcschemes/Tests.xcscheme

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<?xml version="1.0" encoding="UTF-8"?>
22
<Scheme
3-
LastUpgradeVersion = "0720"
3+
LastUpgradeVersion = "0820"
44
version = "1.3">
55
<BuildAction
66
parallelizeBuildables = "YES"

Merge Sort/MergeSort.playground/Contents.swift

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,9 @@ func merge<T: Comparable>(leftPile: [T], rightPile: [T]) -> [T] {
1212
var leftIndex = 0
1313
var rightIndex = 0
1414
var orderedPile = [T]()
15+
if orderedPile.capacity < leftPile.count + rightPile.count {
16+
orderedPile.reserveCapacity(leftPile.count + rightPile.count)
17+
}
1518

1619
while leftIndex < leftPile.count && rightIndex < rightPile.count {
1720
if leftPile[leftIndex] < rightPile[rightIndex] {

Merge Sort/MergeSort.swift

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,9 @@ func merge<T: Comparable>(leftPile: [T], rightPile: [T]) -> [T] {
1818
var leftIndex = 0
1919
var rightIndex = 0
2020
var orderedPile = [T]()
21+
if orderedPile.capacity < leftPile.count + rightPile.count {
22+
orderedPile.reserveCapacity(leftPile.count + rightPile.count)
23+
}
2124

2225
while leftIndex < leftPile.count && rightIndex < rightPile.count {
2326
if leftPile[leftIndex] < rightPile[rightIndex] {

Threaded Binary Tree/README.markdown

Lines changed: 25 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -158,31 +158,31 @@ outlined above. We use these predecessor/successor attributes to great effect
158158
in this new algorithm for both forward and backward traversals:
159159

160160
```swift
161-
func traverseInOrderForward(visit: T -> Void) {
162-
var n: ThreadedBinaryTree
163-
n = minimum()
164-
while true {
165-
visit(n.value)
166-
if let successor = n.successor() {
167-
n = successor
168-
} else {
169-
break
161+
public func traverseInOrderForward(_ visit: (T) -> Void) {
162+
var n: ThreadedBinaryTree
163+
n = minimum()
164+
while true {
165+
visit(n.value)
166+
if let successor = n.successor() {
167+
n = successor
168+
} else {
169+
break
170+
}
171+
}
170172
}
171-
}
172-
}
173173

174-
func traverseInOrderBackward(visit: T -> Void) {
175-
var n: ThreadedBinaryTree
176-
n = maximum()
177-
while true {
178-
visit(n.value)
179-
if let predecessor = n.predecessor() {
180-
n = predecessor
181-
} else {
182-
break
174+
public func traverseInOrderBackward(_ visit: (T) -> Void) {
175+
var n: ThreadedBinaryTree
176+
n = maximum()
177+
while true {
178+
visit(n.value)
179+
if let predecessor = n.predecessor() {
180+
n = predecessor
181+
} else {
182+
break
183+
}
184+
}
183185
}
184-
}
185-
}
186186
```
187187
Again, this a method of `ThreadedBinaryTree`, so we'd call it via
188188
`node.traverseInorderForward(visitFunction)`. Note that we are able to specify
@@ -221,7 +221,7 @@ continuously manage the `leftThread` and `rightThread` variables. Rather than
221221
walking through some boring code, it is best to explain this with an example
222222
(although you can read through [the implementation](ThreadedBinaryTree.swift)
223223
if you want to know the finer details). Please note that this requires
224-
knowledge of binary search trees, so make sure you have
224+
knowledge of binary search trees, so make sure you have
225225
[read this first](../Binary Search Tree/).
226226

227227
> Note: we do allow duplicate nodes in this implementation of a threaded binary
@@ -342,11 +342,12 @@ Many of these methods are inherent to binary search trees as well, so you can
342342
find [further documentation here](../Binary Search Tree/).
343343

344344

345-
## See also
345+
## See also
346346

347347
[Threaded Binary Tree on Wikipedia](https://en.wikipedia.org/wiki/Threaded_binary_tree)
348348

349349
*Written for the Swift Algorithm Club by
350350
[Jayson Tung](https://github.com/JFTung)*
351+
*Migrated to Swift 3 by Jaap Wijnen*
351352

352353
*Images made using www.draw.io*

Threaded Binary Tree/ThreadedBinaryTreeTests.swift renamed to Threaded Binary Tree/ThreadedBinaryTree.playground/Contents.swift

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,8 @@
1-
/*
2-
TESTS
3-
I don't have access to an Apple computer, so I can't make a Playground or any
4-
of that fancy stuff. Here's a simple demonstration of the ThreadedBinaryTree
5-
class. It follows the examples in the README.
6-
*/
1+
//: Playground - noun: a place where people can play
2+
73

84
// Simple little debug function to make testing output pretty
9-
func check(tree: ThreadedBinaryTree<Int>?) {
5+
func check(_ tree: ThreadedBinaryTree<Int>?) {
106
if let tree = tree {
117
print("\(tree.count) Total Nodes:")
128
print(tree)
@@ -27,9 +23,9 @@ func check(tree: ThreadedBinaryTree<Int>?) {
2723
} else {
2824
print("This is an INVALID threaded binary tree.")
2925
}
30-
} else {
31-
print("This tree is nil.")
32-
}
26+
} else {
27+
print("This tree is nil.")
28+
}
3329
}
3430

3531

0 commit comments

Comments
 (0)