Trs Apptons o trs Orgnzton rts Attk trs to syst Anyss o tr ntworks Prsng xprssons Trs (rtrv o norton) Don-n strutur Mutstng Dstnton-s orwrng Trnsprnt swts Forwrng ts o prxs t routrs Struturs or nt pntton o gorts su s ps n oton o sont sts 1
Trs Connt n y grp T = (V, E) Equvnt rtrztons ny two vrts n T r onnt y unqu pt T s onnt n E = V - 1 T s y n E = V - 1 n g s rov ro E, tn t rsutng grp s sonnt n g s to E, tn t rsutng grp ontns y Any tr s t st two vrts o gr on Tr: ny two vrts r onnt y unqu pt I T s tr, tn vry pr o vrts u n v r onnt y unqu pt. Proo: Suppos tt u n v r onnt y two pts. Lt x t rst vrtx wr t two pts urt n y t rst vrtx tr x wr ty rg gn. Tn, tr s y nvovng x n y. I vry pr o vrts u n v s r onnt y unqu pt, tn T s tr. Proo: T s onnt. Suppos T s y. Any two nos o t y v two stnt pts twn t.
Root trs Tr wt sp vrtx r root v s t ngor o u n t unqu u-r pt v s prnt o u; u s o v v s n t unqu u-r pt v s n nstor o u; u s snnt o v Vrtx wtout rn s Lngt o t u-r pt s t v o u Hgst v s t pt o t tr A onnt oponnt t tr rov o t root wt o t root s sp vrtx s root tr 3 4 6 5 1 1 5 7 3 4 6 8 9 7 8 9 Bnry trs Root tr su tt vrtx s xu o two rn rn r stngus y ng tr t or rgt T trs root t t t n rgt o t root r nry trs 3
Fu nry tr nur n o vrts t v o nry tr stss t rurrn n 0 = 1 n n -1 or = 0, 1,, pt o t tr n nur o vrts n t tr n tr s u t v n = tr s u t s u t vry v n u tr n = 1 + + + = +1 1; = og (n + 1) 1 = og n Copt nry tr or nry tr t nos rursvy root s 0 t t o no s + 1; rgt s + tr wt n nos s opt t onssts o nos 0, 1,,, n 1 0 pt o opt nry tr s og n pt o nry tr s nvr ss tn og n u tr s opt tr wt +1 1 vrts 1 3 4 5 6 7 8 9 4
-trs tr s -tr vry vrtx tt s not s xty two rn Trvrs o nry trs prorr trvrs o t nry tr vst t root no prorr trvrs o t t sutr prorr trvrs o t rgt sutr norr trvrs o t nry tr norr trvrs o t t sutr vst t root no norr trvrs o t rgt sutr postorr trvrs o t nry tr postorr trvrs o t t sutr postorr trvrs o t rgt sutr vst t root no 5
Exps g prorr: g norr: g postorr: g Prorr: rursv o Prorr(root) root nu tn Vst(root -> no) Prorr(root -> tc) Prorr(root -> rgtc) n Prorr(root) Vst(root -> no) root -> tc nu tn Prorr(root -> tc) n root -> rgtc nu tn Prorr(root -> rgtc) n In Prorr pt o t trvrs s ru y on n rton to Prorr 6
Bnry sr trs strutur to ntn n orr st o ts Bnry tr Evry ky n t t su-tr o t root s sr tn t ky n t root Evry ky n t rgt su-tr o t root s rgr tn t ky n t root Bot t n rgt su-trs r nry sr trs n r oo gu Proposton: gvn nry tr wt n nos n n orr kys tr s unqu ssgnnt o kys to t nos tt k or nry sr ky Oprtons on nry sr kys Srng ky, SrBnSrTr Insrtng ky, InsrtBnSrTr Dtng ky, DtBnSrTr Assng t kys n sort orr (In-orr trvrs o t nry sr tr) 7
Srng ky SrBnSrTr(root, X, oton, oun) oun := s oton := nu urrnt := root w urrnt nu n not oun o oton := urrnt X = urrnt -> ky tn oun := tru s X < urrnt -> ky tn urrnt := urrnt -> tc s urrnt := urrnt -> rgtc n nw Insrtng ky nsrtbnsrtr(root, X) root = nu tn otbntrno(noptr) noptr -> ky := X root := noptr s SrBnSrTr(root, X, oton, oun) not oun tn otbntrno(noptr) noptr -> ky := X X < oton > ky tn oton -> tc := noptr s oton -> rgtc := noptr n n n 8
9 Dtng ky: no g k g ton o k Dtng ky: ntrn sng- no g k g k ton o
Dtng ky: no wt two rn opy o n-orr sussor ton o n-orr sussor g k k k n orr sussor o X s t sst ky grtr tn X Dtng ky: o DtBnSrTr(root, no) no -> tc nu n no -> rgtc nu sussor := no -> rgtc w sussor -> tc nu sussor := sussor -> tc nw no > ky := sussor -> ky no := sussor prnt := no -> prnt n // Now no ponts to t no wt xu // o on to t //ontnuton no -> tc nu tn ppropc := no -> tc s ppropc := no -> rgtc n prnt = nu tn root := ppropc s prnt -> tc = no tn prnt -> tc := ppropc s prnt -> rgtc := ppropc n 10
Prorty quu Coton o nts, o w s prorty vu Oprtons on prorty quu ton o t nt o gst prorty nsrton o n nt Ipntton s n orr st ton s worst-s opxty O(1) nsrton s worst-s opxty O(n) Ipntton s n unorr st ton s worst-s opxty O(n) nsrton s worst-s opxty O(1) (Mn-)Hp Copt nry tr T vu o vry no s not rgr tn t vu o ts t n t vu o ts rgt t o no o nx s nx +1 rgt o no o nx s nx + or > 0, prnt o no o nx s nx ( 1)/ 38 3 0 90 47 40 3 nx 0 1 3 4 5 6 7 8 9 vu 38 3 0 90 47 40 3 11
Insrtng vu nsrt 1 38 3 0 90 38 3 0 90 47 40 3 47 40 3 1 1 38 1 0 90 38 0 90 47 40 3 3 47 40 3 3 Insrtng vu: o InsrtMnHp(A[], psz, x) := psz := ( 1)/ w 0 n A[] > x A[] := A[] := := ( 1)/ nw A[] := x psz := psz + 1 worst-s opxty s O(og n) 1
Dtng t nu vu ton 3 38 3 0 90 38 3 0 90 47 40 3 47 40 3 0 38 3 0 90 38 3 3 90 47 40 47 40 Dtng t nu vu: o RovMnHp(A[], psz) x := A[0] psz := psz 1 tp := A[pSz] oun := s := 1 w psz 1 n not oun < psz - 1 n A[] > A[ + 1] tn := + 1 n tp A[] tn oun := tru worst-s opxty s O(og n) s A[ ( 1)/ ] := A[] := * + 1 n nw A[ ( 1)/ ] := tp rturn x 13
Stnr Trs Coton o strngs ro nt pt no strng s prx o ny otr strng Root tr s r ttrs ro t pt strngs orrspon xty to pts ro root to vs g o r t y n n t r t v w w o r Coprss Trs (PATRICIA) oprss pts n stnr Tr ntr w gort n vw or y t 14