AVL Trees
AVL Trees
No of
↳ Binary search Trees
edges Height Balanced
I
Balance factor of left of
Height subtree
Height right subtree
= -
{ -
I
, o ,
I } → Balanced Tree
O
o
/ \ ,
O O
o / Io of
O O O
Balanced Tree
Rotationsforinsertion.in#trees
(t ) Rotation Rotation
LL Single
(2) RR Rotation
]
(3) LR Rotation Double Rotation
(4) RL Rotation
]
INIT_IAL PERFORMR0T0N
AFTERROTATIONU
AFTERINSERT
in . . .
.
←
to
- l -
Z O
imam .
←
30
÷
¥÷÷÷
"
"
"
O .
"
"
- "
.
o
o
:*::
r
:*
÷
""
: .
"
"
"
-
-
FORMVLAOFROTATION.to#INSERT0N. - -7
-
-
TTT
-
-
"
-
④
if I
.
.
.
④
.
\ o
④
④ ④
.
.
o
.
E- to to ⑤
l O O
e. it to
ol ④ ⑤
① i / to / I
- i
-
i
⑤ ④ ④ ④
a- no: ios:
Ii Direction of
'
'
-
400
, ,
' . insertion
,
L
.
i -
.
.
i. ④ i ⑤ o
i # Iii \ o ④
④ ④ i
④ of L
'
-
i
•/ . . to ④ ④
⑦ ④ ④ 1/1 . . of \ - i
to ④ ④ ④ ④
④ ← Inserted / o
ol \o
Node ① ⑦ ④
PROGRAMFORLLROT.AT#
Struct Node
{
*
Struct Node l child ;
int data ;
int
height ; 11 we will set
height for
*
struct Node rchild ; each and Node
every
} *
root =
NULL ;
* *
struct Node Insert ( Node p ,
int key )
{
if ( NULL)
p==
{
t =
new Node ;
t → data -_
key ;
t =L ;
→
height
t → lchild = t → schild =
NULL ;
return t
;
}
if ( data)
keys p →
insert ( p
p slchild l child key )
= →
;
-
else if ( data)
key >
p
→
p
→ rchild Lp → rchild key);
= insert ,
p
node at
returning time
if ( Balance Factor ( p) ==2 Id Balance Factor ( p → lchild ) = = I )
return LL Rotation ( p) ;
l )
p
return LR Rotation ( p) ;
return RL Rotation ( p) ;
return
p;
}
*
int Node
Height ( struct Node p)
{
int hd ,
hr ; 11 Height of left subtree ( HL ) , height of right subtree ( HR )
ht Sd → lchild ? → lchild →
height :O ;
[
=p p p
rchild ?
ypwdudup
h schild
height :O ;
→ → →
p
ht Sd → lchild ? → lchild →
height :O ;
=p p p
schild ?
hr
=p SI schild
height :O ;
→ →
p
→
p
return he -
hr ;
* *
Struct Node LL Rotation ( struct Node p) p
pe
{ 0
/£%P
,q¥y%
*
Struct Node pl =p
→
echild ;
struct Node
*
plz -_
pl → l child ;
per pe§£
pl → ochild =p ;
p
→ lchild =
per ;
Node
p
→
height =
Height Ip) ;
pl →
height
=
Node Height Cpe ) ;
iflooot =p) 11 performed
Tf rotation
-
was on
root =
pl ; root node , zoot needs to be
updated .
return pl ;
}
* *
Struct Node LR Rotation ( struct Node p)
{ p per
o o
struct Node
*
pl =p → lchild ; pel Pel L p
O O O
l
struct Node
*
pls =
pl -
orchid ; 1) per 1
l
C) 2
11
I 2
pl → schild =
per → lchild ;
p
→ lchild =
plo → a child ;
per → lchild =
pl ;
per → schild =p
;
pl → height =
Node Height Cpd ) ;
height Node
Height Ip ) ;
→ =
p
Node Height Iplo ) ;
per height
=
→
if lzoot =
=p )
root
per ;
-
-
11 New root ;
return
plz ;
}
DELETIONFROMAVLTREES.li#ROTAT0N
I .
L 1 Rotation 4 .
R 1 Rotation
2 .
L -
1 Rotation 5 .
R
-
1 Rotation
3 .
L O Rotation 6 .
R -
O Rotation
I
z o
④
LI ol lo
.
no
④ ⑤
/ ④ ④
° of
④ ④
'
L -
I -
'
I
⑥ if o
④ ④ ④ mirror
images
t
!
Ll L -
④
I -
,
z
④
z
10 I O
£70
o O
L -
o
£ ⑦
o
o O O
o
S 20
20 5
HEIGHTVSNODES.co#-VLTREESIfheighIisgiven :
2h 11 2h" from
Man nodes I Not l because
height is
starting 1
•
n =
-
-
•
Min nodes n
=
Look in table
h =
I h=2 h =3 h = 4 h = 5
n = I n =2 n = 4 n = 7 n = 12
O l l l
l
O O O O O
o
/ , / to if I - i
,
/ I -
i
O O O O O O O
I 6 IIE
E
o
flood 8
0
|n1247122O3÷
-
¥f
↳ Iit
{
Nch) O O
=
NC
th -
2) + Nch
-
Dtt
otherwise
11 formula same as Fibonacci series
↳ balanced series
Tf'N'Nodegiefd :
Min height
logs ( htt )
•
=
•
Man
height
=
Look in table