You are on page 1of 54

Data Structure and Algorithm (CS-102)

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)

$alanced *inar+ tree


The di ad,antage o! a *inar+ earch tree i that it height can *e a large a N-1 Thi mean that the time needed to -er!orm in ertion and deletion and man+ other o-eration can *e .(N) in the "or t ca e /e "ant a tree "ith mall height A *inar+ tree "ith N node ha height at least (log N) Thu # our goal i to 0ee- the height o! a *inar+ earch tree O(log N) Such tree are called balanced *inar+ earch tree & 12am-le are A3L tree# red-*lac0 tree&

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

A3L Search Tree


(-1) C (0) A D (0) G (1)

In ertion in A3L earch Tree


In ertion into an A3L earch tree ma+ a!!ect the $9 o! a node# re ulting the $ST un*alanced& A techni:ue called Rotation i u ed to re tore he *alance o! the earch tree

A3L Search Tree


(-1) C (0) A D (0) G (1)

A3L Search Tree


(-2) C (0) A G (2) D (-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

BL Le!t "#b-tree o! B BR Rig$t "#b-tree o! B AR Rig$t "#b-tree o! A $ %eig$t

Unbalanced AVL search tree after insertion

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

Unbalanced AVL search tree after insertion

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

Unbalanced AVL search tree

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

Unbalanced VAL search tree

Balanced AVL searc$ tree a!ter LL rotation

RR Rotation
(-1)

A h AL $L $ (0) h
c

(-2) &nsert ' into BR

A AL $
(-1)

c h+1

$R

BL

BR

Unbalanced AVL search tree after insertion

RR Rotation
(-2)

A AL BL $
(-1)

(0) RR Rotation (0)

$ A

c h+1

BR

h AL BL

h+1

Unbalanced AVL search tree after insertion

BR

Balanced AVL search tree after Rotation

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

Unbalanced AVL search tree

RR Rotation 12am-le
(-2)

34
(0) (-1)

(-2) RR Rotation (0)

44
(0) (-1)

26
(0)

44 40
(-1)

34
(0)

56 40
(0)

56
(0) 6

26

65

Balanced AVL search tree after RR rotation

(-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

Unbalanced AVL search tree after insertion

(-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

Balanced AVL search tree after LR Rotation

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

3! (0) Unbalanced AVL search tree

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

Balanced AVL search tree

(-1)

RL Rotation
(-2) &nsert ' into -R
c

A (0) $ h (0) C h-1 AL CL h

h (-1) C h-1 AL CL

A (+1) $

CR $R

CR $R

An*alanced A3L earch tree a!ter in ertion

(-2)

RL Rotation
(0) RL Rotation (+1)

A (+1) $ h (-1) C h-1 AL CL


c

C A

(0)

$
c

h h AL h-1

CR $R

h BR

CL CR

Balanced AVL search ree a! er RL R" a #"n

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

(0) "1 Unbalanced AVL search tree

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)

Balanced AVL search tree

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&

In ert 2?# 1B# 110#C=


$-1& $-1& $-1& 1 (0) 64 (D1) 1B (0) 110 2? (0) C= 14

$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

Deletion in A3L earch Tree


Deletion in A3L earch tree -roceed the ame "a+ a the deletion in *inar+ earch tree 4o"e,er# in the e,ent o! im*alance due to deletion# one or more rotation need to *e a--lied to *alance the A3L tree&

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

Unbalanced AVL search tree after deletion of node x

R0 Rotation
(+2)

A
(0)

(-1) $ R0 Rotation

(+1)

$ h
c

A BL
c

AR BR

BL

B0(B) 22 03 #se R0 rotation

BR

AR

Unbalanced AVL search tree after deletion of x

Balanced AVL searc$ tree a!ter rotation

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"

4nbalanced AVL searc$ tree a!ter deletion

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"

Balanced AVL search tree after deletion

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

Unbalanced AVL search tree after deletion of node x

R1 Rotation
(+2)

A
(+1) $

(0) R1 Rotation

(0)

A BL
c

h -1 AR h -1 BR
c

h BL

B0(B) 22 13 #se R1 rotation

BR

h-1 AR

Balanced AVL searc$ tree a!ter rotation

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

4nbalanced AVL searc$ tree a!ter deletion

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

Balanced AVL searc$ tree a!ter deletion

(-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

Unbalanced AVL search tree after deletion

(-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

B0(B) 22 -13 CR AR #se R-1 rotation

BL

CL

CR AR

Balanced AVL search tree after Rotation

R-1 Rotation 12am-le


(+1) (+2)

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)

Unbalanced AVL search tree after deletion

R-1 Rotation 12am-le


(+2) (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
(

(-2) 1elete '

A h-1 AL BL $
h (0)

$ (0) h $L
c

$R

BR

Unbalanced AVL search tree after deletion

L0 Rotation
(-1)

A h -1 AL $ (0) h $L
c

(+1) 1elete ' (-1)

$ AL A
h
c

(0)

$R

h-1 BL

BR

Balanced AVL search tree after deletion

L1 Rotation
(-1)

A h AL
(

(-2) 1elete '

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

Unbalanced AVL search tree after deletion

L-1 Rotation
(-1)

A h AL
(

(-2) 1elete '

A h-1 AL
h-1

h-1 $L

$ (-1) h $R
c

(-1)

BL

BR

Unbalanced AVL search tree after deletion

L-1 Rotation
(-2)

A h-1 AL h-1 $L $ (-1) h $R


c

(0) 1elete '

$
(-1)

A
c

AL

h-1 BL

BR

Balanced AVL search tree after deletion

You might also like