rmup CSE 7: AVL trs rmup: ht is n invrint? Mihl L Friy, Jn 9, 0 ht r th AVL tr invrints, xtly? Disuss with your nighor. AVL Trs: Invrints Intrlu: Exploring th ln invrint Cor i: xtr invrint to BSTs tht nfor ln. AVL Tr Invrints An AVL tr hs th following invrints: Th strutur invrint: All nos hv 0,, or hilrn. Th BST invrint: For ll nos, ll kys in th lft sutr r smllr; ll kys in th right sutr r lrgr Th ln invrint: For ll nos, s (hight (lft)) hight (hight (right)). Qustion: why s (hight (lft)) hight (hight (right))? hy not hight (lft) = hight (right)? ht hppns if w insrt two lmnts. ht hppns? AVL = Alson-Vlsky n Lnis AVL tr invrints rviw AVL tr invrints rviw Qustion: is this vli AVL tr? Qustion: is this lso n AVL tr? 7 0 5 9 7 5 0 9 5
AVL tr invrints rviw Implmnting n AVL itionry Qustion:...n wht out now? How o w implmnt n AVL itionry? gt: Sm s BST! ontinsky: Sm s BST! 7 5 put:??? rmov:??? - 5 9 7 A si xmpl AVL rottion Suppos w insrt,, n. ht hppns? insrt() insrt() insrt() ht o w o now? Hint: thr s only on possil solution. An lgorithm for insrt / put, in piturs: Originl tr Insrt Rott lft (Bln) (Unln!) (Bln!) Rott. 9 0 Prti Prti Prti: insrt, n fix th tr: 5 0 9 7 0 Stp : insrt 5 0 9 7 0
Prti Stp : Strt from th insrt no n mov k up to th root. Fin th first unln sutr. 5 Prti Stp : Rott lft or right to fix. (Hr, w rott right). 5 9 0 9 0 7 7 0 0 A son s... Th two AVL ss Th lin s Now, try this. Insrt,, thn. ht s th issu? insrt n insrt rott lft Th kink s Tr is still unln! 5 Hnling th kink s Lt s try gin A son ttmpt... Insight: Hnling th kink s is hr. Cn w somhow onvrt th kink s into th lin s? insrt,, (unln!) oul-rott: onvrt to lin oul-rott: fix tr Solution: s, us two rottions! 7
Th kink s: rottion Th kink s: rottion Initil tr (Unln) Fix th innr sutr: Aftr fixing th sutr Fix th outr sutr: 9 0 Prti Prti Try insrting,,,, into n AVL tr. rott lft on insrt insrt insrt insrt insrt Prti In summry... oul rottion on, prt oul rottion on, prt In summry... Implmnting AVL oprtions gt: Sm s BST! ontinsky: Sm s BST! put: Do BST insrt, mov up tr, prform singl or oul rottions to ln tr rmov: Eithr lzy-lt or us similr mtho to insrt
A not on implmnttion Anlyzing Arrylist somtims n to rott lft, rott right, oul-rott lft, or oul-rott right. Do w n to implmnt mthos? No: n ru runny y hving n rry of hilrn inst of using lft or right fils. This lts us rfr to hilrn y inx so w only hv to writ two mthos: rott, n oul-rott. An now, for ompltly unrlt topi... (E.g. w n hv rott pt two ints: th inx to th iggr sutr, n th inx to th smllr sutr) 5 Anlyzing ArryList Anlyzing ArryList Exris: mol th worst-s runtim of ArryList s mtho in trms of n, th numr of itms insi th list: puli voi (T itm) { if (rry is full) { rsiz n opy } this.rry[this.siz] = itm; this.siz += ; } whn th rry is not full Answr: T (n) = n + whn th rry is full Qustion: wht s th runtim on vrg? Cor i: ost of rsizing is mortiz ovr th susqunt lls Mtphors: hn you py rnt, tht lrg ost is mortiz ovr th following month hn you uy n xpnsiv mhin, tht lrg ost is mortiz n pys itslf k ovr th nxt svrl yrs So, in th ORST possil s, wht s th runtim? Θ (n). 7 Anlyzing ArryList s whn th rry is not full Our rurrn: T (n) = n + whn th rry is full Snrio: Lt s suppos th rry initilly hs siz k. Lt s lso suppos th rry initilly is t pity. How muh work o w n to o to rsiz on thn fill k up to pity? (k + ) + (k ) = k + k. Not: sin rry ws full, n = k in first rsiz ht is th vrg mount of work on? k + k = + k 9 Anlyzing ArryList s vritions Now, wht if inst of rsizing y ouling, wht if w inrs th pity y 00 h tim? Assuming w r full, how muh work o w o in totl to rsiz on thn fill k up to pity? (k + ) + 99 = k + 00 ht is th vrg mount of work on? k + 00 = k 00 00 + ht is k? k is th vlu of n h tim w rsiz. If w plot this, w ll gt stp-wis funtion tht grows linrly! So, woul in Θ (n). 0
Anlyzing ArryList s vritions Amortiz nlysis This is ll mortiz nlysis. Th thniqu w isuss: Now, wht if inst of rsizing y ouling, w tripl? Assuming w r full, how muh work o w o in totl to rsiz on thn fill k up to pity? (k + ) + (k ) = k + k ht is th vrg mount of work on? k + k = k + So, woul in Θ (). Aggrgt nlysis: Show sris of n oprtions hs n uppr-oun of T (n). Th vrg ost is thn T (n) n. Othr ommon thniqus (not ovr in this lss): Th ounting mtho: Assign h oprtion n mortiz ost, whih my iffr from tul ost. If mortiz ost > tul ost, inur rit. Crit is ltr us to py for oprtions whr mortiz ost < tul ost. Th potntil mtho: Th t strutur hs potntil nrgy, iffrnt oprtions ltr tht nrgy. Hoory, physis mtphors?