I implemented a skip list in C++(following pseudo code in this paper). The skip list itself provided simple set interface: contains, insert, remove I also implemented a map interface using skip list. I compared performance of Skip List with STL set and map. STL’s algorithm is about twice faster than skip list. Detail of implementation and experiment data comes later…

#### Implementation

The motivation of Skip List is simple. Linked List has *O(n)* random access time, while it can perform fast deletion given node location. Array have *O(1)* random access, while deletion is *O(n)*. Thus it is nature to look for something in between, say, *O(log n)* for random accessing and deleting. Binary search tree is one such thing, while it loss the linear structure, and not so intuitive. Skip List is a more intuitive, and it has *O(log n)* complexity for random accessing and deletion on average. Skip List is based on a sorted Linked List, thus we can expect efficient deletion. If we can have efficient accessing, then we are done. Efficient accessing is achieved by adding extra pointers to let us “skip” multiple nodes instead of going through one by one. Ideally, as shown in figure above, each node got several levels, each level has a pointer, which pointing to next node reach that level. Most nodes has low height, and few are high. If we can make sure that the number of nodes with height _h_ grow reverse exponentially with _h_, then we can access each node in *O(log n)* time on average. Accessing a node is done by starting from highest list head with level, go along the list at that level until the next element is larger than or equal to the element you need. When the next one is larger, go one level down, and continue going along. Repeat this until you are at level 1. By now, either your next element at level 1 is the element you are looking for, or search fails.

1 | Node* search(const T& x,vector<Node*>* update=0){ |

This is the *search* method, the core of all features. *next* is a vector of pointers, which correspond to the “pointer tower” at each node.

1 | void insert(const T& x){ |

Insertion is easy: first call *search*, if the element already exist, overwrite it with new value(maybe not exactly the same, but == operator returns true). If not exist, then insert the node into the list, and maintain adjacent pointers(by using vector *update*, detail refer to the paper).

1 | int randomLevel(){ |

*randLevel* generate random number satisfying the required distribution. Note that to avoid single node has too high level, the random number should be no greater than current level + 1.

1 | void remove(const T& x){ |

*remove* is similar to *insert*, except that only one side of pointers need to be maintained. The removed node is deleted directly, so no need to maintain out going pointers.

#### Experiment 1

- Insert n(from 10000 to 100000) random numbers, repeat 10 times, find the average time for each n.
- Remove n(from 10000 to 100000) random numbers, repeat 10 times, find the average time for each n.
- Combination of insertion and deletion for n(from 10000 to 100000) random numbers with 100% hit rate(no insert same element, or remove element not exist in the list), repeat 10 times, find the average time for each n.

The image above shows the result of experiment 1. X axis is n, Y axis is average time cost.

- The running time almost grow linearly with n for insertion and deletion, although theoretical complexity is O(log n).
- Deletion is faster than Insertion. This is because insertion need to maintain pointers on both sides of inserted node, while deletion only maintain those nodes pointing to the deleted node(only one side).
- Because of combined operation, the average size of skip list is small, thus the code is faster