12.07.2015 Views

A Practical Introduction to Data Structures and Algorithm Analysis

A Practical Introduction to Data Structures and Algorithm Analysis

A Practical Introduction to Data Structures and Algorithm Analysis

SHOW MORE
SHOW LESS
  • No tags were found...

Create successful ePaper yourself

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

Sec. 16.2 R<strong>and</strong>omized <strong>Algorithm</strong>s 541head0525 30 31 42 58 62 69head(a)525 30 31 42 58 626901head(b)52530 31 42 58 6269012(c)Figure 16.2 Illustration of the Skip List concept. (a) A simple linked list.(b) Augmenting the linked list with additional pointers at every other node. Tofind the node with key value 62, we visit the nodes with values 25, 31, 58, <strong>and</strong> 69,then we move from the node with key value 58 <strong>to</strong> the one with value 62. (c) Theideal Skip List, guaranteeing O(log n) search time. To find the node with keyvalue 62, we visit nodes in the order 31, 69, 58, then 69 again, <strong>and</strong> finally, 62.public E find(Key searchKey) { // Skiplist SearchSkipNode x = head;// Dummy header nodefor (int i=level; i>=0; i--) // For each level...while ((x.forward[i] != null) && // go forward(searchKey.compareTo(x.forward[i].key()) > 0))x = x.forward[i];// Go one last stepx = x.forward[0]; // Move <strong>to</strong> actual record, if it existsif ((x != null) && (searchKey.compareTo(x.key()) == 0))return x.element();// Got itelse return null;// Its not there}Figure 16.3 Implementation for the Skip List find function.

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

Saved successfully!

Ooh no, something went wrong!