Skip to content

Commit 1160658

Browse files
authored
Merge branch 'master' into serialize-with-array
2 parents af8b44f + ebaaac2 commit 1160658

File tree

117 files changed

+3887
-1919
lines changed

Some content is hidden

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

117 files changed

+3887
-1919
lines changed

.travis.yml

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ script:
1212
- xcodebuild test -project ./Array2D/Tests/Tests.xcodeproj -scheme Tests
1313
- xcodebuild test -project ./AVL\ Tree/Tests/Tests.xcodeproj -scheme Tests
1414
- xcodebuild test -project ./Binary\ Search/Tests/Tests.xcodeproj -scheme Tests
15+
- xcodebuild test -project ./Boyer-Moore/Tests/Tests.xcodeproj -scheme Tests
1516
# - xcodebuild test -project ./Binary\ Search\ Tree/Solution\ 1/Tests/Tests.xcodeproj -scheme Tests
1617
- xcodebuild test -project ./Bloom\ Filter/Tests/Tests.xcodeproj -scheme Tests
1718
# - xcodebuild test -project ./Bounded\ Priority\ Queue/Tests/Tests.xcodeproj -scheme Tests
@@ -22,21 +23,23 @@ script:
2223
# - xcodebuild test -project ./Depth-First\ Search/Tests/Tests.xcodeproj -scheme Tests
2324
# - xcodebuild test -project ./Graph/Graph.xcodeproj -scheme GraphTests
2425
# - xcodebuild test -project ./Heap/Tests/Tests.xcodeproj -scheme Tests
25-
# - xcodebuild test -project ./Heap\ Sort/Tests/Tests.xcodeproj -scheme Tests
26+
- xcodebuild test -project ./Heap\ Sort/Tests/Tests.xcodeproj -scheme Tests
2627
- xcodebuild test -project ./Insertion\ Sort/Tests/Tests.xcodeproj -scheme Tests
2728
# - xcodebuild test -project ./K-Means/Tests/Tests.xcodeproj -scheme Tests
2829
# - xcodebuild test -project ./Linked\ List/Tests/Tests.xcodeproj -scheme Tests
2930
- xcodebuild test -project ./Longest\ Common\ Subsequence/Tests/Tests.xcodeproj -scheme Tests
30-
# - xcodebuild test -project ./Minimum\ Spanning\ Tree\ \(Unweighted\)/Tests/Tests.xcodeproj -scheme Tests
31+
- xcodebuild test -project ./Minimum\ Spanning\ Tree\ \(Unweighted\)/Tests/Tests.xcodeproj -scheme Tests
3132
# - xcodebuild test -project ./Priority\ Queue/Tests/Tests.xcodeproj -scheme Tests
3233
- xcodebuild test -project ./Queue/Tests/Tests.xcodeproj -scheme Tests
3334
# - xcodebuild test -project ./Quicksort/Tests/Tests.xcodeproj -scheme Tests
35+
- xcodebuild test -project ./Rootish\ Array\ Stack/Tests/Tests.xcodeproj -scheme Tests
3436
# - xcodebuild test -project ./Run-Length\ Encoding/Tests/Tests.xcodeproj -scheme Tests
3537
# - xcodebuild test -project ./Select\ Minimum\ Maximum/Tests/Tests.xcodeproj -scheme Tests
3638
- xcodebuild test -project ./Selection\ Sort/Tests/Tests.xcodeproj -scheme Tests
3739
# - xcodebuild test -project ./Shell\ Sort/Tests/Tests.xcodeproj -scheme Tests
38-
# - xcodebuild test -project ./Shortest\ Path\ \(Unweighted\)/Tests/Tests.xcodeproj -scheme Tests
40+
- xcodebuild test -project ./Shortest\ Path\ \(Unweighted\)/Tests/Tests.xcodeproj -scheme Tests
3941
# - xcodebuild test -project ./Single-Source\ Shortest\ Paths\ \(Weighted\)/SSSP.xcodeproj -scheme SSSPTests
4042
- xcodebuild test -project ./Stack/Tests/Tests.xcodeproj -scheme Tests
4143
- xcodebuild test -project ./Topological\ Sort/Tests/Tests.xcodeproj -scheme Tests
4244
- xcodebuild test -project ./Treap/Treap/Treap.xcodeproj -scheme Tests
45+
- xcodebuild test -project ./Palindromes/Test/Test.xcodeproj -scheme Test

AVL Tree/AVLTree.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ public class TreeNode<Key: Comparable, Payload> {
2929
internal var leftChild: Node?
3030
internal var rightChild: Node?
3131
fileprivate var height: Int
32-
weak fileprivate var parent: Node?
32+
fileprivate weak var parent: Node?
3333

3434
public init(key: Key, payload: Payload?, leftChild: Node?, rightChild: Node?, parent: Node?, height: Int) {
3535
self.key = key

AVL Tree/Images/RotationStep0.jpg

21.7 KB
Loading

AVL Tree/Images/RotationStep1.jpg

12.8 KB
Loading

AVL Tree/Images/RotationStep2.jpg

13 KB
Loading

AVL Tree/Images/RotationStep3.jpg

12.7 KB
Loading

AVL Tree/README.markdown

Lines changed: 28 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -43,20 +43,44 @@ The difference between the heights of the left and right subtrees is called the
4343
If after an insertion or deletion the balance factor becomes greater than 1, then we need to re-balance this part of the AVL tree. And that is done with rotations.
4444

4545
## Rotations
46-
4746
Each tree node keeps track of its current balance factor in a variable. After inserting a new node, we need to update the balance factor of its parent node. If that balance factor becomes greater than 1, we "rotate" part of that tree to restore the balance.
4847

49-
TODO: describe with pictures how these rotations work
48+
![Rotation0](Images/RotationStep0.jpg)
49+
50+
For the rotation we're using the terminology:
51+
* *Root* - the parent not of the subtrees that will be rotated;
52+
* *Pivot* - the node that will become parent (basically will be on the *Root*'s position) after rotation;
53+
* *RotationSubtree* - subtree of the *Pivot* upon the side of rotation
54+
* *OppositeSubtree* - subtree of the *Pivot* opposite the side of rotation
55+
56+
Let take an example of balancing the unbalanced tree using *Right* (clockwise direction) rotation:
57+
58+
![Rotation1](Images/RotationStep1.jpg) ![Rotation2](Images/RotationStep2.jpg) ![Rotation3](Images/RotationStep3.jpg)
59+
60+
The steps of rotation could be described by following:
61+
62+
1. Assign the *RotationSubtree* as a new *OppositeSubtree* for the *Root*;
63+
2. Assign the *Root* as a new *RotationSubtree* for the *Pivot*;
64+
3. Check the final result
65+
66+
67+
In pseudocode the algorithm above could be written as follows:
68+
```
69+
Root.OS = Pivot.RS
70+
Pivot.RS = Root
71+
Root = Pivot
72+
```
5073

51-
Insertion never needs more than 2 rotations. Removal might require up to *log(n)* rotations.
74+
This is a constant time operation - __O(1)__
75+
Insertion never needs more than 2 rotations. Removal might require up to __log(n)__ rotations.
5276

5377
## The code
5478

5579
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.
5680

5781
> **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.
5882
59-
The interesting bits are in the `balance()` method which is called after inserting or deleting a node.
83+
The interesting bits are in the `balance()` method which is called after inserting or deleting a node.
6084

6185
## See also
6286

Array2D/README.markdown

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,14 +30,14 @@ let myCookie = cookies[3][6]
3030
Actually, you could create the array in a single line of code, like so:
3131

3232
```swift
33-
var cookies = [[Int]](count: 9, repeatedValue: [Int](count: 7, repeatedValue: 0))
33+
var cookies = [[Int]](repeating: [Int](repeating: 0, count: 7), count: 9)
3434
```
3535

3636
but that's just ugly. To be fair, you can hide the ugliness in a helper function:
3737

3838
```swift
3939
func dim<T>(count: Int, _ value: T) -> [T] {
40-
return [T](count: count, repeatedValue: value)
40+
return [T](repeating: value, count: count)
4141
}
4242
```
4343

@@ -72,7 +72,7 @@ public struct Array2D<T> {
7272
public init(columns: Int, rows: Int, initialValue: T) {
7373
self.columns = columns
7474
self.rows = rows
75-
array = .init(count: rows*columns, repeatedValue: initialValue)
75+
array = .init(repeating: initialValue, count: rows*columns)
7676
}
7777

7878
public subscript(column: Int, row: Int) -> T {
-238 Bytes
Loading

Binary Search Tree/README.markdown

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -412,7 +412,7 @@ To see how this works, take the following tree:
412412

413413
![Example](Images/MinimumMaximum.png)
414414

415-
For example, if we look at node `10`, its leftmost descendent is `6`. We get there by following all the `left` pointers until there are no more left children to look at. The leftmost descendent of the root node `7` is `1`. Therefore, `1` is the minimum value in the entire tree.
415+
For example, if we look at node `10`, its leftmost descendent is `7`. We get there by following all the `left` pointers until there are no more left children to look at. The leftmost descendent of the root node `6` is `1`. Therefore, `1` is the minimum value in the entire tree.
416416

417417
We won't need it for deleting, but for completeness' sake, here is the opposite of `minimum()`:
418418

@@ -426,7 +426,7 @@ We won't need it for deleting, but for completeness' sake, here is the opposite
426426
}
427427
```
428428

429-
It returns the rightmost descendent of the node. We find it by following `right` pointers until we get to the end. In the above example, the rightmost descendent of node `2` is `5`. The maximum value in the entire tree is `11`, because that is the rightmost descendent of the root node `7`.
429+
It returns the rightmost descendent of the node. We find it by following `right` pointers until we get to the end. In the above example, the rightmost descendent of node `2` is `5`. The maximum value in the entire tree is `11`, because that is the rightmost descendent of the root node `6`.
430430

431431
Finally, we can write the code that removes a node from the tree:
432432

@@ -500,11 +500,11 @@ if let node2 = tree.search(2) {
500500

501501
First you find the node that you want to remove with `search()` and then you call `remove()` on that object. Before the removal, the tree printed like this:
502502

503-
((1) <- 2 -> (5)) <- 7 -> ((9) <- 10)
503+
((1) <- 2 -> (5)) <- 6 -> ((9) <- 10)
504504

505505
But after `remove()` you get:
506506

507-
((1) <- 5) <- 7 -> ((9) <- 10)
507+
((1) <- 5) <- 6 -> ((9) <- 10)
508508

509509
As you can see, node `5` has taken the place of `2`.
510510

Binary Search Tree/Solution 2/BinarySearchTree.playground/Contents.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
//: Playground - noun: a place where people can play
22

33
// Each time you insert something, you get back a completely new tree.
4-
var tree = BinarySearchTree.Leaf(7)
4+
var tree = BinarySearchTree.leaf(7)
55
tree = tree.insert(newValue: 2)
66
tree = tree.insert(newValue: 5)
77
tree = tree.insert(newValue: 10)

Binary Search Tree/Solution 2/BinarySearchTree.playground/Sources/BinarySearchTree.swift

Lines changed: 27 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -4,25 +4,25 @@
44
The tree is immutable. Any insertions or deletions will create a new tree.
55
*/
66
public enum BinarySearchTree<T: Comparable> {
7-
case Empty
8-
case Leaf(T)
9-
indirect case Node(BinarySearchTree, T, BinarySearchTree)
7+
case empty
8+
case leaf(T)
9+
indirect case node(BinarySearchTree, T, BinarySearchTree)
1010

1111
/* How many nodes are in this subtree. Performance: O(n). */
1212
public var count: Int {
1313
switch self {
14-
case .Empty: return 0
15-
case .Leaf: return 1
16-
case let .Node(left, _, right): return left.count + 1 + right.count
14+
case .empty: return 0
15+
case .leaf: return 1
16+
case let .node(left, _, right): return left.count + 1 + right.count
1717
}
1818
}
1919

2020
/* Distance of this node to its lowest leaf. Performance: O(n). */
2121
public var height: Int {
2222
switch self {
23-
case .Empty: return 0
24-
case .Leaf: return 1
25-
case let .Node(left, _, right): return 1 + max(left.height, right.height)
23+
case .empty: return 0
24+
case .leaf: return 1
25+
case let .node(left, _, right): return 1 + max(left.height, right.height)
2626
}
2727
}
2828

@@ -32,21 +32,21 @@ public enum BinarySearchTree<T: Comparable> {
3232
*/
3333
public func insert(newValue: T) -> BinarySearchTree {
3434
switch self {
35-
case .Empty:
36-
return .Leaf(newValue)
35+
case .empty:
36+
return .leaf(newValue)
3737

38-
case .Leaf(let value):
38+
case .leaf(let value):
3939
if newValue < value {
40-
return .Node(.Leaf(newValue), value, .Empty)
40+
return .node(.leaf(newValue), value, .empty)
4141
} else {
42-
return .Node(.Empty, value, .Leaf(newValue))
42+
return .node(.empty, value, .leaf(newValue))
4343
}
4444

45-
case .Node(let left, let value, let right):
45+
case .node(let left, let value, let right):
4646
if newValue < value {
47-
return .Node(left.insert(newValue: newValue), value, right)
47+
return .node(left.insert(newValue: newValue), value, right)
4848
} else {
49-
return .Node(left, value, right.insert(newValue: newValue))
49+
return .node(left, value, right.insert(newValue: newValue))
5050
}
5151
}
5252
}
@@ -57,11 +57,11 @@ public enum BinarySearchTree<T: Comparable> {
5757
*/
5858
public func search(x: T) -> BinarySearchTree? {
5959
switch self {
60-
case .Empty:
60+
case .empty:
6161
return nil
62-
case .Leaf(let y):
62+
case .leaf(let y):
6363
return (x == y) ? self : nil
64-
case let .Node(left, y, right):
64+
case let .node(left, y, right):
6565
if x < y {
6666
return left.search(x: x)
6767
} else if y < x {
@@ -82,11 +82,11 @@ public enum BinarySearchTree<T: Comparable> {
8282
public func minimum() -> BinarySearchTree {
8383
var node = self
8484
var prev = node
85-
while case let .Node(next, _, _) = node {
85+
while case let .node(next, _, _) = node {
8686
prev = node
8787
node = next
8888
}
89-
if case .Leaf = node {
89+
if case .leaf = node {
9090
return node
9191
}
9292
return prev
@@ -98,11 +98,11 @@ public enum BinarySearchTree<T: Comparable> {
9898
public func maximum() -> BinarySearchTree {
9999
var node = self
100100
var prev = node
101-
while case let .Node(_, _, next) = node {
101+
while case let .node(_, _, next) = node {
102102
prev = node
103103
node = next
104104
}
105-
if case .Leaf = node {
105+
if case .leaf = node {
106106
return node
107107
}
108108
return prev
@@ -112,9 +112,9 @@ public enum BinarySearchTree<T: Comparable> {
112112
extension BinarySearchTree: CustomDebugStringConvertible {
113113
public var debugDescription: String {
114114
switch self {
115-
case .Empty: return "."
116-
case .Leaf(let value): return "\(value)"
117-
case .Node(let left, let value, let right):
115+
case .empty: return "."
116+
case .leaf(let value): return "\(value)"
117+
case .node(let left, let value, let right):
118118
return "(\(left.debugDescription) <- \(value) -> \(right.debugDescription))"
119119
}
120120
}

0 commit comments

Comments
 (0)