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.

q, (k, v)) that <strong>in</strong>serts a position stor<strong>in</strong>g the entry (k, v) after position p (on the same<br />

level as p) <strong>and</strong> above position q, return<strong>in</strong>g the position r of the new entry (<strong>and</strong><br />

sett<strong>in</strong>g <strong>in</strong>ternal references so that next, prev, above, <strong>and</strong> below methods will<br />

work correctly for p, q, <strong>and</strong> r). The expected runn<strong>in</strong>g time of the <strong>in</strong>sertion<br />

algorithm on a skip list with n entries is O(logn), which we show <strong>in</strong> Section 9.4.2.<br />

Code Fragment 9.11: Insertion <strong>in</strong> a skip list. Method<br />

co<strong>in</strong>Flip() returns "heads" or "tails", each with<br />

probability 1/2. Variables n, h, <strong>and</strong> s hold the number<br />

of entries, the height, <strong>and</strong> the start node of the skip<br />

list.<br />

Figure 9.11: Insertion of an entry with key 42 <strong>in</strong>to the<br />

skip list of Figure 9.9. We assume that the r<strong>and</strong>om<br />

"co<strong>in</strong> flips" for the new entry came up heads three<br />

times <strong>in</strong> a row, followed by tails. The positions visited<br />

are highlighted <strong>in</strong> blue. The positions <strong>in</strong>serted to hold<br />

562

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

Saved successfully!

Ooh no, something went wrong!