Skiplists ‐ Basic Structure - WilfullMurder/DataStructures-Java GitHub Wiki
Theoretically, a skiplist is a sequence of singly-linked lists L0,...,Lh where each list Lr contains a subset of the items Lr-1. Starting with the input list L0 that contains n items we then construct L1 from L0. L2 from L1, and so forth. The items in Lr are obtained by tossing a coin for each element, x, in Lr-1 and including x in Lr if the coin is heads. This process ends once we have created a list Lr that is empty. An example of a skiplist is shown in Fig.1.
At the head of every list is a node, the sentinel, which acts as a dummy node (like the dummy node in a doubly-linked list) for the list. The foundational property of skiplists is that there is a short path, the search path, from the sentinel in Lh to every node in L0. Fabricating the search path for a node, u, is straightforward (see Fig.2), we simply start at the top left of corner of the skiplist (corresponding to the sentinel in Lh) and always go right unless that would overshoot u, in which case we step down into the list below.
More specifically, to fabricate the search path for the node, u, in L0, we begin at the sentinel, w, in Lh. Then, we examine w.next. If w.next contains an item that appears before u in L0, we then set w=w.next. Else, we move down and continue the search at the occurence of w in the list Lh-1, continuing in this manner until we find the predecessor of u in L0.
The expected length of the search path for any node, u, in L0 is at most 2logn+O(1)=O(logn)
A space-efficient method of implementing a skiplist is to define a node, u, that incorporates a data value, x, and an array of pointers where u.next[i] points to u's successor in the list Li. So, the data x in a node is referenced only once, even though x may appear in several lists.
The sections Skiplists ‐ an efficient Sorted Set and Skiplists ‐ an efficient Random‐Access List consider two different implementations of skiplists. In each implementation, L0 stores the main structure (a list or sorted set of elements). The main difference between these structures is in how a search path is navigated; specifically, they diverge in how they decide if a search path should go down into Lr-1 or go right within Lr.