Professional Documents
Culture Documents
NITRKL
Con ider the in ertion o! !ollo"ing element A# $# C# # %&#'# (# ) into the $ST
A
O(N)
C ' ( )
Con ider the in ertion o! !ollo"ing element )# '# (# # %&#C# $# A into the $ST
) ' ( C $ A
O(N)
A3L tree
4eight o! a node The height o! a lea! i 1& The height o! a null -ointer i 5ero& The height o! an internal node i the ma2imum height o! it children -lu 1
A3L tree
An A3L tree i a *inar+ earch tree in "hich
!or e,er+ node in the tree# the height o! the le!t and right u*tree di!!er *+ at most 1. An empty binary tree is an AVL tree
TL
le!t u*tree o! T
A3L tree
h(TL ) 4eight o! the u*tree TL TR Right u*tree o! T h(TR ) 4eight o! the u*tree TR T i an A3L tree i!! TL and TR are A3L tree and 6h(TL ) - h(TR ) 6 78 1 h(TL ) - h(TR ) i 0no"n a *alancing !actor ($9) and !or an A3L tree the $9 o! a node can *e either 0 # 1# or -1
1 (0)
Rotation
To -er!orm rotation ; Identi!+ a -eci!ic node A "ho e $9(A) i neither 0# 1# or -1 and "hich i the neare t ance tor to the in erted node on the -ath !rom the in erted node to the root
Rotation
Re*alancing rotation are cla i!ied a LL# LR# RR and RL LL Rotation< In erted node i in the le!t u*-tree o! le!t u*-tree o! node A RR Rotation< In erted node i in the right u*tree o! right u*-tree o! node A LR Rotation< In erted node i in the right u*tree o! le!t u*-tree o! node A RL Rotation< In erted node i in the le!t u*-tree o! right u*-tree o! node A
LL Rotation
(+1)
A
(0)
(+2)
A
&nsert ' into BL
$ h
c
AR BR
(+1) $
AR
c
BL
h+1
BL
BR
LL Rotation
(+2)
A
(+1) $ h+1
(0) LL Rotation
$ A
(0)
AR
c
h+1
BL
(
c
BL
BR
BR
Balanced AVL searc$ tree a!ter rotation
AR
LL Rotation 12am-le
(+1) (+2)
96
(0) (0) (0)
96
&nsert )* (+1) (+1) (0)
85 90
110
(0)
85 90
110
(0)
64
64
(0) 36
LL Rotation 12am-le
(+2) (0) (0) (+1)
96
(+1) (+1)
=>
(0)
85 90
110
(0)
LL Rotation
?@
96
(0)
64
(0) 36
(0) 36
90 (0) 110
RR Rotation
(-1)
A h AL $L $ (0) h
c
A AL $
(-1)
c h+1
$R
BL
BR
RR Rotation
(-2)
A AL BL $
(-1)
$ A
c h+1
BR
h AL BL
h+1
BR
RR Rotation 12am-le
(-1) (-2)
34
(0) (0)
34
&nsert *, (0) (0) (-1)
26
(0)
44 40 56
26
(0)
44 40
(-1)
56
(0) 6
RR Rotation 12am-le
(-2)
34
(0) (-1)
44
(0) (-1)
26
(0)
44 40
(-1)
34
(0)
56 40
(0)
56
(0) 6
26
65
(-1)
A (+1)
LR Rotation
&nsert ' into -L
(-1)
A C
(-1)
(+2)
$ h h-1 BL CL C (0)
c
CR AR
BL
CL
CR AR
(-1)
A (+2)
(-1)
LR Rotation
LR Rotation
(0)
(0) (-1)
$ h h BL
x
$
(
A
c
h BL
CL
c
CR
CL CR AR
AR
LR Rotation 12am-le
(+1) (+2)
44
(0) (0) (0)
44
&nsert ). (0) (-1) (0)
30 39
76
(0)
30 39
76
(+1)
16
16
LR Rotation 12am-le
(+2) (0)
44
(-1) (0) (0)
39
LR Rotation (0) (0) (-1)
30 39
). (0)
76
(+1)
30 37
44
(0) !6 (0)
16
16
(-1)
RL Rotation
(-2) &nsert ' into -R
c
h (-1) C h-1 AL CL
A (+1) $
CR $R
CR $R
(-2)
RL Rotation
(0) RL Rotation (+1)
C A
(0)
$
c
h h AL h-1
CR $R
h BR
CL CR
RL Rotation 12am-le
(-1) (-2)
34
(0) (0)
34
&nsert /1 (0) (0) (+1)
26
(0)
44 40 56
26
(-1)
44 40
(0)
56
RL Rotation 12am-le
(-2) (0)
34
(0)
40
(+1) RL Rotation (+1) (0)
26
(-1)
44 40
(0)
34
(0)
44 41
(0)
56
/1
(0) 26
56
(0)
A3L Tree
Con truct an A3L earch tree *+ in erting the !ollo"ing element in the order o! their occurrence ?@# 1# 1@# 2?# 1B# 110# C=# =>
In ert ?@# 1
(0) 1
64
(D1)
$0&
In ert 1@
1
64
$%2& LR $0& 1
14 $0& 64
$-1& 14 $0&
$0&
$0& 1
14 $0& 64
In ert =>
$-1& 1 14 $-2& $-2& $0& 64 13 $0& 26 98 => (0) $%2& 110 $%1& $-1& LL $-1& $-1& 1 $0& 64 $0& 13 $0& 98 26 $0& 85 14
$0& 110
A3L deletion
Let A *e the clo e t ance tor node on the -ath !rom ' (deleted node) to the root "ith a *alancing !actor D2 or -2 Cla i!+ the rotation a L or R de-ending on "hether the deletion occurred on the le!t or right u*tree o! A
A3L Deletion
De-ending on the ,alue o! B0(B) "here B i the root o! the le!t or right u*tree o! A# the R or L im*alance i !urther cla i!ied a R0# R1 and R -1 or L0# L1 and L-1&
R0 Rotation
(+1)
A
(0)
(+2)
A
1elete node ' (0)
$ h
c
h AR
(
$ h
c
AR BR h -1
BL
BR
BL
R0 Rotation
(+2)
A
(0)
(-1) $ R0 Rotation
(+1)
$ h
c
A BL
c
AR BR
BL
BR
AR
R0 Rotation 12am-le
(+1)
46
(0) (+1) (-1)
(+2)
46
1elete *0 (0) (0) (+1) (0)
20 23
54
(-1)
20 23
54
(-1)
18
(0) !
60 (0)
18
!
(0) 2"
(0) 2"
R0 Rotation 12am-le
(+2)
46
(0) (+1) (0)
(-1)
20
R0 (0) (+1) (+1)
20 23
54
(-1)
18
(-1)
46
(0) "
18
(0) !
23
(0) 2"
(0) 2"
R1 Rotation
(+1)
A
(+1) $ 1elete node '
(+2)
A
(+1) $
h AR h -1 BR
c
h -1 AR h -1 BR
c
h BL
h BL
R1 Rotation
(+2)
A
(+1) $
(0) R1 Rotation
(0)
A BL
c
h -1 AR h -1 BR
c
h BL
BR
h-1 AR
R1 Rotation 12am-le
(+1)
37
(+1) (+1) (+1)
(+2)
37
1elete )5 (+1) (+1) (0)
26 28
(0)
41
3# (0)
26 28
41
(0)
18
(0) 16
18
(0) 16
R1 Rotation 12am-le
(+2)
37
(+1) (+1) (0)
(0)
26
R1 Rotation (0) (+1) (0)
26
41
18 28
37
(0)
18
(0) 16
28 (0)
16
(0) "1
(-1)
A (+1)
R-1 Rotation
1elete '
(-1)
A C
(0)
(+2)
$ h-1 BL CL C (0)
c
h
(
h-1
c
h-1
CR AR
BL
AR
CL
CR
(-1)
A (+2)
R-1 Rotation
h -1
R -1
(0)
(0)
$ h-1 BL CL C (0)
c
$ h -1
(0)
c
A h -1
h-1
BL
CL
CR AR
44
(-1) (0) (-1)
44
1elete ,2 (-1) (0) (0)
22 28 2B
48
(0)
22 28
48
(0) 2#
18
>2
18
2C
23 (0)
44
(-1) (0) (0)
28
R-1 (0) (0)
22 28 2B
48
(0)
22
(0)
(0) ""
(0)
18
18
23
2#
48
2C
Balanced AVL search tree after rotation
L0 Rotation
(-1)
A h AL
(
A h-1 AL BL $
h (0)
$ (0) h $L
c
$R
BR
L0 Rotation
(-1)
A h -1 AL $ (0) h $L
c
$ AL A
h
c
(0)
$R
h-1 BL
BR
L1 Rotation
(-1)
A h AL
(
A h-1 AL (0) $
(+1)
$ (+1)
(0)
C h-1
h-1
C h-1 c BR CR
$R
Unbalanced AVL search tree after deletion
CL
CL CR
L1 Rotation
(-2)
A h-1 AL $ (+1)
(0) L1 (0)
(0)
C A h-1 AL $
(0)
C h-1 CL CR
h-1
h-1 c
$R
CL CR
BR
L-1 Rotation
(-1)
A h AL
(
A h-1 AL
h-1
h-1 $L
$ (-1) h $R
c
(-1)
BL
BR
L-1 Rotation
(-2)
$
(-1)
A
c
AL
h-1 BL
BR