Introduction to Skiplist

Get introduced to a new data structure, the skiplist.

We'll cover the following

Skiplist overview

Here, we discuss a beautiful data structure: the skiplist, which has a variety of applications. Using a skiplist we can implement a List that has O(logn)O(\log n) time implementations of get(i), set(i, x), add(i, x), and remove(i). We can also implement an SSet in which all operations run in O(logn)O(\log n) expected time.

The efficiency of skiplists relies on their use of randomization. When a new element is added to a skiplist, the skiplist uses random coin tosses to determine the height of the new element. The performance of skiplists is expressed in terms of expected running times and path lengths. This expectation is taken over the random coin tosses used by the skiplist. In the implementation, the random coin tosses used by a skiplist are simulated using a pseudo-random number (or bit) generator.

Skiplist structure

Conceptually, a skiplist is a sequence of singly-linked lists L0,,Lh.L_0,\cdots,L_h. Each list LrL_r contains a subset of the items in Lr1L_{r−1}. We start with the input list L0L_0 that contains nn items and construct L1L_1 from L0,L2L_0, L_2 from L1L_1, and so on. The items in LrL_r are obtained by tossing a coin for each element, xx, in Lr1L_{r−1} and including xx in LrL_r if the coin turns up as heads. This process ends when we create a list LrL_r that is empty. An example of a skiplist is shown below:

Create a free account to access the full course.

By signing up, you agree to Educative's Terms of Service and Privacy Policy