S Notes : inry Ser Trees (Lst upte /7/ 8:7 M) ROTTIONS Single left rottion t (K rotting ege ) Single rigt rottion t (K rotting ege ) F oule rigt rottion t F G F G Wt two single rottions re equivlent?
(OTTOM-UP) R-LK TRS re-l tree is inry ser tree wose eigt is logritmi in te numer of eys store.. very noe is olore re or l. (olors re only emine uring insertion n eletion). very lef (te sentinel) is olore l.. ot ilren of re noe re l.. very simple pt from il of noe X to lef s te sme numer of l noes. Tis numer is nown s te l-eigt of X ((X)). mple: = re = l 6 7 9 6 7 Oservtions:. re-l tree wit n internl noes ( eys ) s eigt t most lg(n+).. If noe X is not lef n its siling is lef, ten X must e re.. Tere my e mny wys to olor inry ser tree to me it re-l tree.. If te root is olore re, ten it my e swite to l witout violting struturl properties.
Insertion. Strt wit unlne insert of t lef (ot ilren re te sentinel).. olor of new noe is.. My violte struturl property. Les to tree ses, long wit symmetri versions. Te pointer points t re noe wose prent migt lso e re. se : (in Notes, using Segewi s oo, tis is one top-own efore tting new lef) = re = l + + + se : = re = l + + - - e e
se : = re = l + + - e - e mple: = re = l 6 7 Insert 6 6 7 7 Insert 6 6 7 7 6 7
Insert 7 6 7 6 7 7 7 Insert 6 6 7 7 7 7 Reset to l 6 7 7 mple: 6 7 6 7 9 Insert 7 6 6 7 6 6 7 7 9 7 9 7 7
6 6 7 6 6 6 7 7 9 7 9 7 7 6 6 7 7 9 7 eletion Strt wit one of te unlne eletion ses:. elete noe is t lef.. Splie roun to sentinel.. olor of elete noe? Re one l Set oule l pointer t sentinel. etermine wi of four relning ses pplies.. elete noe is prent of one t lef.. Splie roun to t lef. olor of elete noe? Re Not possile l t lef must e re. nge its olor to l.. Noe wit ey-to-elete is prent of two t noes.. Stel ey n t from suessor (ut not te olor).. elete suessor using te pproprite one of te previous two ses.
se : 7 = re = l + + - - - - - - e f e f se : = re = = l = = eiter +olor() +olor() - - - - - - - - - e f e f se : = re = = l = = eiter +olor() +olor() - - - - - - - e f - e f
se : 8 = re = = l = = eiter +olor()+olor() +olor()+olor() - +olor() +olor() - +olor() - - +olor() - +olor() - +olor() +olor() - e - +olor() f e f (t most tree rottions our wile proessing te eletion of one ey) mple: 6 7 9 elete 6 7 9 6 7 9
9 6 7 9 6 7 9 If rees te root, ten one. Only ple in tree were tis ppens. elete 6 7 9 elete 7 9 9 9 If rees re noe, ten nge olor to l n one.
elete 9 9 9 elete 9 9 9 9 elete 9 9 9 9 elete 9 9
VL TRS n VL tree is inry ser tree wose eigt is logritmi in te numer of eys store.. noe stores te ifferene of te eigts (nown s te lne ftor) of te rigt n left sutrees roote y te ilren: eigt rigt - eigt left + + + - 6 + + 7 9 -. lne ftor must e +,, - (lens rigt, lne, lens left).. n insertion is implemente y:. tting lef. Rippling nges to lne ftor:. Rigt il ripple Prent.l = + n ripple to prent Prent.l = - to omplete insertion Prent.l = + + n ROTTION to omplete insertion. Left il ripple Prent.l = - n ripple to prent Prent.l = + to omplete insertion Prent.l = - - n ROTTION to omplete insertion
. Rottions. Single (LL) - rigt rottion t Rest of Tree Rest of Tree - - Restores eigt of sutree to pre-insertion numer of levels RR se is symmetri. oule (LR) F - Rest of Tree Rest of Tree + G F + - - - G - - Insert on eiter sutree Restores eigt of sutree to pre-insertion numer of levels RL se is symmetri
eletion - Still ve RR, RL, LL, n LR, ut two itionl (symmetri) ses rise. Suppose 7 is elete from tis tree. iter LL or LR my e pplie. 6 - - + - 7 Fioni Trees - speil se of VL trees eiiting two worst-se eviors -. Mimlly sewe. (m eigt is rougly log.68 n =. lg n, epete eigt is lg n +.). θ(log n) rottions for single eletion. (empty) (empty) 6 7
TRPS (LRS, p. ) Hyri of ST n min-ep ies Gives oe tt is lerer tn R or VL (ut omprle to sip lists) pete eigt of tree is logritmi (. lg n) Keys re use s in ST Tree lso s min-ep property se on e noe ving priority: Rnomize priority - generte wen new ey is inserte Virtul priority - ompute (wen neee) using funtion similr to s funtion 9 6 7 6 7 7 6 8 7 6 77 8 8 7 67 7 8 8 9 88 sies: te first pulise su yri were te rtesin trees of J. Vuillemin, Unifying Loo t t Strutures,. M (), pril 9, 9-9. more omplete eplntion ppers in.m. Mreigt, Priority Ser Trees, SIM J. omputing (), My 98, 7-76 n pter of M. e erg et.l. Tese re lso use in te elegnt implementtion in M.. eno n T.. Striovsy, omputing Longest ommon Sustrings in.. Hirs, omputer Siene - Teory n pplitions, LNS, 8, 6-7. Insertion Insert s lef Generte rnom priority (lrge rnge to minimize uplites) Single rottions to fi min-ep property
mple: Insert 6 wit priority of 9 6 7 6 7 7 6 8 7 6 77 8 8 7 67 7 8 8 9 88 6 fter rottions: 6 6 9 6 7 7 8 7 8 7 6 77 8 6 7 67 7 8 8 9 88 eletion Fin noe n nge priority to Rotte to ring up il wit lower priority. ontinue until min-ep property ols. Remove lef.
elete ey : 6 UGMNTING T STRUTURS Re LRS, setion. on using R tree wit rning informtion for orer sttistis. Retrieving n element wit given rn etermine te rn of n element Prolem: Mintin summry informtion to support n ggregte opertion on te smllest (or lrgest) eys in O(log n) time. mple: Prefi Sum Given ey, etermine te sum of ll eys given ey (prefi sum). Solution: Store sum of ll eys in sutree t te root of te sutree. 7 6 7 9 8 9 6 6 Key Prefi Sum 6 6 7 9 6 6 7
To ompute prefi sum for ey: Initilize sum to Ser for ey, moifying totl s ser progresses: Ser goes left - leve totl lone Ser goes rigt or ey s een foun - present noe s ey n left il s sum to totl Key is : ( + ) + ( + 6) + ( + ) = 6 Key is : ( + ) + ( + 9) = Key is 6: ( + ) + (6 + ) = Vrition: etermine te smllest ey tt s prefi sum speifie vlue. Uptes to tree: Non-struturl (tt/remove noe) - moify noe n every nestor Single rottion (for prefi sum) Σ Σ Σ Σ Σ Σ+Σ+ Σ Σ Σ Σ (Similr for oule rottion) Generl se - see LRS., espeilly Teorem. Intervl trees (LRS.) - more signifint pplition Set of (lose) intervls [low, ig] - low is te ey, ut uplites re llowe sutree root ontins te m vlue ppering in ny intervl in tt sutree ggregte opertion to support - fin ny intervl tt overlps given intervl [low, ig ] Moify ST ser...
8 if ptr == nil no intervl in tree overlps [low, ig ] if ig ptr->low n ptr->ig low return ptr s n nswer if ptr->left!= nil n ptr->left->m low ptr := ptr->left else ptr := ptr->rigt Uptes to tree - similr to prefi sum, ut reple itions wit mimums