Skip to content

Commit c694f88

Browse files
committed
Update AVLExp.md
1 parent bc30328 commit c694f88

File tree

1 file changed

+21
-8
lines changed

1 file changed

+21
-8
lines changed

src/algorithms/explanations/AVLExp.md

Lines changed: 21 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ no matter what the order of the input data.
99

1010
A **binary tree** is either is either empty (`Empty`) or else it
1111
it has a root node and two subtrees (which are binary trees).
12-
The root node `t` has a key, `t.key`. Ordinarily it would also
12+
The root node `t` has a key `t.key`. Ordinarily it would also
1313
hold other data (`t.data`), which the user would like to find by
1414
searching for the key. Since this attribute has no impact on
1515
how insertion and search take place, we disregard it here.
@@ -43,7 +43,7 @@ either (1) both left subtrees or (2) both right subtrees.
4343

4444
## The left-left case
4545

46-
If the new key was added to the left child of the left child (the
46+
If the new key was added to the left child of a left child (the
4747
*left-left* case), the balance can be
4848
restored with a single _**Right Rotation**_ operation.
4949

@@ -53,9 +53,14 @@ Note that the imbalance may be located further up the tree than the immediate gr
5353
*Note that all diagrams should hae nodes in BST order, and where the subtrees are not necessarily single nodes, represent them as subtree-triangles*
5454
*Have arrow showing which node is not in blanace*
5555

56-
In the diagrams below, t6 is the node where the imbalance is noted,
57-
XXXX To Here 7:30 Wedn
56+
In the diagrams below, t6 is the node where the imbalance is noted.
5857

58+
As shown in the diagram below, t6 is the node at which the imbalance has been noted, and t2 is its left child. The clockwise rotation to restore balance makes t2 the parent of t6, while t6 is the *right* child of t2. Additionally, since t2 already had t4 as its right child, t4 is moved to become the left child of t6. Note that the BST invariant is preserved: t4, is bigger than t2, and is also smaller than t6. The rotation reduces the distance from the root to t1 (where the new node was added), so the tree is now
59+
60+
**I think there is something wrong with this diagram and the explanation**
61+
**unless t4 is empty, then the tree was already unbalance before 1 was added??**
62+
63+
5964

6065
, as explained in the diagram The 6 and 4 nodes and the edge between them rotate clockwise, and
6166
the 5 node changes parents from 4 to 6. This reduces the distance from
@@ -73,15 +78,23 @@ Test comment
7378

7479
[This shouldn't be seen if the comment signal is correct]: #)
7580

81+
**Check whether the numbering here is still the same in the animation**
82+
**Note I have added a link to the parent of t6 -- check, since the pointer to the left child of this anonymous node needs to be changed, have we given it a number?**
83+
84+
**Lee - is t4 empty in this case? If not, then the tree would already have been unbalanced previously, when t4 was added??**
7685

7786
```
78-
6 2
87+
/ /
88+
t6 t2
7989
/ \ Right Rotation / \
80-
2 7 - - - - - - - > 1 6
90+
t2 7 - - - - - - - > 1 t6
8191
/ \ < - - - - - - - / \
82-
1 4 Left Rotation 4 7
92+
1 t4 Left Rotation t4 7
8393
```
8494

95+
**9 Jan 4PM I haven't gone beyond this**
96+
97+
8598
## The right-right case
8699

87100
The right-right case is the exact opposite. If the tree on the right in
@@ -109,7 +122,7 @@ making the tree unbalanced) are moved closer to the root.
109122
Trees rooted at 1, 3, 5 and 7 are not affected, except the distances from
110123
the root of 3, 5 and 7 are changed by one, affecting the overall balance.
111124

112-
## right-left case (double rotation):
125+
## The right-left case (double rotation):
113126

114127
If the new key was added to the left child of the right child (the
115128
right-left case) and the resulting tree is too unbalanced, it is a mirror

0 commit comments

Comments
 (0)