CS2230CSII:Datastructures
Meeting25:AVLtrees,indetailBrandonMyers
UniversityofIowa
Today’sbigideas
• TheAVLtreeisbalancedbinarytreeandrebalancesitselfusing“tri-noderestructuring”
• Tri-noderestructuringlooksatthreerootsinvolvedinthenewimbalanceandre-arrangesthemtorestorethebalanceproperty
b=y
a=z
c=xT0
T1
T2 T3h-1h-1
h
h
h
h+1
h+2
Insertmessesuptheheights
b=y
a=z
c=xT0
T1
T2 T3?h-1
h
h
?
?
?
d
insert(d)
b=y
a=z
c=xT0
T1
T2 T3h-1h-1
h
h
h
h+1
h+2
Peerinstruction
b=y
a=z
c=xT0
T1
T2 T3?h-1
h
h
?
?
?
d
insert(d)
Aftertheinsert,whichnodenowdoesn’tsatisfytheAVLbalanceproperty?a)ab)bc) cd) dd) multiplenodes
https://b.socrative.com/login/student/CS2230Aids1000-4999CS2230Bids5000+
b=y
a=z
c=xT0
T1
T2 T3h-1h-1
h
h
h
h+1
h+2
Togettheanswer
b=y
a=z
c=xT0
T1
T2 T3hh-1
h
h
h+1
h+2
h+3
d
insert(d)
b=y
a=z
c=xT0
T1
T2 T3h-1h-1
h
h
h
h+1
h+2
Tri-noderestructuring
b=y
a=z
c=xT0
T1
T2 T3hh-1
h
h
h+1
h+2
h+3
Goaloftri-noderestructuring:rearrange3rootstorebalancethetree
d
insert(d)
Tri-noderestructuring
b=y
a=z
c=xT0
T1
T2 T3hh-1
h
h
h+1
h+2
h+3
Intuition:wewanttomakethemiddlenode (here,itisb)thenewrootofthesubtree
Howdowepickthe3nodesintherestructuring?Rootisthenodethatnowhasunbalancedchildren(here,a),thenincludetwonodesbelowitonthepathtod(here,bandc)
d
b=y
a=z c=x
T0 T1 T2 T3
restructure
d
hh-1
h+1h+1
h h
h+2
Anotherexample
c=y
b=x
a=z
T0
T1 T2
T3
h-1 h-1
hh
h+1h
h+2
c=y
b=x
a=z
T0
T1 T2
T3
h-1 ?
?h
?h
h+3
d
insert(d)
Peerinstruction
c=y
b=x
a=z
T0
T1 T2
T3
h-1 h-1
hh
h+1h
h+2
c=y
b=x
a=z
T0
T1 T2
T3
h-1 ?
?h
?h
?
d
insert(d)
Wemightneedtorebalancethetree.Whatwillbetheresult?a) nochangeb) aremainstherootbutleft=bc) bisthenewrootwithleft=a,right=cd) cisthenewrootwithleft=a,right=be) disthenewrootwithleft=b,right=c
https://b.socrative.com/login/student/CS2230Aids1000-4999CS2230Bids5000+
Answer,part1
c=y
b=x
a=z
T0
T1 T2
T3
h-1 h-1
hh
h+1h
h+2
c=y
b=x
a=z
T0
T1 T2
T3
h-1 h
h+1h
h+2h
h+3
d
insert(d)
Wemightneedtorebalancethetree.Whatwillbetheresult?a) nochangeb) aremainstherootbutleft=bc) bisthenewrootwithleft=a,right=cd) cisthenewrootwithleft=a,right=be) disthenewrootwithleft=b,right=c
Answer,part2
c=y
b=x
a=z
T0
T1 T2
T3
h-1 h
h+1h
h+2
h+3
d
Wemightneedtorebalancethetree.Whatwillbetheresult?a) nochangeb) aremainstherootbutleft=bc) bisthenewrootwithleft=a,right=cd) cisthenewrootwithleft=a,right=be) disthenewrootwithleft=b,right=c
b=x
c=ya=z
T0 T1 T2 T3
d
h
Implementingtri-noderestructuring
• Step1:traverseupfromtheinsertednode,checkingtheAVLbalancepropertyateachnode.Onceyoufindonce,youhaveyour3nodesinvolvedinthetri-node
c=y
b=x
a=z
T0
T1 T2
T3
h-1 h
h+1h
h+2
h+3
d
checkd,checkitsparents...checkb,checkc,checka(aha!imbalanced)
h
Peerinstruction
Node54wasjustinserted,whatthreenodesshouldbeincludedinthetri-noderestructuring?
44
17 78
32 50 88
48 62
54
https://b.socrative.com/login/student/CS2230Aids1000-4999CS2230Bids5000+
Answer
44
17 78
32 50 88
48 62
54
Implementingtri-noderestructuring
• Step1:traverseupfromtheinsertednode,checkingtheAVLbalancepropertyateachnode.Onceyoufindonce,youhaveyour3nodesinvolvedinthetri-node
• Step2:doeither1rotationor2rotationsdependingonwhichoffourcases...
Case1:right,right
Case2:left,left
T0T1
T2T3
c = xb = y
a = z
T0 T1 T2T3
c = xb = y
a = zsingle rotation
T3T2
T1T0
a = xb = y
c = z
T0T1T2T3
a = xb = y
c = zsingle rotation
h
Implementingtri-noderestructuringinsertoccurredinshadedsubtreeSinglerotations
T0T1 T2 T3
Doublerotations• Case3:right,left
• Case4:left,right
double rotationa = z
b = xc = y
T0T2
T1T3 T0
T2T3T1
a = zb = x
c = y
double rotationc = z
b = xa = y
T0T2
T1T3 T0
T2T3 T1
c = zb = x
a = y
Implementingtri-noderestructuringinsertoccurredinshadedsubtree
Peerinstruction
88
44
17 78
32 50
48 62
2
5
1
1
3
4
2
1
54
1
T0 T2
T3
x
y
z
2
3
4
5
67
1
Whichcaseisneededtoperformeither1or2rotations?a)case1:right,rightb)case2:left,leftc) case3:right,leftd)case4:left,right
Answer
Questionfornexttime...
afteraninsertimbalancesaBinarySearchTree ofheightH,howmanytri-noderestructuringsdowehavetoperformtobalancethewholetree?
(giveanswerinbig-oh)
Today’sbigideas
• TheAVLtreeisbalancedbinarytreeandrebalancesitselfusing“tri-noderestructuring”
• Tri-noderestructuringlooksatthreerootsinvolvedinthenewimbalanceandre-arrangesthemtorestorethebalanceproperty