|
1 | 1 | # AVL Trees
|
2 | 2 |
|
3 |
| -A binary tree is either is either empty (Empty) or else it |
| 3 | +AVL trees are **self-balancing** binary search trees. |
| 4 | +Because the AVL tree is always balanced, search will be *O(log n)*, |
| 5 | + no matter what the order of the input data. |
| 6 | + |
| 7 | +### Binary Search Trees |
| 8 | + |
| 9 | +A **binary tree** is either is either empty (`Empty`) or else it |
4 | 10 | it has a root node and two subtrees (which are binary trees).
|
5 |
| - The root node t has a key, t.key. Ordinarily it would also |
6 |
| - hold other data (t.data), which the user would like to find by |
| 11 | + The root node `t` has a key, `t.key`. Ordinarily it would also |
| 12 | + hold other data (`t.data`), which the user would like to find by |
7 | 13 | searching for the key. Since this attribute has no impact on
|
8 | 14 | how insertion and search take place, we disregard it here.
|
9 | 15 | Note that a newly inserted node will always appear as a leaf
|
10 |
| - in the tree. The BST invariant is always maintained: for each |
| 16 | + in the tree. |
| 17 | + |
| 18 | +In a **binary search tree (BST)** the BST invariant is always maintained:\ |
| 19 | +for each |
11 | 20 | subtree t, with root key t.key, the left subtree, t.left,
|
12 | 21 | contains no node with key greater than k, and the right subtree,
|
13 | 22 | t.right, contains no node with key smaller than k.
|
14 | 23 |
|
| 24 | +### AVL tree balancing |
| 25 | + |
| 26 | +The AVL tree preserves the balance of the tree by checking after every insertion to detect |
| 27 | +when the tree has become unbalanced |
| 28 | +and performing one or more rotation operations to restore balance when necessary. |
| 29 | +Unbalanced is defined as a difference of more than `1` between the heights of the left and right subtrees. |
| 30 | + |
| 31 | +(Here **maybe insert small pictures, thumbnails?** to show -- unbalanced at gp of new node, |
| 32 | +and also balanced at gp but unblanaced at ggp.) |
| 33 | + |
| 34 | +A (temporarily) unbalanced tree takes on one of two configurations, *zig-zag* or *zig-zig*, and the |
| 35 | +sequence of rotations depends on the configuration around the node where the unbalance is detected. |
| 36 | + |
| 37 | +### Zig-zig case |
| 38 | + |
| 39 | +In the *zig-zig* case, the child and grandchild nodes of the unbalanced are either both |
| 40 | +left subtrees or both right subtrees. |
| 41 | + |
| 42 | + |
15 | 43 | ## The left-left case
|
16 | 44 |
|
17 | 45 | If the new key was added to the left child of the left child (the
|
18 |
| -left-left case) and the resulting tree is too unbalanced, the balance can be |
19 |
| -restored with a "Right rotation" operation, as explained in the diagram |
| 46 | +*left-left* case), the balance can be |
| 47 | +restored with a single _**Right Rotation**_ operation. |
| 48 | + |
| 49 | +**This is not quite correct, as I think unbalance can occur at the great-great grandparent |
| 50 | +without occuring at the parent** |
| 51 | + |
| 52 | + |
| 53 | + |
| 54 | +, as explained in the diagram |
20 | 55 | below. The 6 and 4 nodes and the edge between them rotate clockwise, and
|
21 | 56 | the 5 node changes parents from 4 to 6. This reduces the distance from
|
22 | 57 | the root to the 1 (where the new node was added), restoring the balance
|
23 | 58 | (the distance to the node rooted at 7 is increased but this does not
|
24 | 59 | cause the AVL tree balance condition to be violated). Right rotation is
|
25 | 60 | done by calling rightRotate(t6), where t6 is the tree rooted at 6.
|
26 | 61 |
|
| 62 | + |
| 63 | + |
27 | 64 | ```
|
28 | 65 | 6 2
|
29 | 66 | / \ Right Rotation / \
|
|
0 commit comments