You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: src/algorithms/explanations/AVLExp.md
+15-12Lines changed: 15 additions & 12 deletions
Original file line number
Diff line number
Diff line change
@@ -1,8 +1,9 @@
1
1
# AVL Trees
2
2
3
-
AVL trees are **self-balancing** binary search trees.
3
+
An AVL tree is **self-balancing** binary search tree.
4
+
4
5
Because the AVL tree is always balanced, search will be *O(log n)*,
5
-
no matter what the order of the input data.
6
+
no matter what the order of the input data.
6
7
7
8
### Binary Search Trees
8
9
@@ -23,21 +24,21 @@ for each
23
24
24
25
### AVL tree balancing
25
26
26
-
The AVL tree preserves the balance of the tree by checking after every insertion to detect
27
+
The AVL tree preserves the balance of the tree by (1) checking after every insertion to detect
27
28
when the tree has become unbalanced
28
-
and performing one or more rotation operations to restore balance when necessary.
29
+
and (2) performing one or more rotation operations to restore balance when necessary.
29
30
Unbalanced is defined as a difference of more than `1` between the heights of the left and right subtrees.
30
31
31
-
(Here **maybe insert small pictures, thumbnails?** to show -- unbalanced at gp of new node,
32
+
(Here **MAYBE insert small pictures, thumbnails?** to show -- unbalanced at gp of new node,
32
33
and also balanced at gp but unblanaced at ggp.)
33
34
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.
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.
36
37
37
38
### Zig-zig case
38
39
39
-
In the *zig-zig*case, the child and grandchild nodes of the unbalanced are either both
40
-
left subtrees or both right subtrees.
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.
41
42
42
43
43
44
## The left-left case
@@ -46,9 +47,11 @@ If the new key was added to the left child of the left child (the
46
47
*left-left* case), the balance can be
47
48
restored with a single _**Right Rotation**_ operation.
48
49
49
-
**This is not quite correct, as I think unbalance can occur at the great-great grandparent
50
-
without occuring at the parent**
50
+
Note that the imbalance may be located further up the tree than the immediate grandparent of the newly inserted node:
51
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*
52
55
53
56
54
57
, as explained in the diagram
@@ -59,7 +62,7 @@ the root to the 1 (where the new node was added), restoring the balance
59
62
cause the AVL tree balance condition to be violated). Right rotation is
60
63
done by calling rightRotate(t6), where t6 is the tree rooted at 6.
61
64
62
-
Picture courtesy of Greek for Geeks
65
+
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.
0 commit comments