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.

the search for the position of S 0 hold<strong>in</strong>g the entry are<br />

highlighted <strong>in</strong> blue. The positions removed are drawn<br />

with dashed l<strong>in</strong>es.<br />

Ma<strong>in</strong>ta<strong>in</strong><strong>in</strong>g the Top-most Level<br />

A skip-list S must ma<strong>in</strong>ta<strong>in</strong> a reference to the start position (the top-most, left<br />

position <strong>in</strong> S) as an <strong>in</strong>stance variable, <strong>and</strong> must have a policy for any <strong>in</strong>sertion that<br />

wishes to cont<strong>in</strong>ue <strong>in</strong>sert<strong>in</strong>g a new entry past the top level of S. There are two<br />

possible courses of action we can take, both of which have their merits.<br />

One possibility is to restrict the top level, h, to be kept at some fixed value that is<br />

a function of n, the number of entries currently <strong>in</strong> the dictionary (from the<br />

analysis we will see that h = max{ 10,2 log n} is a reasonable choice, <strong>and</strong><br />

pick<strong>in</strong>g h = 3 logn is even safer). Implement<strong>in</strong>g this choice means that we<br />

must modify the <strong>in</strong>sertion algorithm to stop <strong>in</strong>sert<strong>in</strong>g a new position once we<br />

reach the top-most level (unless logn < log(n + 1), <strong>in</strong> which case we can<br />

now go at least one more level, s<strong>in</strong>ce the bound on the height is <strong>in</strong>creas<strong>in</strong>g).<br />

The other possibility is to let an <strong>in</strong>sertion cont<strong>in</strong>ue <strong>in</strong>sert<strong>in</strong>g a new position as<br />

long as heads keeps gett<strong>in</strong>g returned from the r<strong>and</strong>om number generator. This is<br />

the approach taken <strong>in</strong> Algorithm SkipInsert of Code Fragment 9.11. As we<br />

show <strong>in</strong> the analysis of skip lists, the probability that an <strong>in</strong>sertion will go to a level<br />

that is more than O(logn) is very low, so this design choice should also work.<br />

Either choice will still result <strong>in</strong> the expected O(logn) time to perform search,<br />

<strong>in</strong>sertion, <strong>and</strong> removal, however, which we show <strong>in</strong> the next section.<br />

9.4.2 A Probabilistic Analysis of Skip Lists <br />

As we have shown above, skip lists provide a simple implementation of an ordered<br />

dictionary. In terms of worst-case performance, however, skip lists are not a<br />

superior data structure. In fact, if we don't officially prevent an <strong>in</strong>sertion from<br />

cont<strong>in</strong>u<strong>in</strong>g significantly past the current highest level, then the <strong>in</strong>sertion algorithm<br />

564

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

Saved successfully!

Ooh no, something went wrong!