The AVL Tree Rotations Tutorial: by John Hargrove Version 1.0.1, Updated Mar-22-2007
The AVL Tree Rotations Tutorial: by John Hargrove Version 1.0.1, Updated Mar-22-2007
By John Hargrove
Version 1.0.1, Updated Mar!!!00"
A#stra$t
% &rote this do$u'ent in an e((ort to $over &hat % $onsider to #e a dar) area o( the AVL Tree $on$ept.
*hen presented &ith the tas) o( &riting an AVL tree $lass in Java, % &as le(t s$ouring the &e# (or use(ul
in(or'ation on ho& this all &or)s. There &as a lot o( use(ul in(or'ation on the &i)ipedia pages (or AVL
tree and Tree rotation. +ou $an (ind lin)s to these pages in se$tion ,. The tree rotation page on &i)ipedia
is la$)ing, % (eel. The AVL tree page needs &or) as &ell, #ut this page is hurting #adly, and at so'e point
in the (uture, % &ill li)ely integrate 'ost o( this do$u'ent into that page. This do$u'ent $overs #oth types
o( rotations, and all , appli$ations o( the'. There is also a s'all se$tion on de$iding &hi$h rotations to
use in di((erent situations.
1. Rotations- Ho& they &or)
A tree rotation $an #e an i'ti'idating $on$ept at (irst. +ou end up in a situation &here you.re /uggling
nodes, and these nodes have trees atta$hed to the', and it $an all #e$o'e $on(using very (ast. % (ind it
helps to #lo$) out &hat.s going on &ith any o( the su#trees &hi$h are atta$hed to the nodes you.re
(u'#ling &ith, #ut that $an #e hard.
Left Rotation (LL)
%'agine &e have this situation-
Figure 1-1
a
\
b
\
c
To (i0 this, &e 'ust per(or' a le(t rotation, rooted at A. This is done in the (ollo&ing steps-
# #e$o'es the ne& root.
a ta)es o&nership o( #.s le(t $hild as its right $hild, or in this $ase, null.
# ta)es o&nership o( a as its le(t $hild.
The tree no& loo)s li)e this-
Figure 1-2
b
/ \
a c
Right Rotation (RR)
A right rotation is a 'irror o( the le(t rotation operation des$ri#ed a#ove. %'agine &e have this situation-
Figure 1-3
c
/
b
/
a
To (i0 this, &e &ill per(or' a single right rotation, rooted at 1. This is done in the (ollo&ing steps-
# #e$o'es the ne& root.
$ ta)es o&nership o( #.s right $hild, as its le(t $hild. %n this $ase, that value is null.
# ta)es o&nership o( $, as it.s right $hild.
The resulting tree-
Figure 1-4
b
/ \
a c
Left-Right Rotation (LR) or "Double left"
2o'eti'es a single le(t rotation is not su((i$ient to #alan$e an un#alan$ed tree. Ta)e this situation-
Figure 1-5
a
\
c
3er(e$t. %t.s #alan$ed. Let.s insert .#..
Figure 1-6
a
\
c
/
b
4ur initial rea$tion here is to do a single le(t rotation. Let.s try that.
Figure 1-7
c
/
a
\
b
4ur le(t rotation has $o'pleted, and &e.re stu$) in the sa'e situation. %( &e &ere to do a single right
rotation in this situation, &e &ould #e right #a$) &here &e started. *hat.s $ausing this5 The ans&er is
that this is a result o( the right su#tree having a negative #alan$e. %n other &ords, #e$ause the right
su#tree &as le(t heavy, our rotation &as not su((i$ient. *hat $an &e do5 The ans&er is to per(or' a right
rotation on the right su#tree. Read that again. *e &ill per(or' a right rotation on the right subtree. *e
are not rotating on our $urrent root. *e are rotating on our right $hild. Thin) o( our right su#tree, isolated
(ro' our 'ain tree, and per(or' a right rotation on it-
Be(ore-
Figure 1-8
c
/
b
A(ter-
Figure 1-9
b
\
c
A(ter per(or'ing a rotation on our right su#tree, &e have prepared our root to #e rotated le(t. Here is our
tree no&-
Figure 1-10
a
\
b
\
c
Loo)s li)e &e.re ready (or a le(t rotation. Let.s do that-
Figure 1-11
b
/ \
a c
Voila. 3ro#le' solved.
Right-Left Rotiation (RL) or "Double right"
A dou#le right rotation, or rightle(t rotation, or si'ply RL, is a rotation that 'ust #e per(or'ed &hen
atte'pting to #alan$e a tree &hi$h has a le(t su#tree, that is right heavy. This is a 'irror operation o(
&hat &as illustrated in the se$tion on Le(tRight Rotations, or dou#le le(t rotations. Let.s loo) at an
e0a'ple o( a situation &here &e need to per(or' a RightLe(t rotation.
Figure 1-12
c
/
a
\
b
%n this situation, &e have a tree that is un#alan$ed. The le(t su#tree has a height o( !, and the right
su#tree has a height o( 0. This 'a)es the #alan$e (a$tor o( our root node, $, e6ual to !. *hat do &e do5
2o'e )ind o( right rotation is $learly ne$essary, #ut a single right rotation &ill not solve our pro#le'. Let.s
try it-
Figure 1-13
a
\
c
/
b
Loo)s li)e that didn.t &or). 7o& &e have a tree that has a #alan$e o( !. %t &ould appear that &e did not
a$$o'plish 'u$h. That is true. *hat do &e do5 *ell, let.s go #a$) to the original tree, #e(ore &e did our
pointless right rotation-
Figure 1-14
c
/
a
\
b
The reason our right rotation did not &or), is #e$ause the le(t su#tree, or .a., has a positive #alan$e (a$tor,
and is thus right heavy. 3er(or'ing a right rotation on a tree that has a le(t su#tree that is right heavy &ill
result in the pro#le' &e /ust &itnessed. *hat do &e do5 The ans&er is to 'a)e our le(t su#tree le(t
heavy. *e do this #y per(or'ing a le(t rotation our le(t su#tree. 8oing so leaves us &ith this situation-
Figure 1-15
c
/
b
/
a
This is a tree &hi$h $an no& #e #alan$ed using a single right rotation. *e $an no& per(or' our right
rotation rooted at 1. The result-
Figure 1-16
b
/ \
a c
Balan$e at last.
!. Rotations, *hen to Use The' and *hy
Ho& to de$ide &hen you need a tree rotation is usually easy, #ut deter'ining &hi$h type o( rotation you
need re6uires a little thought.
A tree rotation is ne$essary &hen you have inserted or deleted a node &hi$h leaves the tree in an
un#alan$ed state. An un#alan$ed state is de(ined as a state in &hi$h any su#tree has a #alan$e (a$tor o(
greater than 1, or less than 1. That is, any tree &ith a di((eren$e #et&een the heights o( its t&o su#trees
greater than 1, is $onsidered un#alan$ed.
This is a #alan$ed tree-
Figure 2-1
1
/ \
2 3
This is an un#alan$ed tree-
Figure 2-2
1
\
2
\
3
This tree is $onsidered un#alan$ed #e$ause the root node has a #alan$e (a$tor o( !. That is, the right
su#tree o( 1 has a height o( !, and the height o( 1.s le(t su#tree is 0. Re'e'#er that #alan$e (a$tor o( a
tree &ith a le(t su#tree A and a right su#tree B is
B A
2i'ple.
%n (igure !!, &e see that the tree has a #alan$e o( !. This 'eans that the tree is $onsidered 9right
heavy9. *e $an $orre$t this #y per(or'ing &hat is $alled a 9le(t rotation9. Ho& &e deter'ine &hi$h
rotation to use (ollo&s a (e& #asi$ rules. 2ee psuedo $ode-
IF tree is right heavy
{
IF tree's right subtree is e!t heavy
{
"er!#r$ %#ube &e!t r#tati#'
(
)&*)
{
"er!#r$ *i'ge &e!t r#tati#'
(
(
)&*) IF tree is e!t heavy
{
IF tree's e!t subtree is right heavy
{
"er!#r$ %#ube +ight r#tati#'
(
)&*)
{
"er!#r$ *i'ge +ight r#tati#'
(
(
As you $an see, there is a situation &here &e need to per(or' a 9dou#le rotation9. A single rotation in the
situations des$ri#ed in the pseudo $ode leave the tree in an un#alan$ed state. :ollo& these rules, and
you should #e a#le to #alan$e an AVL tree (ollo&ing an insert or delete every ti'e.
;. 2u''ary
%t<s i'portant to understand that the e0a'ples a#ove &ere on very s'all trees to )eep the $on$epts $lear.
%n theory, ho&ever, i( you develop an appli$ation &hi$h uses AVL trees, progra''ing (or the situations
sho&n a#ove &hile using the rules provided should s$ale /ust (ine.
%( you have $o''ents, 6uestions or $riti$is's, (eel (ree to e'ail 'e at $astorv0=g'ail.$o'
,. :urther Reading
Tree rotation page on *i)ipedia, http->>en.&i)ipedia.org>&i)i>Tree?rotation
AVL tree page on *i)ipedia, http->>en.&i)ipedia.org>&i)i>AVL?tree
Ani'ated AVL Tree Java applet, http->>&e#pages.ull.es>users>/riera>8o$en$ia>AVL>AVL@!0tree
@!0applet.ht'
AVL Trees- Tutorial and 1AA %'ple'entation, http->>&&&.$'$rossroads.$o'>#radapp>(tp>sr$>li#s>1A
A>AvlTrees.ht'l