CS 360 Exm 2 Fll 2014 Nme 1. The lsses shown elow efine singly-linke list n stk. Write three ifferent O(n)-time versions of the reverse_print metho s speifie elow. Eh version of the metho shoul output the t ontine within the noes of the list in reverse orer, tht is, from k to front. Exmple: if the list ontins elements [4, 9, 1, 6] then the output shoul e 6 1 9 4. lss Noe { int t; Noe next; } lss List { Noe he; voi reverse_print( ); } lss Stk { Stk( ); oolen isempty( ); voi push (int x); int pop( ); }. Use stk s n uxiliry t struture. Do not use ny other t struture, o not use reursion, n o not moify the linke list. [5 points]. Use reursion. Do not use n expliit stk or ny other t struture, n o not moify the linke list. Hint: use helper funtion tht is reursive. [5 points]
. Do not use stk or ny other t struture, n o not use reursion. You my use only O(1) extr spe eyon the linke list. Your metho my temporrily moify the linke list, ut when the metho ens, the linke list must e ore extly the sme s it ws efore the metho ws lle. Hint: reverse the list in ple. [10 points] 2. Drw one inry tree tht yiels ll three of the trversls s shown elow. [8 points] Pre-orer: Post-orer: In-orer: L G Z J T X C M P R A N Y E B H V T J M C P X Z G Y B E N V H A R L G T J Z C M X P L Y N B E A H V R
3. A stnr Queue implementtion is provie elow. Complete the lss MultipleQueues, whih represents ritrrily mny queues, y using one instne Q of lss Queue s shown. In lss MultipleQueues, istint queues re hieve y speifying istint qnum vlues. The expete ehvior of methos enqueue n equeue is illustrte in n exmple elow. The enqueue metho must tke O(1) time, ut equeue my tke ω(1) time. [15 points] lss Queue { Queue( ); int size ( ); voi enqueue (int z); int equeue( ); } lss MultipleQueues { Queue Q; MultipleQueues( ) { Q = new Queue( ); } voi enqueue (int qnum, int z); // write this metho int equeue (int qnum); // write this metho } MultipleQueues M = new MultipleQueues( ); M.enqueue (7, 30); M.enqueue (2, 40); M.enqueue (7, 50); M.enqueue (2, 60); print (M.equeue (2)); print (M.equeue (2)); // outputs: 40 60 print (M.equeue (7)); print (M.equeue (7)); // outputs: 30 50
4. Answer the following questions out running times of list opertions. [25 points]. Stte the worst-se θ running time for n effiient implementtion of eh list opertion shown elow, when using eh speifie t struture. (x is vlue, n p is position.) Fin (x) After (p) Before (p) Insert (x) InsertAfter (p,x) InsertBefore (p,x) InsertFirst (x) InsertLst (x) Remove (x) Remove (p) RemoveFirst ( ) RemoveLst ( ) Unsorte singly-linke list, with link to he ut no link to til noe Unsorte ouly-linke list, with links to oth he n til noes Unsorte non-irulr rry, where rry inex k yiels the rnk k element. For whih opertion(s) oes the θ running time hnge if we use singly-linke list with links to oth he n til noes, rther thn singly-linke list with link to only the he noe? Also stte eh suh opertions running time for singly-linke list with links to oth he n til noes.. For whih opertion(s) oes the θ running time hnge if we use irulr rry rther thn non-irulr rry? Also stte eh suh opertions running time for irulr rry.. Whih opertion(s) shoul not e provie if the elements in the t struture must e mintine in sorte orer? e. For whih opertion(s) oes the θ running time hnge if we use sorte rry rther thn n unsorte rry? Also stte eh suh opertions running time for sorte rry.
5. Consier hsh tle with N slots, where N 5. [6 points]. Use liner proing n insert 2 rnom keys. Determine the proility tht these 2 keys will oupy 2 jent slots (possily with wrproun).. Use liner proing n insert 3 rnom keys. Determine the proility tht these 3 keys will oupy 3 jent slots (possily with wrproun).. Use seprte hining n insert 3 rnom keys. Determine the proility tht ll 3 keys re ple in the sme hin.. Use seprte hining n insert 3 rnom keys. Determine the proility tht ll 3 keys re ple in ifferent hins. e. Use seprte hining n insert 3 rnom keys. Determine the proility tht extly 2 of these 3 keys re ple in the sme hin.
6. Strt with the -lk tree shown elow whih hs keys 3, 4, 5. First insert keys 6, 7, 8, 9, 10, 11, 12 (in tht orer) n rw the tree fter eh insert. Next remove keys 3, 4, 5, 6, 7 (in tht orer) n rw the tree fter eh remove. Clerly inite the olor of eh noe (=lk, r=). Mke sure tht eh tree you rw is properly forme -lk tree. If you rw ny intermeite steps, irle the trees tht shoul e gre. [12 points] 4 3 r 5 r
7. Strt with the AVL tree shown elow whih hs keys 5, 10. First insert keys 6, 7, 8, 9, 0, 4, 3, 2, 1 (in tht orer) n rw the tree fter eh insert. Next remove keys 3, 10, 9 (in tht orer) n rw the tree fter eh remove. It is permitte ut not requi to show the height of eh noe. Mke sure tht eh tree you rw is properly forme AVL tree. If you rw ny intermeite steps, irle the trees tht shoul e gre. [12 points] 5 10
8. Strt with the sply tree shown elow whih hs key 5. First insert keys 6, 3, 4, 1, 2, 9, 7, 8 (in tht orer) n rw the tree fter eh insert. Next remove keys 1, 9, 6, 7 (in tht orer) n rw the tree fter eh remove. Mke sure tht eh tree you rw is properly forme sply tree. If you rw ny intermeite steps, irle the trees tht shoul e gre. [12 points] 5
Insertion into -lk tree Getting strte: Rehing the root: 0. x 3. root root lef lef lef Restruturing (single or oule rottion): 1. 1. 1. 1. Reoloring: 2. 2. 2. 2.
Removl from -lk tree Getting strte: 0. x lef 0. x lef efiient lef lef lef lef 0. x w 0. x y w lef lef lef lef y lef lef lef lef lef lef Restruturing (single or oule rottion): 1. ny sme ny 1. sme efiient efiient 1. ny sme 1. ny sme e efiient e e efiient e `
Removl from -lk tree (ontinue) Reoloring: 2. 2. efiient efiient 2. efiient 2. efiient efiient efiient Ajustment (single rottion): 3. 3. efiient efiient efiient efiient [Alwys followe y 1, 1, 1, 1, 2, or 2] Rehing the root: 4. root efiient root
AVL tree: insertion n removl 1. Y, h+3 1. U, h+3 Right rotte t Y U, h+2 Z, h T, h Y, h+2 Left rotte t U T, h+1 W W Z, h+1 2. Y, h+3 Left rotte t U, 2. U, h+3 Right rotte t Y, U, h+2 Z, h h Right rotte t Y T, h Y, h+2 th Left rotte t U T, h W, h+1 W, h+1 Z, h Sply tree: insertion n removl 1. Zig root X P Right rotte t P 1. Zg root P X Left rotte t P 2. G P Right rotte t G, 2. Zigzig Zgzg G P Left rotte t G, then Left rotte t P X Right rotte then t P X 3. P G X Left rotte t P, Right th rotte t G 3. Zigzg Zgzig G X P Right rotte t P, th Left rotte t G