Skip to content

Commit c316fa9

Browse files
committed
2 parents b65ac55 + ecd1ee3 commit c316fa9

File tree

136 files changed

+3679
-559
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

136 files changed

+3679
-559
lines changed

.travis.yml

+24
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
language: objective-c
2+
osx_image: xcode7.2
3+
4+
script:
5+
6+
- xcodebuild test -project ./Array2D/Tests/Tests.xcodeproj -scheme Tests
7+
- xcodebuild test -project ./AVL\ Tree/Tests/Tests.xcodeproj -scheme Tests
8+
- xcodebuild test -project ./Binary\ Search/Tests/Tests.xcodeproj -scheme Tests
9+
- xcodebuild test -project ./Binary\ Search\ Tree/Solution\ 1/Tests/Tests.xcodeproj -scheme Tests
10+
- xcodebuild test -project ./Bloom\ Filter/Tests/Tests.xcodeproj -scheme Tests
11+
- xcodebuild test -project ./Breadth-First\ Search/Tests/Tests.xcodeproj -scheme Tests
12+
- xcodebuild test -project ./Heap/Tests/Tests.xcodeproj -scheme Tests
13+
- xcodebuild test -project ./Heap\ Sort/Tests/Tests.xcodeproj -scheme Tests
14+
- xcodebuild test -project ./Insertion\ Sort/Tests/Tests.xcodeproj -scheme Tests
15+
- xcodebuild test -project ./K-Means/Tests/Tests.xcodeproj -scheme Tests
16+
- xcodebuild test -project ./Linked\ List/Tests/Tests.xcodeproj -scheme Tests
17+
- xcodebuild test -project ./Priority\ Queue/Tests/Tests.xcodeproj -scheme Tests
18+
- xcodebuild test -project ./Queue/Tests/Tests.xcodeproj -scheme Tests
19+
- xcodebuild test -project ./Quicksort/Tests/Tests.xcodeproj -scheme Tests
20+
- xcodebuild test -project ./Run-Length\ Encoding/Tests/Tests.xcodeproj -scheme Tests
21+
- xcodebuild test -project ./Select\ Minimum\ Maximum/Tests/Tests.xcodeproj -scheme Tests
22+
- xcodebuild test -project ./Selection\ Sort/Tests/Tests.xcodeproj -scheme Tests
23+
- xcodebuild test -project ./Shell\ Sort/Tests/Tests.xcodeproj -scheme Tests
24+
- xcodebuild test -project ./Stack/Tests/Tests.xcodeproj -scheme Tests

AVL Tree/README.markdown

+5-5
Original file line numberDiff line numberDiff line change
@@ -8,15 +8,15 @@ This is an example of an unbalanced tree:
88

99
![Unbalanced tree](Images/Unbalanced.png)
1010

11-
All the children are in the left branch and none are in the right. This is essentially the same as a [linked list](../Linked List/) and as a result, searching takes **O(n)** time instead of the much faster **O(log n)** that you'd expect from a binary search tree.
11+
All the children are in the left branch and none are in the right. This is essentially the same as a [linked list](../Linked List/). As a result, searching takes **O(n)** time instead of the much faster **O(log n)** that you'd expect from a binary search tree.
1212

1313
A balanced version of that tree would look like this:
1414

1515
![Balanced tree](Images/Balanced.png)
1616

1717
One way to make the binary search tree balanced is to insert the nodes in a totally random order. But that doesn't guarantee success, nor is it always practical.
1818

19-
The other solution is to use a *self-balancing* binary tree. This type of data structure adjusts the tree to keep it balanced after you insert or delete nodes. The height of such a tree is guaranteed to be *log(n)* where *n* is the number nodes. On a balanced tree all insert, remove, and search operations take **O(log n)** time. That means fast. ;-)
19+
The other solution is to use a *self-balancing* binary tree. This type of data structure adjusts the tree to keep it balanced after you insert or delete nodes. The height of such a tree is guaranteed to be *log(n)* where *n* is the number nodes. On a balanced tree all insert, remove, and search operations take only **O(log n)** time. That means fast. ;-)
2020

2121
## Introducing the AVL tree
2222

@@ -32,7 +32,7 @@ As mentioned, in an AVL tree a node is balanced if its left and right subtree ha
3232

3333
![Balanced trees](Images/BalanceOK.png)
3434

35-
But these are trees that are unbalanced, because the height of the left subtree is too large compared to the right subtree:
35+
But the following are trees that are unbalanced, because the height of the left subtree is too large compared to the right subtree:
3636

3737
![Unbalanced trees](Images/BalanceNotOK.png)
3838

@@ -52,7 +52,7 @@ Insertion never needs more than 2 rotations. Removal might require up to *log(n)
5252

5353
## The code
5454

55-
Most of the code in [AVLTree.swift](AVLTree.swift) is just regular [binary search tree](../Binary Search Tree/) stuff. You'll find this in any implementation of a binary search tree. For example, searching the tree is exactly the same. The only things that an AVL tree does slightly differently is inserting and deleting the nodes.
55+
Most of the code in [AVLTree.swift](AVLTree.swift) is just regular [binary search tree](../Binary Search Tree/) stuff. You'll find this in any implementation of a binary search tree. For example, searching the tree is exactly the same. The only things that an AVL tree does slightly differently are inserting and deleting the nodes.
5656

5757
> **Note:** If you're a bit fuzzy on the regular operations of a binary search tree, I suggest you [catch up on those first](../Binary Search Tree/). It will make the rest of the AVL tree easier to understand.
5858
@@ -66,6 +66,6 @@ The interesting bits are in the following methods:
6666

6767
[AVL tree on Wikipedia](https://en.wikipedia.org/wiki/AVL_tree)
6868

69-
AVL tree was the first self-balancing binary tree. These days, the [red-black tree](../Red-Black Tree/) seems to be more common.
69+
AVL tree was the first self-balancing binary tree. These days, the [red-black tree](../Red-Black Tree/) seems to be more popular.
7070

7171
*Written for Swift Algorithm Club by Mike Taghavi and Matthijs Hollemans*

AVL Tree/Tests/AVLTreeTests.swift

+47
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,53 @@ class AVLTreeTests: XCTestCase {
6868
self.tree?.search(400)
6969
}
7070
}
71+
72+
func testMinimumOnPopulatedTree() {
73+
self.tree?.autopopulateWithNodes(500)
74+
let min = self.tree?.root?.minimum()
75+
XCTAssertNotNil(min, "Minimum function not working")
76+
}
77+
78+
func testMinimumOnSingleTreeNode() {
79+
let treeNode = TreeNode(key: 1, payload: "A")
80+
let min = treeNode.minimum()
81+
82+
XCTAssertNotNil(min, "Minimum on single node should be returned")
83+
XCTAssertEqual(min?.payload,treeNode.payload)
84+
}
85+
86+
func testSuccessorOfRoot() {
87+
self.tree?.autopopulateWithNodes(10)
88+
let successor = self.tree?.root?.successor()
89+
XCTAssertNotNil(successor, "Succesor of root, non-empty tree, not found")
90+
}
91+
92+
func testSuccessorOfMinimum() {
93+
self.tree?.autopopulateWithNodes(10)
94+
95+
let minimum = self.tree?.root?.minimum()
96+
XCTAssertNotNil(minimum, "Minimum should exist here")
97+
98+
let successor = minimum!.successor()
99+
XCTAssertNotNil(successor, "Succesor of minimum, non-empty tree, not found")
100+
}
101+
102+
func testSuccessorSingleNode() {
103+
let singleNode = TreeNode(key: 1, payload: "A")
104+
let successor = singleNode.successor()
105+
XCTAssertNil(successor, "Empty node should not have succesor")
106+
}
107+
108+
func testDeleteExistentKey() {
109+
self.tree?.delete(1)
110+
XCTAssertNil(self.tree?.search(1), "Key should not exist anymore")
111+
}
112+
113+
func testDeleteNOTExistentKey() {
114+
self.tree?.delete(1056)
115+
XCTAssertNil(self.tree?.search(1056), "Key should not exist")
116+
}
117+
71118
}
72119

73120
extension AVLTree where Key : SignedIntegerType {
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<Scheme
3+
LastUpgradeVersion = "0720"
4+
version = "1.3">
5+
<BuildAction
6+
parallelizeBuildables = "YES"
7+
buildImplicitDependencies = "YES">
8+
</BuildAction>
9+
<TestAction
10+
buildConfiguration = "Debug"
11+
selectedDebuggerIdentifier = "Xcode.DebuggerFoundation.Debugger.LLDB"
12+
selectedLauncherIdentifier = "Xcode.DebuggerFoundation.Launcher.LLDB"
13+
shouldUseLaunchSchemeArgsEnv = "YES">
14+
<Testables>
15+
<TestableReference
16+
skipped = "NO">
17+
<BuildableReference
18+
BuildableIdentifier = "primary"
19+
BlueprintIdentifier = "7B2BBC7F1C779D720067B71D"
20+
BuildableName = "Tests.xctest"
21+
BlueprintName = "Tests"
22+
ReferencedContainer = "container:Tests.xcodeproj">
23+
</BuildableReference>
24+
</TestableReference>
25+
</Testables>
26+
<AdditionalOptions>
27+
</AdditionalOptions>
28+
</TestAction>
29+
<LaunchAction
30+
buildConfiguration = "Debug"
31+
selectedDebuggerIdentifier = "Xcode.DebuggerFoundation.Debugger.LLDB"
32+
selectedLauncherIdentifier = "Xcode.DebuggerFoundation.Launcher.LLDB"
33+
launchStyle = "0"
34+
useCustomWorkingDirectory = "NO"
35+
ignoresPersistentStateOnLaunch = "NO"
36+
debugDocumentVersioning = "YES"
37+
debugServiceExtension = "internal"
38+
allowLocationSimulation = "YES">
39+
<AdditionalOptions>
40+
</AdditionalOptions>
41+
</LaunchAction>
42+
<ProfileAction
43+
buildConfiguration = "Release"
44+
shouldUseLaunchSchemeArgsEnv = "YES"
45+
savedToolIdentifier = ""
46+
useCustomWorkingDirectory = "NO"
47+
debugDocumentVersioning = "YES">
48+
</ProfileAction>
49+
<AnalyzeAction
50+
buildConfiguration = "Debug">
51+
</AnalyzeAction>
52+
<ArchiveAction
53+
buildConfiguration = "Release"
54+
revealArchiveInOrganizer = "YES">
55+
</ArchiveAction>
56+
</Scheme>

Algorithm Design.markdown

+3-3
Original file line numberDiff line numberDiff line change
@@ -4,17 +4,17 @@ What to do when you're faced with a new problem and you need to find an algorith
44

55
### Is it similar to another problem?
66

7-
One thing I like about [The Algorithm Design Manual](http://www.algorist.com) by Steven Skiena is that it includes a catalog of problems and solutions you can try.
7+
If you can frame your problem in terms of another, more general problem, then you might be able to use an existing algorithm. Why reinvent the wheel?
88

9-
If you can frame your problem in terms of another, more general problem, then you might be able to use an existing algorithm.
9+
One thing I like about [The Algorithm Design Manual](http://www.algorist.com) by Steven Skiena is that it includes a catalog of problems and solutions you can try. (See also his [algorithm repository](http://www3.cs.stonybrook.edu/~algorith/).)
1010

1111
### It's OK to start with brute force
1212

1313
Naive, brute force solutions are often too slow for practical use but they're a good starting point. By writing the brute force solution, you learn to understand what the problem is really all about.
1414

1515
Once you have a brute force implementation you can use that to verify that any improvements you come up with are correct.
1616

17-
And if you only work with small datasets, then a brute force approach may actually be good enough on its own.
17+
And if you only work with small datasets, then a brute force approach may actually be good enough on its own. Don't fall into the trap of premature optimization!
1818

1919
### Divide and conquer
2020

Array2D/README.markdown

+9-3
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# Array2D
22

3-
In C and Objective-C you can write this,
3+
In C and Objective-C you can write the following line,
44

55
int cookies[9][7];
66

@@ -61,7 +61,7 @@ var threeDimensions = dim(2, dim(3, dim(4, 0)))
6161

6262
The downside of using multi-dimensional arrays in this fashion -- actually, multiple nested arrays -- is that it's easy to lose track of what dimension represents what.
6363

64-
So instead let's create our own type that acts like a 2-D array and that is more convenient to use. Here it is:
64+
So instead let's create our own type that acts like a 2-D array and that is more convenient to use. Here it is, short and sweet:
6565

6666
```swift
6767
public struct Array2D<T> {
@@ -100,8 +100,14 @@ Thanks to the `subscript` function, you can do the following to retrieve an obje
100100
let myCookie = cookies[column, row]
101101
```
102102

103+
Or change it:
104+
105+
```swift
106+
cookies[column, row] = newCookie
107+
```
108+
103109
Internally, `Array2D` uses a single one-dimensional array to store the data. The index of an object in that array is given by `(row x numberOfColumns) + column`. But as a user of `Array2D` you don't have to worry about that; you only have to think in terms of "column" and "row", and let `Array2D` figure out the details for you. That's the advantage of wrapping primitive types into a wrapper class or struct.
104110

105111
And that's all there is to it.
106112

107-
*Written by Matthijs Hollemans*
113+
*Written for Swift Algorithm Club by Matthijs Hollemans*
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<Scheme
3+
LastUpgradeVersion = "0720"
4+
version = "1.3">
5+
<BuildAction
6+
parallelizeBuildables = "YES"
7+
buildImplicitDependencies = "YES">
8+
</BuildAction>
9+
<TestAction
10+
buildConfiguration = "Debug"
11+
selectedDebuggerIdentifier = "Xcode.DebuggerFoundation.Debugger.LLDB"
12+
selectedLauncherIdentifier = "Xcode.DebuggerFoundation.Launcher.LLDB"
13+
shouldUseLaunchSchemeArgsEnv = "YES">
14+
<Testables>
15+
<TestableReference
16+
skipped = "NO">
17+
<BuildableReference
18+
BuildableIdentifier = "primary"
19+
BlueprintIdentifier = "7B2BBC7F1C779D720067B71D"
20+
BuildableName = "Tests.xctest"
21+
BlueprintName = "Tests"
22+
ReferencedContainer = "container:Tests.xcodeproj">
23+
</BuildableReference>
24+
</TestableReference>
25+
</Testables>
26+
<AdditionalOptions>
27+
</AdditionalOptions>
28+
</TestAction>
29+
<LaunchAction
30+
buildConfiguration = "Debug"
31+
selectedDebuggerIdentifier = "Xcode.DebuggerFoundation.Debugger.LLDB"
32+
selectedLauncherIdentifier = "Xcode.DebuggerFoundation.Launcher.LLDB"
33+
launchStyle = "0"
34+
useCustomWorkingDirectory = "NO"
35+
ignoresPersistentStateOnLaunch = "NO"
36+
debugDocumentVersioning = "YES"
37+
debugServiceExtension = "internal"
38+
allowLocationSimulation = "YES">
39+
<AdditionalOptions>
40+
</AdditionalOptions>
41+
</LaunchAction>
42+
<ProfileAction
43+
buildConfiguration = "Release"
44+
shouldUseLaunchSchemeArgsEnv = "YES"
45+
savedToolIdentifier = ""
46+
useCustomWorkingDirectory = "NO"
47+
debugDocumentVersioning = "YES">
48+
</ProfileAction>
49+
<AnalyzeAction
50+
buildConfiguration = "Debug">
51+
</AnalyzeAction>
52+
<ArchiveAction
53+
buildConfiguration = "Release"
54+
revealArchiveInOrganizer = "YES">
55+
</ArchiveAction>
56+
</Scheme>

0 commit comments

Comments
 (0)