Introduction To AVL Trees
Dr. Thomas E. Hicks
Trinity University
[Set all IE margins
to .5]
This is the simple/trivial case for Rotate-Right Rotation (N is
a leaf node with no sons). After the Rotate-Right Rotation
The Balance Factor of A = ____________
The Balance Factor of B = ____________
The Balance Factor of N = ____________
Add Node
70
________
|__50__| -1 ?? <-- Father of
A
.
.
________
________
|__40__| +0
|__60__| -2 <-- A
.
________
|__65__| -1 <--
B
.
________
|__70__| +0 <--
N
A is placed to the left of B
Remember that when using AVL Rotations for Insertion, There will only be four standard forms which trigger rotations. The Rotate-Left rotation must occur whenever A = -2 and B = -1.
Balanced
subtree
. ? .
(Prior To Insertion which forces a rotation):
. .
__L__._Info_._bf_._R__
................................>
|_.__|___A__|_-1_|_._|
.
.
.
.
.
.
.
__________________<..
______________________
.
| A-L
| . .....................>
|_.__|___B__|_+0_|_._|
.
|
| . h
.
.
.
h+2 .
|
| . .
__________________<..
..>__________________
.
|
| . .
|
B-L |
. .
| B-R
|
.
|
| . .
|
| . .
|
|
. |________________|<..
.
|
| .h h .
|
|
.
.
|
| . .
|
|
.
h+1 .
|
| . .
|
|
.................................,..>
|_________________|<............>|________________|
Un-Balanced
subtree
. ? .
(After Insertion - now needs a
rotation):
. .
__L__._Info_._bf_._R__
................................>
|_.__|___A__|_-2_|_._|
.
.
.
.
.
.
.
__________________<..
______________________
.
| A-L
| . .....................>
|_.__|___B__|_-1_|_._|
.
|
| . h
.
.
.
h+2 .
|
| . .
__________________<..
..>__________________
.
|
| . .
|
B-L |
. .
| B-R
|
.
|
| . .
|
| . .
|
|
. |________________|<..
.
|
| .h .
|
|
.
.
|
| . h+1 .
|
|
.
h+1 .
|
| . .
|
|
.................................,..>
|_________________|<.. .
|________________|
...>|______N_________|
Re-Balanced
subtree
.
? .
(After Rotate-Left
Rotation):
. .
__L__._Info_._bf_._R__
................................> |_.__|__B__|_+0_|_._|
.
.
.
.
.
.
.
______________________
.
.
|_.__|___A__|_+0_|_._|
..>__________________
.
.
.
. |
B-R |
h+2 .
__________________<.. .
__________________<.. .
|
|
.
| A-L |
. .
|
B-L |
. .
|
|
.
|
| .h h.
|
| . .
|
|
.
|
| . .
|
| .h .
|
|
.
|
| . .
|
| . h+1 .
|
|
.
|
| . .
|
| . .
|________________|
......|_________________|<..........>
|_________________|<..
..>|______N_________|