|
1 | 1 | # AVL Trees
|
2 | 2 |
|
3 | 3 | ## Background
|
4 |
| -Is the fastest way to search for data to store them in an array, sort them and perform binary search? No. This will |
5 |
| -incur minimally O(nlogn) sorting cost, and O(n) cost per insertion to maintain sorted order. |
| 4 | +Say you want to search of a data in an array. If the array were sorted, lucky you! You can do it with binary search in |
| 5 | +`O(logn)` time. But if the array weren't sorted, you can't avoid that `O(n)` linear search loop. Now, one idea is to |
| 6 | +first sort the array, and incur a 1-time cost of `O(n)` and subsequent search operations can enjoy that `O(logn)` query |
| 7 | +cost. This is all gucci, but it assumes that there will be no additional data streaming in. If incoming data is not |
| 8 | +infrequent, you'll have to incur `O(n)` insertion cost each time to maintain sorted order, and this can undermine |
| 9 | +performance as a whole. If only there were some structure that allows us to enjoy `O(logn)` operations across.. |
6 | 10 |
|
7 | 11 | We have seen binary search trees (BSTs), which always maintains data in sorted order. This allows us to avoid the
|
8 | 12 | overhead of sorting before we search. However, we also learnt that unbalanced BSTs can be incredibly inefficient for
|
9 |
| -insertion, deletion and search operations, which are O(h) in time complexity (in the case of degenerate trees, |
10 |
| -operations can go up to O(n)). |
| 13 | +insertion, deletion and search operations, which are O(height) in time complexity (in the case of degenerate trees, |
| 14 | +think of a linked list, operations can go up to O(n)). |
11 | 15 |
|
12 | 16 | Here we discuss a type of self-balancing BST, known as the AVL tree, that avoids the worst case O(n) performance
|
13 | 17 | across the operations by ensuring careful updating of the tree's structure whenever there is a change
|
|
0 commit comments