Download - CS 2230 CS II: Data structures
![Page 1: CS 2230 CS II: Data structures](https://reader030.vdocuments.mx/reader030/viewer/2022012704/61a67a646fa78d1dc93d42f5/html5/thumbnails/1.jpg)
CS2230CSII:Datastructures
Meeting25:AVLtrees,indetailBrandonMyers
UniversityofIowa
![Page 2: CS 2230 CS II: Data structures](https://reader030.vdocuments.mx/reader030/viewer/2022012704/61a67a646fa78d1dc93d42f5/html5/thumbnails/2.jpg)
Today’sbigideas
• TheAVLtreeisbalancedbinarytreeandrebalancesitselfusing“tri-noderestructuring”
• Tri-noderestructuringlooksatthreerootsinvolvedinthenewimbalanceandre-arrangesthemtorestorethebalanceproperty
![Page 3: CS 2230 CS II: Data structures](https://reader030.vdocuments.mx/reader030/viewer/2022012704/61a67a646fa78d1dc93d42f5/html5/thumbnails/3.jpg)
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)
![Page 4: CS 2230 CS II: Data structures](https://reader030.vdocuments.mx/reader030/viewer/2022012704/61a67a646fa78d1dc93d42f5/html5/thumbnails/4.jpg)
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+
![Page 5: CS 2230 CS II: Data structures](https://reader030.vdocuments.mx/reader030/viewer/2022012704/61a67a646fa78d1dc93d42f5/html5/thumbnails/5.jpg)
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)
![Page 6: CS 2230 CS II: Data structures](https://reader030.vdocuments.mx/reader030/viewer/2022012704/61a67a646fa78d1dc93d42f5/html5/thumbnails/6.jpg)
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)
![Page 7: CS 2230 CS II: Data structures](https://reader030.vdocuments.mx/reader030/viewer/2022012704/61a67a646fa78d1dc93d42f5/html5/thumbnails/7.jpg)
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
![Page 8: CS 2230 CS II: Data structures](https://reader030.vdocuments.mx/reader030/viewer/2022012704/61a67a646fa78d1dc93d42f5/html5/thumbnails/8.jpg)
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)
![Page 9: CS 2230 CS II: Data structures](https://reader030.vdocuments.mx/reader030/viewer/2022012704/61a67a646fa78d1dc93d42f5/html5/thumbnails/9.jpg)
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+
![Page 10: CS 2230 CS II: Data structures](https://reader030.vdocuments.mx/reader030/viewer/2022012704/61a67a646fa78d1dc93d42f5/html5/thumbnails/10.jpg)
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
![Page 11: CS 2230 CS II: Data structures](https://reader030.vdocuments.mx/reader030/viewer/2022012704/61a67a646fa78d1dc93d42f5/html5/thumbnails/11.jpg)
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
![Page 12: CS 2230 CS II: Data structures](https://reader030.vdocuments.mx/reader030/viewer/2022012704/61a67a646fa78d1dc93d42f5/html5/thumbnails/12.jpg)
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
![Page 13: CS 2230 CS II: Data structures](https://reader030.vdocuments.mx/reader030/viewer/2022012704/61a67a646fa78d1dc93d42f5/html5/thumbnails/13.jpg)
Peerinstruction
Node54wasjustinserted,whatthreenodesshouldbeincludedinthetri-noderestructuring?
44
17 78
32 50 88
48 62
54
https://b.socrative.com/login/student/CS2230Aids1000-4999CS2230Bids5000+
![Page 14: CS 2230 CS II: Data structures](https://reader030.vdocuments.mx/reader030/viewer/2022012704/61a67a646fa78d1dc93d42f5/html5/thumbnails/14.jpg)
Answer
44
17 78
32 50 88
48 62
54
![Page 15: CS 2230 CS II: Data structures](https://reader030.vdocuments.mx/reader030/viewer/2022012704/61a67a646fa78d1dc93d42f5/html5/thumbnails/15.jpg)
Implementingtri-noderestructuring
• Step1:traverseupfromtheinsertednode,checkingtheAVLbalancepropertyateachnode.Onceyoufindonce,youhaveyour3nodesinvolvedinthetri-node
• Step2:doeither1rotationor2rotationsdependingonwhichoffourcases...
![Page 16: CS 2230 CS II: Data structures](https://reader030.vdocuments.mx/reader030/viewer/2022012704/61a67a646fa78d1dc93d42f5/html5/thumbnails/16.jpg)
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
![Page 17: CS 2230 CS II: Data structures](https://reader030.vdocuments.mx/reader030/viewer/2022012704/61a67a646fa78d1dc93d42f5/html5/thumbnails/17.jpg)
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
![Page 18: CS 2230 CS II: Data structures](https://reader030.vdocuments.mx/reader030/viewer/2022012704/61a67a646fa78d1dc93d42f5/html5/thumbnails/18.jpg)
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
![Page 19: CS 2230 CS II: Data structures](https://reader030.vdocuments.mx/reader030/viewer/2022012704/61a67a646fa78d1dc93d42f5/html5/thumbnails/19.jpg)
Answer
![Page 20: CS 2230 CS II: Data structures](https://reader030.vdocuments.mx/reader030/viewer/2022012704/61a67a646fa78d1dc93d42f5/html5/thumbnails/20.jpg)
Questionfornexttime...
afteraninsertimbalancesaBinarySearchTree ofheightH,howmanytri-noderestructuringsdowehavetoperformtobalancethewholetree?
(giveanswerinbig-oh)
![Page 21: CS 2230 CS II: Data structures](https://reader030.vdocuments.mx/reader030/viewer/2022012704/61a67a646fa78d1dc93d42f5/html5/thumbnails/21.jpg)
Today’sbigideas
• TheAVLtreeisbalancedbinarytreeandrebalancesitselfusing“tri-noderestructuring”
• Tri-noderestructuringlooksatthreerootsinvolvedinthenewimbalanceandre-arrangesthemtorestorethebalanceproperty