Skip list is a data structure that implements dictinaries. It assumes that random numbers can be generated easily in any computer systems and uses randomization in its structure.
The expected worst case performance of a skip list is similar to that of an AVL tree.
A skip list S for dictionary D consists a series of lists {S0, S1, ..., Sh} where S0 contains every item of D plus the two special items with keys -∞ and +∞. For 1 <= i <= (h-1), list Si contains a randomly generated subset (usually half) of the items in list Si-1 plus the two special items. List Sh contains only the two special items.
Traversal operations in skip list:
Main operations:
Position * SkipSearch(k) { initialize p to be the top-most and left position of the skip list (i.e., p is placed on the left node of Sh) while (p->below != NULL) { p = p->below; while (p->after->key <= k) { p = p->after; } } return p; }
Data SkipLookup(k) { p = SkipSearch(k); if (p->key == k) return p->data else Exception 404 "No such item" }
void SkipInsert(k, obj) { p = SkipSearch(k); if (p->key == k) Exception "Duplicate Key" else { th = 0; q = insertAfterAbove(p, NULL, (k, obj)); while (th < h && random() < 0.5) { while (p->above == NULL) p = p->before; p = p->above; q = insertAfterAbove(p, q, (k, NULL)); th++; } if (th == h) { add a top list; // with only the two special elements h++; } } }
The probability that the height of the newly inserted tower rises to h is 2-h.
Data & SkipRemove(k) { p = SkipSearch(k); if (p->key != k) Exception 404 "No such item else { Data *temp = p->obj Position *q; // delete the whole tower while (p != NULL) { p->before->after = p->after; p->after->before = p->before; q = p; p = p->above; delete q; } return (*temp); } }