DS Lec 13 AVL
-
Upload
williamsock -
Category
Documents
-
view
225 -
download
0
Transcript of DS Lec 13 AVL
7/23/2019 DS Lec 13 AVL
http://slidepdf.com/reader/full/ds-lec-13-avl 1/17
AVL Trees
Lecture # 13
7/23/2019 DS Lec 13 AVL
http://slidepdf.com/reader/full/ds-lec-13-avl 2/17
Problem with the BST
Insert the nodes 1,2,3,4,5,6,7 in BST
Then search an element
How man o!erations the search will !er"orm here are the ad$anta%es o" BST&
7/23/2019 DS Lec 13 AVL
http://slidepdf.com/reader/full/ds-lec-13-avl 3/17
Balanced Trees
'rdered (inar trees are "airl eas to im!lement)
But, the can ha$e one draw(ac*
+$en an ordered (inar tree can de%rade into a linear
list i" the insertion routine recei$es elements inascendin% or nearl ascendin%- order)
1, 2, 3, 4 1
2
3
4
7/23/2019 DS Lec 13 AVL
http://slidepdf.com/reader/full/ds-lec-13-avl 4/17
Balanced trees
Se$eral methods ha$e (een de$elo!ed to
!re$ent trees "rom (ecomin% s*ewed)
Some o" the most !ower"ul is called ./L
trees)
Their name is deri$ed "rom the scientists who
"irst studied them0 .delson/els*ii and
Landis 162-
7/23/2019 DS Lec 13 AVL
http://slidepdf.com/reader/full/ds-lec-13-avl 5/17
AVL Trees
. (alanced (inar search tree 'ldest "orm o" (alanced (inar tree
.$oids s*ewin% ( re(alancin% when needed
Sel"(alancin%
7/23/2019 DS Lec 13 AVL
http://slidepdf.com/reader/full/ds-lec-13-avl 6/17
AVL Tree (Balance Binary Search
Tree) It is a (inar search tree in which the height of the
two sub-trees of every node never differ by
more than 1)
What about these trees?
3
6
82
1 4 7
3
6
82
1 4
5
7/23/2019 DS Lec 13 AVL
http://slidepdf.com/reader/full/ds-lec-13-avl 7/17
Building an AVL Tree ew node can (e inserted into the ri%ht or le"t su(tree as a!!ro!riate)
ew node can (e inserted without chan%in% the hei%ht o" the su(tree, in
which case neither the hei%ht nor the (alance o" the root will (e
chan%ed)
The onl case that can cause di""icult occurs when the new node is
added to a su(tree o" the root that is strictl taller than the othersu(tree, and the hei%ht is increased) This would cause one su(tree to
ha$e hei%ht 2 more than the other destroin% the ./L tree)
k 1
k 2
T1 T2T3
hh h+1
Total Height = h+3
k 2
k 1
T3T1 T2h
h+1
Total Height = h+2
Rotate Left
7/23/2019 DS Lec 13 AVL
http://slidepdf.com/reader/full/ds-lec-13-avl 8/17
Building an AVL Tree
BalanceHei%ht in"ormation can (e *e!t at the node)
hen we do insertion, we need to u!date all the
(alancin% in"ormation "or the nodes on the !ath
(ac* to the root)
k 1
k 2X
Y Z
k 2
k 1
X Y
Z
Equal o !ot"
7/23/2019 DS Lec 13 AVL
http://slidepdf.com/reader/full/ds-lec-13-avl 9/17
Building an AVL TreeSingle Rotation
E#a$%le& i'(et 1) 2) 3) 4) 5) 6) 7
1
2
3
Rotate2
1 3
*'(et 4) 5
2
1 3
4
5
Rotate
2
1 4
3 5
6
*'(et 6
Rotate
3
4
2 5
61
7*'(et 7
Rotate
3
4
2
5
61
7
7/23/2019 DS Lec 13 AVL
http://slidepdf.com/reader/full/ds-lec-13-avl 10/17
Building an AVL Tree
Insertion/ Rotation
E#a$%le& *'(et 3 i' the folloi'g tee a', $ake it -.L
4
6
2 8
1
7/23/2019 DS Lec 13 AVL
http://slidepdf.com/reader/full/ds-lec-13-avl 11/17
Building an AVL TreeInsertion/ Rotation
E#a$%le& *'(et ke/( 8 though 15 i' e0e(e o,e i' the
folloi'g tee a', $ake it a' -.L
3
4
2
5
61
7*'(et 15
!o ole$15
14*'(et 14
Rotate 3
4
2
5
61
15
14
7
The Rotatio' ha( 'ot fi#e, the height i$ala'e
7/23/2019 DS Lec 13 AVL
http://slidepdf.com/reader/full/ds-lec-13-avl 12/17
Building an AVL Tree
Double Right-Left Rotation
k 3
-
k 1
k 2
1
2Right Left
oule Rotatio'
k 2
-
k 1
k 3
7/23/2019 DS Lec 13 AVL
http://slidepdf.com/reader/full/ds-lec-13-avl 13/17
Building an AVL Tree
Double Right-Left Rotation
Right Left
oule Rotatio'
2
3
4
2
5
61
7
15
14
k 3
k 1
k 2
1
3
4
2
5
61
14
157
k 2
k 1k 3
7/23/2019 DS Lec 13 AVL
http://slidepdf.com/reader/full/ds-lec-13-avl 14/17
Building an AVL TreeDouble Left-Right Rotation
k 3
-
k 2
k 1
1
2 Left Right
oule Rotatio'
k 2
-
k 3
k 1
Left Right
oule Rotatio'1
2
23
25
29
2615
k 2
k 1
k 3
22
22
23
29
2515
k 2
k 1k 3
26
7/23/2019 DS Lec 13 AVL
http://slidepdf.com/reader/full/ds-lec-13-avl 15/17
Building an AVL Tree
ow we write a "ormal al%orithm to decide
which t!e o" rotation we !er"orm when ./L
condition is $iolated)
Let is a !ointer to the node at which ./Lcondition is $iolated)
ildi
7/23/2019 DS Lec 13 AVL
http://slidepdf.com/reader/full/ds-lec-13-avl 16/17
Building an AVL TreeIf P is left higher (Its left height is greater)
{ Q = P -> left;
if(Q is Right higher (Its right height is greater) //Perform Left-Right double rotation
{ Perform left rotation at QPerform right rotation at P
}
else erform right rotation at P //Perform single right rotation
}
else //P is right higher
{ Q = P -> right
if(Q is left higher (its left height is greater) //Perform Right-Left double rotation{ Perform right rotation at Q
Perform left rotation at P
}
else //erform single left rotation
erform left rotation at P
}
7/23/2019 DS Lec 13 AVL
http://slidepdf.com/reader/full/ds-lec-13-avl 17/17
Exercise
Insert these *es and ma*e an ./L tree 1,2,3,4,5,6,7,15,14,13,12,11,1,,