Because the `binary search`

depends on the characteristics of array random access, it can only be implemented with `arrays`

. If the data is stored in a `linked list`

, is it really impossible to use the `binary search`

algorithm?

In fact, we only need to modify the linked list to support a search algorithm similar to `"binary search"`

. We call the data structure after the transformation “Skip list”(跳表).

It is a dynamic data structure with excellent performance in all aspects. It can support fast insertion, deletion, and search operations. It is not complicated to write, and can even replace Red-black trees.

## How to understand Skip list

For a `single linked list`

, even if the data stored in the `linked list`

is ordered, if we want to find a certain data in it, we can only traverse the `linked list`

from beginning to end. This way the search efficiency will be very low, the `time complexity`

will be very high, it is O(n).

The two nodes extract a node to the upper level, and we refer to the extracted level as the index or index layer. The down in the figure represents the down pointer, pointing to the next level node.

If we are looking for a node now, say 16 . We can traverse the index layer first. When traversing to the node with a value of 13 in the index layer, we find that the next node is 17, and the node 16 to be found must be between the two nodes. Then we descend to the original linked list layer by the down pointer of the index layer node and continue traversing. At this time, we only need to traverse 2 nodes, we can find the node with the value equal to 16. Thus, if you want to find 16 and need to traverse 10 nodes, you only need to traverse 7 nodes.

From this example, we can see that after adding a layer of index, the number of nodes that need to traverse a node is reduced, which means that the search efficiency is improved. Then what if we add another level of index? Will efficiency increase more?

Similar to the way the first level index is built before, we base the first level index on the two nodes and extract a node to the second level index. Now let’s look up 16 again, we only need to traverse 6 nodes, and the number of nodes that need to be traversed is reduced.

The above example does not have a large amount of data, so even if a two-level index is added, the improvement in search efficiency is not obvious. The following figure is a linked list of 64 nodes. According to the above idea, a five-level index is established.

As can be seen from the figure, when there is no index, the search 62 needs to traverse 62 nodes. Now only need to traverse 11 nodes, the speed is much improved! Therefore, when the length n of the linked list is relatively large, such as 1000 and 10000, the efficiency of the search will be very obvious after the index is built.

## How fast is the query with Skip list?

The execution efficiency of the algorithm can be measured by time complexity. We know that the time complexity of querying a data in a single linked list is O(n). So in Skip list with a multi-level index, what is the time complexity of querying a certain data?

Let’s look at such a question. If there are n nodes in the list, how many levels of index will there be?

According to the above, the number of nodes in the first level index is about n/2, the number of nodes in the second level index is about n/4, and the number of nodes in the third level index is about n/8. By analogy, that is, the number of nodes of the k-th index is 1/2 of the number of nodes of the k-1th index, and the number of the k-th index nodes is n / (2^k).

Suppose the index has h levels, and the most advanced index has 2 nodes. By the above formula, n/(2^h)=2 can be obtained, thereby obtaining h=log2(n-1). If the original linked list layer is included, the height of the entire Skip list is log2n. When we query a certain data in the Skip list, if each layer has to traverse m nodes, the time complexity of querying a data in the Skip list is O(m * logn).

What is the value of this m? According to the previous index structure, each level of our index only needs to traverse up to 3 nodes, that is, m = 3.

Suppose the data we are looking for is x. In the k-th index, after we traverse to the y node, we find that x is greater than y and less than the subsequent z, so we drop from the k-th index through the down pointer of y. Go to the k-1 level index. In the k-1th index, there are only 3 nodes (including y and z) between y and z. Therefore, we only need to traverse 3 nodes in the k-1 level index, and so on, each level index only needs to traverse 3 nodes at most.

Through the above analysis, we get m = 3. So the time complexity of querying arbitrary data in the Skip list is O(logn). The time complexity of this lookup is the same as the binary search. In other words, we actually implemented a binary search based on a single-linked list. However, the efficiency of this query is improved, provided that many levels of indexing are established.

## Is the Skip list a waste of memory?

Compared to a simple single linked list, Skip list needs to store multiple levels of index, which definitely consumes more storage space. How much extra storage does it take in the end?

The spatial complexity analysis of Skip list is not difficult. Assuming that the original linked list size is n, the first level index has about n/2 nodes, the second level index has about n/4 nodes, and so on. When you increase the level, you cut it by half until you have 2 nodes left. If we calculate the number of nodes in each layer of index, it is a `geometric sequence`

.

The sum of the nodes of these levels of index iis n/2 + n/4 + n/8 + … + 8 + 4 + 2 = n-2. Therefore, the space complexity of the Skip list is O(n). That is, if we construct a single-linked list of n nodes into a Skip list, we need to use an additional storage space close to n nodes.

If we have three or five nodes, draw a node to the superior index. Although the space complexity is still O(n), the index construction method of the two nodes above is to reduce the storage space of the index node by half.

## Efficient dynamic insertion and deletion

We know that in a single-linked list, once the position to be inserted is located, the time complexity of inserting the node is very low. It is O(1). However, in order to ensure the order of the data in the original linked list, we need to find the location to be inserted first, this search operation will be more time consuming.

For a purely singly linked list, you need to iterate through each node to find the location of the insertion. However, for the Skip list, we have said that the time complexity of finding a node is O(logn), so here is to find the location where a certain data should be inserted. The method is similar, and the time complexity is also O(logn).

If this node also appears in the index, in addition to deleting the nodes in the original linked list, we also delete the nodes in the index. Because the delete operation in the singly linked list needs to get the predecessor node of the deleted node, then the deletion is completed by the pointer operation. So when looking for the node to delete, be sure to get the precursor node. Of course, if we use a doubly linked list, we don’t need to consider this issue.

## Skip list index update

When we keep inserting data into the Skip list, if we don’t update the index, there may be a lot of data between the two index nodes. In extreme cases, Skip list will also degenerate into a singly linked list.

As a dynamic data structure, we need some means to maintain the balance between the index and the original linked list size. That is to say, if there are more nodes in the linked list, the index nodes will be increased accordingly, avoiding complexity degradation, and performance degradation of lookup, insertion, and deletion operations.

If you understand the balanced binary tree like red black tree and AVL tree, you know that they maintain the balance of the size of the left and right subtrees by rotating left and right, and Skip list maintains the aforementioned “balance” through random functions.

When we insert data into the Skip list, we can choose to insert this data into the partial index layer at the same time. How to choose which index layer to join?

We use a random function to determine which level of index to insert this node into. If the random function generates the value k, then we will add this node to the k-level index from the first level to the kth level.

The choice of random function is very particular. From the probabilistic point of view, the index size and data size balance of Skip list can be guaranteed, and the performance is not degraded excessively.

## Java code for Skip list

1 | public class SkipList { |