Académique Documents
Professionnel Documents
Culture Documents
Definition:
History:
Convention in diagrams:
Height Balance: AVL Trees Data Structures and Program Design In C++
Transp. 37, Sect. 10.4, Height Balance: AVL Trees 279 1999 Prentice-Hall, Inc., Upper Saddle River, N.J. 07458
–
–
–
–
– =
–
–
–
–
–
=
–
–
–
–
=
non-AVL trees
AVL trees
–
– –
–
–
–
–
–
=
–
–
=
–
–
– =
–
–
–
–
Examples of AVL trees and other binary trees Data Structures and Program Design In C++
Transp. 38, Sect. 10.4, Height Balance: AVL Trees 280 1999 Prentice-Hall, Inc., Upper Saddle River, N.J. 07458
C++ Conventions for AVL Trees
C++ Conventions for AVL Trees Data Structures and Program Design In C++
Transp. 39, Sect. 10.4, Height Balance: AVL Trees 281 1999 Prentice-Hall, Inc., Upper Saddle River, N.J. 07458
Virtual Methods and Dummy Methods
k: – k t: k e: – k
–
– t – e – t
v: k p: k a: – k
–
–
– e t – e – t e – t
–
–
– v – p – v – a – p – v
m: k u: k h: k
–
–
e t e – t – e – t
–
– a p – v – a p v – a – h p v
–
–
– m – m – u – m – u
k, m: k u: k – m
–
Rotate
left
– m m – k – u
–
– u
t, v : m p: m Double – t
–
rotation
left
– k – u – k u – m u
–
– t – v t – v – k – p – v
–
– p
Insertions into an AVL tree Data Structures and Program Design In C++
Transp. 41, Sect. 10.4, Height Balance: AVL Trees 283 1999 Prentice-Hall, Inc., Upper Saddle River, N.J. 07458
Public Insertion Method
{
Error code result = success;
if (sub root == NULL) {
sub root = new AVL node< Record >(new data);
taller = true;
}
else if (new data == sub root->data) {
result = duplicate error;
taller = false;
}
else if (new data < sub root->data) { // Insert in left subtree.
result = avl insert(sub root->left, new data, taller);
if (taller == true)
switch (sub root->get balance( )) { // Change balance factors.
case left higher:
left balance(sub root);
taller = false; // Rebalancing always shortens the tree.
break;
case equal height:
sub root->set balance(left higher);
break;
case right higher:
sub root->set balance(equal height);
taller = false;
break;
}
}
root – right_tree
=
right_tree
–
– root
Rotate
left
h T1
h+1 T3
h T2 h + 1 T3 h T1 h T2
root sub_tree
=
becomes
– right_tree root right_tree
sub_tree
h T1
h–1
h T4 h T1 T2 or T3 h T4
h
T2 h–1 T3
or
h
Total height = h + 2
The new balance factors for root and right tree depend on the pre-
vious balance factor for sub tree:
old sub tree new root new right tree new sub tree
− − − −
/ − \ −
\ / − −
Function to restore right balance Data Structures and Program Design In C++
Transp. 47, Sect. 10.4, Height Balance: AVL Trees 289 1999 Prentice-Hall, Inc., Upper Saddle River, N.J. 07458
Removal of a Node
–
Height
unchanged
T1 T2 T1 T2
no rotations
Deleted
Case 1
–
p p
–
Height
reduced
T1 T2 T1 T2
Deleted
Case 2
p q
–
–
Height
unchanged
–
q p
–
h–1 T1
single left h T3
h–1 T1
rotations h T2 h T3 h T2
Deleted
Case 3a
p –
q
–
–
Height
q p reduced
–
h–1 T1
h T3
h–1 T2 h–1 T1 h–1 T2
h T3
Deleted
Case 3b
p – r
–
q p q
–
Height
r reduced
h–1 T1
double T2 h–2 T3
rotation h–1 T4 h–1 T1 or h–1 T4
T2 h–2 T3
or h–1
Deleted
h–1
Case 3c
Sample cases, deletion from an AVL tree Data Structures and Program Design In C++
Transp. 49, Sect. 10.4, Height Balance: AVL Trees 291 1999 Prentice-Hall, Inc., Upper Saddle River, N.J. 07458
Initial: m
–
e p
–
c j n s
–
b – d h k – o – r u
–
–
– a g – i – l – t
–
– f
Adjust
Delete p : m balance
–
factors m
–
o
p
–
=
n s
–
– n s
–
o – r u
–
– r u
–
– t
– t
Rotate m
=
left:
e s
–
c j – o u
–
–
b – d h k n – r – – t
–
– a g – i – l
–
– f
j –
Double rotate
right around m :
e – m
–
c h k – s
–
b – d g – i – l – o u
–
– a – f n – – r – t
Example of deletion from an AVL tree Data Structures and Program Design In C++
Transp. 50, Sect. 10.4, Height Balance: AVL Trees 292 1999 Prentice-Hall, Inc., Upper Saddle River, N.J. 07458
Analysis of AVL Trees
–
–
–
–
–
– –
–
–
–
F1
– – –
–
–
–
– F2
– – –
–
–
F3
– F4