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
+21-8Lines changed: 21 additions & 8 deletions
Original file line number
Diff line number
Diff line change
@@ -9,7 +9,7 @@ no matter what the order of the input data.
9
9
10
10
A **binary tree** is either is either empty (`Empty`) or else it
11
11
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
13
13
hold other data (`t.data`), which the user would like to find by
14
14
searching for the key. Since this attribute has no impact on
15
15
how insertion and search take place, we disregard it here.
@@ -43,7 +43,7 @@ either (1) both left subtrees or (2) both right subtrees.
43
43
44
44
## The left-left case
45
45
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
47
47
*left-left* case), the balance can be
48
48
restored with a single _**Right Rotation**_ operation.
49
49
@@ -53,9 +53,14 @@ Note that the imbalance may be located further up the tree than the immediate gr
53
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
54
*Have arrow showing which node is not in blanace*
55
55
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.
58
57
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
+
59
64
60
65
, as explained in the diagram The 6 and 4 nodes and the edge between them rotate clockwise, and
61
66
the 5 node changes parents from 4 to 6. This reduces the distance from
@@ -73,15 +78,23 @@ Test comment
73
78
74
79
[This shouldn't be seen if the comment signal is correct]: #)
75
80
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??**
76
85
77
86
```
78
-
6 2
87
+
/ /
88
+
t6 t2
79
89
/ \ Right Rotation / \
80
-
2 7 - - - - - - - > 1 6
90
+
t2 7 - - - - - - - > 1 t6
81
91
/ \ < - - - - - - - / \
82
-
1 4 Left Rotation 4 7
92
+
1 t4 Left Rotation t4 7
83
93
```
84
94
95
+
**9 Jan 4PM I haven't gone beyond this**
96
+
97
+
85
98
## The right-right case
86
99
87
100
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.
109
122
Trees rooted at 1, 3, 5 and 7 are not affected, except the distances from
110
123
the root of 3, 5 and 7 are changed by one, affecting the overall balance.
111
124
112
-
## right-left case (double rotation):
125
+
## The right-left case (double rotation):
113
126
114
127
If the new key was added to the left child of the right child (the
115
128
right-left case) and the resulting tree is too unbalanced, it is a mirror
0 commit comments