Understanding redis transfer table

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:

So that all new pointers increased even become a new list, but the number of nodes it contains only half the original (figure above 7, 19, 26). Now when we want to find data, you can start to find along this new list. When it comes to big unknown origin than the data node, and then back to the original linked list to find out. For example, we want to find 23, the direction of the path to find the winning red pointer is carried out along the following figure:

 

  • 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:

 

 

It is designed for out skiplist inspired by the idea of ​​such a multi-layer list. In fact, the list generated above manner, the number of nodes in each layer above list is half the number of nodes in the layer below, so that the search process is very similar to a binary search, to find that the time complexity can be reduced to O (log n). However, this method has a big problem when the data is inserted. After inserting a new node, it will disrupt the number of nodes on the list of two vertically adjacent stringent 2: 1 correspondence. To maintain this correspondence relationship, it is necessary to all the nodes behind the newly inserted node (also including a newly inserted node) re-adjustment, which will degenerate into a re-time complexity O (n). Delete data also have the same problem
skiplist To avoid this problem, it does not require the number of nodes between the vertically adjacent two chain strict correspondence relation, but a random number of layers (Level) for each node. For example, a node of a random number of layers is 3, then put it into the link layer 1 to layer 3 of these three layers list. For clarity, the figure shows how the process so as to form a skiplist by a step of inserting operation (click to enlarge):

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在比较



Guess you like

Origin www.cnblogs.com/zh718594493/p/12111949.html