|
1 | 1 | # AVL Tree
|
2 | 2 |
|
3 |
| -A binary tree is either is either empty (Empty) or else it |
| 3 | +An AVL tree is **self-balancing** binary search tree. |
| 4 | + |
| 5 | +Because the AVL tree is always balanced, search will be *O(log n)*, |
| 6 | +no matter what the order of the input data. |
| 7 | + |
| 8 | +### Binary Search Trees |
| 9 | + |
| 10 | +A **binary tree** is either is either empty (`Empty`) or else it |
4 | 11 | 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 |
| 12 | + The root node `t` has a key, `t.key`. Ordinarily it would also |
| 13 | + hold other data (`t.data`), which the user would like to find by |
7 | 14 | searching for the key. Since this attribute has no impact on
|
8 | 15 | how insertion and search take place, we disregard it here.
|
9 | 16 | Note that a newly inserted node will always appear as a leaf
|
10 |
| - in the tree. The BST invariant is always maintained: for each |
| 17 | + in the tree. |
| 18 | + |
| 19 | +In a **binary search tree (BST)** the BST invariant is always maintained:\ |
| 20 | +for each |
11 | 21 | subtree t, with root key t.key, the left subtree, t.left,
|
12 | 22 | contains no node with key greater than k, and the right subtree,
|
13 | 23 | t.right, contains no node with key smaller than k.
|
14 | 24 |
|
| 25 | +### AVL tree balancing |
| 26 | + |
| 27 | +The AVL tree preserves the balance of the tree by (1) checking after every insertion to detect |
| 28 | +when the tree has become unbalanced |
| 29 | +and (2) performing one or more rotation operations to restore balance when necessary. |
| 30 | +Unbalanced is defined as a difference of more than `1` between the heights of the left and right subtrees. |
| 31 | + |
| 32 | +(Here **MAYBE insert small pictures, thumbnails?** to show -- unbalanced at gp of new node, |
| 33 | +and also balanced at gp but unblanaced at ggp.) |
| 34 | + |
| 35 | +A temporarily unbalanced AVL tree takes on one of two configurations, *zig-zag* or *zig-zig*. The |
| 36 | +sequence of rotations depends on the configuration around the node where the imbalance is detected. |
| 37 | + |
| 38 | +### Zig-zig case |
| 39 | + |
| 40 | +The *zig-zig* configuration has two mirror-image cases: the child and grandchild nodes or subtrees of the unbalanced node are |
| 41 | +either (1) both left subtrees or (2) both right subtrees. |
| 42 | + |
| 43 | + |
15 | 44 | ## The left-left case
|
16 | 45 |
|
17 | 46 | 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 |
| 47 | +*left-left* case), the balance can be |
| 48 | +restored with a single _**Right Rotation**_ operation. |
| 49 | + |
| 50 | +Note that the imbalance may be located further up the tree than the immediate grandparent of the newly inserted node: |
| 51 | + |
| 52 | +**Insert very small diagrams showing (a) imbalance of the grandparent-parent and (b) imbalance of the great grandparent** |
| 53 | +*Note that all diagrams should hae nodes in BST order, and where the subtrees are not necessarily single nodes, represent them as subtree-triangles* |
| 54 | +*Have arrow showing which node is not in blanace* |
| 55 | + |
| 56 | + |
| 57 | +, as explained in the diagram |
20 | 58 | below. The 6 and 4 nodes and the edge between them rotate clockwise, and
|
21 | 59 | the 5 node changes parents from 4 to 6. This reduces the distance from
|
22 | 60 | the root to the 1 (where the new node was added), restoring the balance
|
23 | 61 | (the distance to the node rooted at 7 is increased but this does not
|
24 | 62 | cause the AVL tree balance condition to be violated). Right rotation is
|
25 | 63 | done by calling rightRotate(t6), where t6 is the tree rooted at 6.
|
26 | 64 |
|
| 65 | +{width=120,height=50} This picture is from Greek for Geeks, and is only a placeholder to show proof of concept inserting diagrams, and to check things like size and cropping. |
| 66 | + |
| 67 | + |
| 68 | + |
| 69 | + |
| 70 | + |
| 71 | + |
27 | 72 | ```
|
28 | 73 | 6 2
|
29 | 74 | / \ Right Rotation / \
|
|
0 commit comments