23.11.2014 Views

Data Structures and Algorithms in Java[1].pdf - Fulvio Frisone

Data Structures and Algorithms in Java[1].pdf - Fulvio Frisone

Data Structures and Algorithms in Java[1].pdf - Fulvio Frisone

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

Describe how to use a map to implement the dictionary ADT, assum<strong>in</strong>g that the<br />

user may attempt to <strong>in</strong>sert entries with the same key.<br />

C-9.2<br />

Suppose we are given two ordered search tables S <strong>and</strong> T, each with n entries<br />

(with S <strong>and</strong> T be<strong>in</strong>g implemented with arrays). Describe an O(log 2 n)-time<br />

algorithm for f<strong>in</strong>d<strong>in</strong>g the kth smallest key <strong>in</strong> the union of the keys from S <strong>and</strong> T<br />

(assum<strong>in</strong>g no duplicates).<br />

C-9.3<br />

Give an O(logn)-time solution for the previous problem.<br />

C-9.4<br />

Design a variation of b<strong>in</strong>ary search for perform<strong>in</strong>g operation f<strong>in</strong>dAll(k) <strong>in</strong> a<br />

dictionary implemented with an ordered search table, <strong>and</strong> show that it runs <strong>in</strong><br />

time O(logn + s), where n is the number of elements <strong>in</strong> the dictionary <strong>and</strong> s is<br />

the size of the iterator returned.<br />

C-9.5<br />

Describe the changes that must be made <strong>in</strong> the pseudo-code descriptions of the<br />

fundamental dictionary methods when we implement a dictionary with a hash<br />

table such that collisions are h<strong>and</strong>led via separate cha<strong>in</strong><strong>in</strong>g, but we add the<br />

space optimization that if a bucket stores just a s<strong>in</strong>gle entry, then we simply<br />

have the bucket reference that entry directly.<br />

C-9.6<br />

The hash table dictionary implementation requires that we f<strong>in</strong>d a prime number<br />

between a number M <strong>and</strong> a number 2M. Implement a method for f<strong>in</strong>d<strong>in</strong>g such a<br />

prime by us<strong>in</strong>g the sieve algorithm. In this algorithm, we allocate a 2M cell<br />

Boolean array A, such that cell i is associated with the <strong>in</strong>teger i. We then<br />

<strong>in</strong>itialize the array cells to all be "true" <strong>and</strong> we "mark off all the cells that are<br />

multiples of 2, 3, 5, 7, <strong>and</strong> so on. This process can stop after it reaches a number<br />

larger than . (H<strong>in</strong>t: Consider a bootstrapp<strong>in</strong>g method for f<strong>in</strong>d<strong>in</strong>g the<br />

primes up to .)<br />

C-9.7<br />

Describe how to perform a removal from a hash table that uses l<strong>in</strong>ear prob<strong>in</strong>g to<br />

resolve collisions where we do not use a special marker to represent deleted<br />

elements. That is, we must rearrange the contents so that it appears that the<br />

removed entry was never <strong>in</strong>serted <strong>in</strong> the first place.<br />

577

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!