Skip to content

Commit 53c9b1e

Browse files
committed
Merge branch '2025linda' into 2024fixes
AVL diagram image broken
2 parents f0684bb + 56e6455 commit 53c9b1e

File tree

3 files changed

+133
-6
lines changed

3 files changed

+133
-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: 51 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,74 @@
11
# AVL Tree
22

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
411
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
714
searching for the key. Since this attribute has no impact on
815
how insertion and search take place, we disregard it here.
916
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
1121
subtree t, with root key t.key, the left subtree, t.left,
1222
contains no node with key greater than k, and the right subtree,
1323
t.right, contains no node with key smaller than k.
1424

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+
1544
## The left-left case
1645

1746
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
2058
below. The 6 and 4 nodes and the edge between them rotate clockwise, and
2159
the 5 node changes parents from 4 to 6. This reduces the distance from
2260
the root to the 1 (where the new node was added), restoring the balance
2361
(the distance to the node rooted at 7 is increased but this does not
2462
cause the AVL tree balance condition to be violated). Right rotation is
2563
done by calling rightRotate(t6), where t6 is the tree rooted at 6.
2664

65+
![Alt text if image doesn't open: AVL-left-left](images/AVL/AVL-left-left.jpg){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+
2772
```
2873
6 2
2974
/ \ Right Rotation / \
Loading

0 commit comments

Comments
 (0)