|
1 |
| -# Heap |
| 1 | +# Binary Search Tree (BST) |
2 | 2 |
|
3 |
| -Written for the Swift Algorithm Club by [Nicolas Ameghino](http://www.github.com/nameghino). |
4 |
| - |
5 |
| -## Intro |
6 |
| - |
7 |
| -A binary search tree is a special kind of binary tree (a tree in which a node only has two children) that performs insertions in a special way such that the tree is always sorted. |
| 3 | +A binary search tree is a special kind of [binary tree](../Binary Tree/) (a tree in which a node only has two children) that performs insertions in a special way such that the tree is always sorted. |
8 | 4 |
|
9 | 5 | ## "Always sorted" property
|
10 | 6 |
|
11 |
| -When performing an insertion, the tree will check the value of the *key* for a node and compare it to the *key* for the soon-to-be-inserted value. It will go down the tree until there's an empty leaf following the rule that if the new value is smaller than the existant one, it will be inserted to the left of the existant node. Otherwise, it will be to the right of it. |
| 7 | +When performing an insertion, the tree will check the value of the *key* for a node and compare it to the *key* for the soon-to-be-inserted value. It will go down the tree until there's an empty leaf, following the rule that if the new value is smaller than the existant one, it will be inserted to the left of the existant node. Otherwise, it will be to the right of it. |
12 | 8 |
|
13 | 9 | Let's look at an example:
|
14 | 10 |
|
15 | 11 | ```
|
16 |
| - (7) |
17 |
| - / \ |
18 |
| - (2) (10) |
19 |
| - / \ |
20 |
| -(1) (5) |
| 12 | + (7) |
| 13 | + / \ |
| 14 | + (2) (10) |
| 15 | + / \ |
| 16 | +(1) (5) |
21 | 17 | ```
|
22 | 18 |
|
23 |
| -Say we want to insert the value 9: |
| 19 | +Notice how each left child is smaller than its parent node, and each right child is greater than its parent node. For example, `2` is smaller than `7` so it goes on the left; `5` is greater than `2` so it goes on the right. |
| 20 | + |
| 21 | +Say we want to insert the new value `9`: |
24 | 22 |
|
25 |
| -We'd start at the root of the tree (the node with the value 7, in this case), and compare it to the new value (9 for now). |
26 |
| -We see that `9 > 7`, so we go down the *right* branch of that node. Here, we recurse on the *right* node, and peform the same procedure: check against the key, but this time `9 < 10`, so we go down the *left* branch. |
27 |
| -We've now arrived to a point in which there are no more values to compare with, so the value 9 is inserted at that ___location, going first right and then left from the root, to have a tree that now looks like this: |
| 23 | +- We start at the root of the tree (the node with the value `7`) and compare it to the new value `9`. |
| 24 | +- We see that `9 > 7`, so we go down the *right* branch of that node and perform the same procedure: check against the key, but this time `9 < 10`, so we go down the *left* branch. |
| 25 | +- We've now arrived to a point in which there are no more values to compare with, so the value `9` is inserted at that ___location, going first right and then left from the root. |
| 26 | + |
| 27 | +The tree now looks like this: |
28 | 28 |
|
29 | 29 | ```
|
30 |
| - (7) |
31 |
| - / \ |
32 |
| - (2) (10) |
33 |
| - / \ / |
34 |
| -(1) (5) (9) |
| 30 | + (7) |
| 31 | + / \ |
| 32 | + (2) (10) |
| 33 | + / \ / |
| 34 | +(1) (5) (9) |
35 | 35 | ```
|
36 | 36 |
|
37 | 37 | By following this simple rule, we keep the tree sorted in a way that, whenever we query it, we can quickly check if a value is in the tree or get such value.
|
38 | 38 |
|
| 39 | +## Searching the tree |
| 40 | + |
| 41 | +[TBD: explain how you'd search the tree and why it is fast; compare with the binary search article] |
| 42 | + |
39 | 43 | ## Deletion
|
40 | 44 |
|
41 | 45 | TBD
|
42 | 46 |
|
| 47 | +## The code |
| 48 | + |
| 49 | +[TBD: explain how you've implemented the tree in Swift] |
| 50 | + |
43 | 51 | ## Disadvantages
|
44 | 52 |
|
45 | 53 | If the tree is not *balanced*, that is, one branch is significantly longer than the other, then queries would suffer performance issues, having to check more values on one branch than it would if the tree were optimally balanced.
|
| 54 | + |
46 | 55 | Balanced trees guarantee that the longest branch of the tree will be at most 1 node longer than the rest, otherwise the tree will balance itself on insertion.
|
47 | 56 |
|
48 |
| -Another issue arises from the insertion of increasing or decreasing values, in which the tree would be massively unbalanced with just one long branch, and all children nodes branching in the same direction. |
| 57 | +Another issue arises from the insertion of increasing or decreasing values, in which the tree would be massively unbalanced with just one long branch, and all child nodes branching in the same direction. |
49 | 58 |
|
50 | 59 | ## See also
|
51 | 60 |
|
52 | 61 | [Binary Search Tree on Wikipedia](https://en.wikipedia.org/wiki/Binary_search_tree)
|
| 62 | + |
| 63 | +*Written for the Swift Algorithm Club by [Nicolas Ameghino](http://www.github.com/nameghino).* |
0 commit comments