COMP 250 Lctur 23 priority quu ADT haps 1 Nov. 1/2, 2017 1
Priority Quu Li a quu, but now w hav a mor gnral dinition o which lmnt to rmov nxt, namly th on with highst priority..g. hospital mrgncy room Assum a st o comparabl lmnts or ys. 2
Priority Quu ADT add(lmnt) rmovmin() highst priority = numbr 1 priority p() contains(lmnt) rmov(lmnt) 3
How to implmnt a Priority Quu? sortd list? binary sarch tr (last lctur)? balancd binary sarch tr (COMP 251)? hap (nxt 3 lcturs) Not th sam hap you har about in COMP 206. 4
Complt Binary Tr (dinition) c m a g j d j d Binary tr o hight h such that vry lvl lss than h is ull, and all nods at lvl h ar as ar to th lt as possibl 5
min Hap (dinition) a b l u m Complt binary tr with uniqu comparabl lmnts, such that ach nod s lmnt is lss than its childrn s lmnt. 6
Hap.add(lmnt).g. add( c ) a b l u m 7
Hap.add(lmnt).g. add( c ) a b l u m? 8
Hap.add(lmnt).g. add( c ) a b l u m c Problm : adding at th nxt availabl slot typically will dstroy th hap proprty. 9
W swap c with its parnt. Q: Can this crat a problm with c s ormr sibling, who is now c s child? a b c l u m 10
W swap c with its parnt. Q: Can this crat a problm with c s ormr sibling, who is now c s child? A: No. Bcaus c < and < m. Thus, c < m. a b c l u m 11
Q: Ar w don? A: Not ncssarily. What about c s parnt? a b c l u m 12
W swap c with its (nw) parnt. Now w ar don bcaus c is gratr than its parnt a a c b l u m 13
Hap.add(lmnt) add( lmnt ){ cur = nw nod at nxt availabl la position cur.lmnt = lmnt whil (cur!= root) and (cur.lmnt < cur.parnt.lmnt){ swapelmnt(cur, parnt) cur = cur.parnt } } 14
Hap.add(lmnt) add( lmnt ){ cur = nw nod at nxt availabl la position cur.lmnt = lmnt whil (cur!= root) and (cur.lmnt < cur.parnt.lmnt){ swapelmnt(cur, parnt) cur = cur.parnt } } 15
Hap.add(lmnt) add( lmnt ){ cur = nw nod at nxt availabl la position cur.lmnt = lmnt whil (cur!= root) and (cur.lmnt < cur.parnt.lmnt){ swapelmnt(cur, parnt) // argumnts ar nods cur = cur.parnt } } 16
How to build a hap? add( ) add( ) add( ) add( a ) add( g ) 17
How to build a hap? add( ) add( ) add( ) add( a ) 18
How to build a hap? add( ) add( ) add( ) add( a ) add( g ) 19
How to build a hap? add( ) add( ) add( ) add( a ) a add( g ) 20
How to build a hap? add( ) add( ) add( ) add( a ) add( g ) g a 21
This mthod o building a hap is slow. I will show you a astr mthod two lcturs rom now. 22
Hap.rmovMin() rturns root lmnt a c b l u m 23
rmovmin() a c b l u m 24
rmovmin() Claim: i th root has two childrn, thn th nw root will b gratr than at last on o its childrn. Why? c b a How to solv this problm? m l u 25
Swap lmnts with smallr child. rmovmin() b a c l u Kp swapping with smallr child, i ncssary. m 26
Lt s do it again. rmovmin() b a c l u m 27
Lt s do it again. rmovmin() b b c l u m 28
rmovmin() Now swap with smallr child, i ncssary, to prsrv hap proprty. m b c l u 29
rmovmin() c b Kp swapping with smallr child, i ncssary. m l u 30
rmovmin() c b m l u 31
rmovmin(){ tmp = root.lmnt rmov last la nod and put its lmnt into th root cur = root whil ((cur has at last on child) and ( (cur.lmnt > cur.lt.lmnt) or (cur has right child and cur.lmnt > cur.right.lmnt)) ) { minchild = child with th smallr lmnt swapelmnt(cur, minchild) cur = minchild } rturn tmp } 32
rmovmin(){ tmp = root.lmnt rmov last la nod and put its lmnt into th root cur = root whil ( (cur has a lt child) and ( (cur.lmnt > cur.lt.lmnt) or (cur has right child and cur.lmnt > cur.right.lmnt)) ) { minchild = child with th smallr lmnt swapelmnt(cur, minchild) cur = minchild } rturn tmp } 33
rmovmin(){ tmp = root.lmnt rmov last la nod and put its lmnt into th root cur = root whil ( (cur has a lt child) and ( (cur.lmnt > cur.lt.lmnt) or (cur has right child and cur.lmnt > cur.right.lmnt)) ) { minchild = child with th smallr lmnt swapelmnt(cur, minchild) cur = minchild } rturn tmp } 34
add(lmnt) rmovmin() uphap downhap 35
Q: What about rmov(lmnt)? 36
Q: What about rmov(lmnt)? A: Worst cas Θ(n) Bst cas (not discussd) 37
Hap (array implmntation) 1 c 2 3 m 4 5 6 7 a g j Not usd d j d d d d 8 9 10 11 12 13 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 38
1 a 2 3 b 4 5 6 7 l u Not usd m g n q w z 8 9 10 11 12 13 a b l u m g n q w z 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 39
Nxt two lcturs writ add(lmnt) and rmovmin() using array indics bst and worst cas astr algorithm or building a hap 40