You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: AVL Tree/README.markdown
+5-5
Original file line number
Diff line number
Diff line change
@@ -8,15 +8,15 @@ This is an example of an unbalanced tree:
8
8
9
9

10
10
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.
12
12
13
13
A balanced version of that tree would look like this:
14
14
15
15

16
16
17
17
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.
18
18
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. ;-)
20
20
21
21
## Introducing the AVL tree
22
22
@@ -32,7 +32,7 @@ As mentioned, in an AVL tree a node is balanced if its left and right subtree ha
32
32
33
33

34
34
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:
36
36
37
37

38
38
@@ -52,7 +52,7 @@ Insertion never needs more than 2 rotations. Removal might require up to *log(n)
52
52
53
53
## The code
54
54
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.
56
56
57
57
> **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.
58
58
@@ -66,6 +66,6 @@ The interesting bits are in the following methods:
66
66
67
67
[AVL tree on Wikipedia](https://en.wikipedia.org/wiki/AVL_tree)
68
68
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.
70
70
71
71
*Written for Swift Algorithm Club by Mike Taghavi and Matthijs Hollemans*
Copy file name to clipboardExpand all lines: Algorithm Design.markdown
+3-3
Original file line number
Diff line number
Diff line change
@@ -4,17 +4,17 @@ What to do when you're faced with a new problem and you need to find an algorith
4
4
5
5
### Is it similar to another problem?
6
6
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?
8
8
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/).)
10
10
11
11
### It's OK to start with brute force
12
12
13
13
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.
14
14
15
15
Once you have a brute force implementation you can use that to verify that any improvements you come up with are correct.
16
16
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!
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.
63
63
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:
65
65
66
66
```swift
67
67
publicstructArray2D<T> {
@@ -100,8 +100,14 @@ Thanks to the `subscript` function, you can do the following to retrieve an obje
100
100
let myCookie = cookies[column, row]
101
101
```
102
102
103
+
Or change it:
104
+
105
+
```swift
106
+
cookies[column, row] = newCookie
107
+
```
108
+
103
109
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.
104
110
105
111
And that's all there is to it.
106
112
107
-
*Written by Matthijs Hollemans*
113
+
*Written for Swift Algorithm Club by Matthijs Hollemans*
0 commit comments