About jump table
Let's put aside the redis, alone understand the next jump table
skiplist data structure Introduction
skiplist, as the name implies, first of all it is a list. In fact, it is developed on the basis of an ordered list.
Let's look at an ordered list, as shown below (left-most node represents an empty gray head node):
In such a linked list, if we want to find some data, then you need to start from scratch to compare one by one, until you find the node that contains the data, or you find the date of a given data is greater than the node (not found). That is, the time complexity is O (n). Similarly, when we want to insert the new data, the same should go through the discovery process to determine the insertion position.
If we increase every two adjacent nodes a pointer so that the next pointer to the next node, as shown below:
- 23 First, compare and 7, and 19 and then compared to larger than they continued relatively backward.
- However, when comparing 23 and 26, less than 26, therefore returned the following list (original list), compared with 22.
- 23 is larger than 22, comparator 26 continues rearwardly and below along the pointer. 23 is smaller than 26, indicating that the inspective data 23 does not exist in the original list, and its insertion position should be between 22 and 26.
Find in this process, due to the newly added pointer, we no longer need to be compared with each node in the list one by one. We need to compare the number of nodes in only about half of the original.
In the same way, we can list on the upper layer of newly generated, a pointer continues to increase for every two adjacent nodes, thereby generating a third list layer. As shown below:
Can be seen from the above skiplist creation and insertion, a number of layers of each node (Level) is a random out, and insert a new node layers does not affect other nodes. Accordingly, the insertion operation only need to modify the node pointers before and after insertion, without the need for a lot of nodes are adjusted. This reduces the complexity of the insertion operation. In fact, this is a very important feature skiplist, which it is inserted in the tree is superior to balance the program. This we will mention later.
According to skiplist structure of the image above, it is easy to understand the origin of the name of this data structure. skiplist, translated into Chinese, can be translated to "jump table" or "jumping table" refers to the first layer except the lowermost chain, it will produce several layers sparse lists, these lists of pointers which intentionally skipped Some of the nodes (and the more nodes list to skip the more senior). This allows us to find the data in the first time at the top of the list to find, and then lower layer by layer, down to the final layer of the first chain to accurately determine the location of the data. In this process, we skipped some of the nodes, which will speed up the search speed.
刚刚创建的这个skiplist总共包含4层链表,现在假设我们在它里面依然查找23,下图给出了查找路径:
需要注意的是,前面演示的各个节点的插入过程,实际上在插入之前也要先经历一个类似的查找过程,在确定插入位置后,再完成插入操作。
至此,skiplist的查找和插入操作,我们已经很清楚了。而删除操作与插入操作类似,我们也很容易想象出来。这些操作我们也应该能很容易地用代码实现出来。
当然,实际应用中的skiplist每个节点应该包含key和value两部分。前面的描述中我们没有具体区分key和value,但实际上列表中是按照key进行排序的,查找过程也是根据key在比较