Avl
Avl
.
The !'iC&TCh and tniven;al OJX'rat,ons are the same with an A VL tree as With a
. operations have to be modi
. and dcIetion
·fi d · d ·
. maintain
. te m or er to .
the balance rrorertY of the tree as new keys are mserted and existing ones
ranc,,,ed.
-
.., .• 1■9ft"INa
Routine to perform Single Rotation with left
In A\ 'l. trcc, the n"- key iii added at the child node. When the new key is inserted
into an A\'l. trcc. the balance property of the tree must be maintained. If the insertion def avlSingleRotateRight( self, k2 ):
of thc ncv.· key C&WiQ any of the subtrees to become unbalanced, they must be kl = k.2.left
rebalanced. k.2.left = kl.right
k I .right = k2
Rotatiottt
k.2.height = 1 + max.(self.Height(U.left). !IClf.HcigblcC.nght))
Muluple subtrees can become unbalanced after inserting a new key. To handle this kl.height= 1 + max.(selt:Heigh[\kl.left), !1Clf.Hetpqkl.nll,ht))
return kl
the ~rs1 suhtrcc encountered this out of balance has to be rebalanced. The root node
ofttu, subtree ,s known as the pivot node.
Case 2: Single Rotadoa witla Ri&lllt
An A VL subtree i, rd>alanced by performing a rotation around the pivot node. This
involvei. rCM1TV,gmg the luu-' of the pivot node, its children, and possibly one of its
grandc::hildrc:n. The acrw.f modific111ion~ depend on which descendant's subtree of the
pivot node the new ley wiu. mi>en.ed inl.(J and the b11lance factors.
There Ille fUUJ p<,i.~1lilc c&~b :
Cbe t : ln~nmg II nod<' 1:11, 11 ldt tl,ild of left 1>ubtree
Ca., 2, ln~n '"' • m,d, " • "'"" d.,Jd of, ;Kin •uht,ee _
~
-
.h.;..m_s_ _ _ _ __
.2~2:!4_ _ _ _ _ _ _ __!D~a~ta~S~tru~cn~,r~es::_;a~n'.!!d..'.:A~lg~o~rit;;:.;
Routine to perfonn Single Rotation with Right
---
~--- -- Tree Structures
225
'
Routine to perform double rotation with left
def avlSingleRotateLeft( self, k I ):
def avlDoubleRota~eRight( self, kI ):
k2 =kl.right
k I .Right= avlSmgleRotateleft( k I .left)
k I.right = k2.left
return avlSingleRotateRigh t(k I)
k2.left = kl
k2.height = 1 + max(self.Height(k2.left), self.Height(k.2.right))
kl.height= 1 + max(self.Height(kl.left), self.Height(kl.right)) After a rotation is performed, the balance factor of the impacted nodes has to be
changed to reflect the new node heights.
return k2
Implementation for insertion
Case 3: Left Right Double Rotation
def insert(self,root,key):
if not root:
return node(key)
elif key < root.data:
root.left= self.insert(root.IeftJcey)
else:
root.right= self.insert(root.right,key)
root.height= 1 + max(self.Height(root.left), self.Height(root.right))
BF = self.calculateBalanc e(root)
Routine to perform double rotation with left
If BF > 1 and key < root.left.data:
def avlDoubleRotateLeft ( self, k3 ): Return self. avlSingleRotateRight (root)
k3.left= avlSingleRot.ateRigbt(k3 .left) If BF < -1 and key > root.right.data:
return avlSingleRotateleft(k 3) Return self. avlSingleRotateLeft (root)
If BF > 1 and key < root.left.data:
Case 4: Right Left Double Rotation Root.left=self. avlDoubleRotateleft (root.left)
If BF < -1 and key < root.right.data:
Root.right=self. avlDoubleRotateright (root.right)
Return root
Example:
Insert I, 2, 3, 4, 5, 6, 7
Insert I
CD 0-()o:()
l'lalil Strurturrs 1111<1 Al11nr1thrn s
--
226
~ Tr.,, Strur.tur.,, 227
lnsen 2
fnHCrt 6
~ - 1-- 1
\)0-0-0
ln11en 3
K,
u
~ 0-0-(l
U Do n.,tation
~1-0
(D0+.o @~
lnscn 4
Insert 7
0-2--2 +-ID~ mad
In:scn 5
G--2~-z -- d o ~
:::::>
~F(J
228
Insert 16
Tree Structures
229
1- 3~-2 4-- to be rotate
~
lnwrt 15
Insert 13
K.
tL
__,,,
Tree Structures
230
d Algorithms
Data Structures a!,_ -.... 231
fo Jnsert 11
Insert 12
3-1 ~2 .- To be rotated
.IJ Require single rotation.
u
23J
lalft't 11