Wednesday, September 29, 2010

Skiplists

Skiplists are probabilistic data structures with many of the same properties as balanced binary search trees. They are analogous to express and local passenger trains and have O(log N) insert and search complexities.

I implemented a skiplist container class in C++ to have the same semantics as STL's std::map<>. Here are some results for insertion of unique integers in random order:


iteration and search tests pass.

N = 25000
avg insert: 13.1927
log_2(N): 14.6096

N = 25000
avg search: 14.503
log_2(N): 14.6096


Another neat property is there isn't an expensive re-balancing operation as seen in red-black and AVL trees. The insert() and delete() operations simply need to update at most log_2(N) pointers. Haven't looked at actual performance of my implementation but I thought it was an interesting exercise.


Here is an example skiplist after 11 inserts (after the jump).