Skip to content

Commit 41fa30f

Browse files
committed
Commit to check Diagram
AVL Background, revised up to rotation explanations. Committed to check diagram of left-left from Greek for Geeks.
1 parent a149057 commit 41fa30f

File tree

3 files changed

+125
-6
lines changed

3 files changed

+125
-6
lines changed
Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
# AVL Trees
2+
3+
AVL trees are binary search trees that have additional rules to keep them balanced.
4+
In an AVL tree search will always be O(log n), no matter what the input order.
5+
6+
## Binary Search Trees
7+
8+
A **binary tree** is either is either empty (Empty) or else it
9+
it has a root node and two subtrees (which are binary trees).
10+
The root node `t` has a key, `t.key`. Ordinarily it would also
11+
hold other data (`t.data`), which the user would like to find by
12+
searching for the key. Since this attribute has no impact on
13+
how insertion and search take place, we disregard it here.
14+
Note that a newly inserted node will always appear as a leaf
15+
in the tree.
16+
In a **binary search tree (BST)** the BST invariant is always maintained: for each
17+
subtree t, with root key t.key, the left subtree, t.left,
18+
contains no node with key greater than k, and the right subtree,
19+
t.right, contains no node with key smaller than k.
20+
21+
## The left-left case
22+
23+
If the new key was added to the left child of the left child (the
24+
left-left case) and the resulting tree is too unbalanced, the balance can be
25+
restored with a "Right rotation" operation, as explained in the diagram
26+
below. The 6 and 4 nodes and the edge between them rotate clockwise, and
27+
the 5 node changes parents from 4 to 6. This reduces the distance from
28+
the root to the 1 (where the new node was added), restoring the balance
29+
(the distance to the node rooted at 7 is increased but this does not
30+
cause the AVL tree balance condition to be violated). Right rotation is
31+
done by calling rightRotate(t6), where t6 is the tree rooted at 6.
32+
33+
```
34+
6 2
35+
/ \ Right Rotation / \
36+
2 7 - - - - - - - > 1 6
37+
/ \ < - - - - - - - / \
38+
1 4 Left Rotation 4 7
39+
```
40+
41+
## The right-right case
42+
43+
The right-right case is the exact opposite. If the tree on the right in
44+
the diagram above is too unbalanced due to insertion into the subtree
45+
rooted at 7, we can call rightRotate(t2) to lift that subtree and lower
46+
the 1 subtree.
47+
48+
## The left-right case (double rotation)
49+
50+
If the new key was added to the right child of the left child (the
51+
left-right case) and the resulting tree is too unbalanced, the balance can be
52+
restored with a left rotation at node 2 followed by a right rotation at
53+
node 6.
54+
```
55+
6 Rotate 6 Rotate 4
56+
/ \ left at 2 / \ right at 6 / \
57+
2 7 - - - - - > 4 7 - - - - - > 2 6
58+
/ \ / \ / \ / \
59+
1 4 2 5 1 3 5 7
60+
/ \ / \
61+
3 5 1 3
62+
```
63+
Nodes in the subtree rooted at 4 (where the extra element was added,
64+
making the tree unbalanced) are moved closer to the root.
65+
Trees rooted at 1, 3, 5 and 7 are not affected, except the distances from
66+
the root of 3, 5 and 7 are changed by one, affecting the overall balance.
67+
68+
## right-left case (double rotation):
69+
70+
If the new key was added to the left child of the right child (the
71+
right-left case) and the resulting tree is too unbalanced, it is a mirror
72+
image of the left-right case:
73+
74+
```
75+
2 Rotate 2 Rotate 4
76+
/ \ right at 6 / \ left at 2 / \
77+
1 6 - - - - - > 1 4 - - - - - > 2 6
78+
/ \ / \ / \ / \
79+
4 7 3 6 1 3 5 7
80+
/ \ / \
81+
3 5 5 7
82+
```

src/algorithms/explanations/AVLExp.md

Lines changed: 43 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,66 @@
11
# AVL Trees
22

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
410
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
713
searching for the key. Since this attribute has no impact on
814
how insertion and search take place, we disregard it here.
915
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
1120
subtree t, with root key t.key, the left subtree, t.left,
1221
contains no node with key greater than k, and the right subtree,
1322
t.right, contains no node with key smaller than k.
1423

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+
1543
## The left-left case
1644

1745
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
2055
below. The 6 and 4 nodes and the edge between them rotate clockwise, and
2156
the 5 node changes parents from 4 to 6. This reduces the distance from
2257
the root to the 1 (where the new node was added), restoring the balance
2358
(the distance to the node rooted at 7 is increased but this does not
2459
cause the AVL tree balance condition to be violated). Right rotation is
2560
done by calling rightRotate(t6), where t6 is the tree rooted at 6.
2661

62+
63+
2764
```
2865
6 2
2966
/ \ Right Rotation / \
Loading

0 commit comments

Comments
 (0)