Skip table and Redis

Jump table principle

Jump table is the underlying data structure of Redis ordered set ZSet

First, there is a head node. The data in this head node is null, which means it is the minimum value of this linked list. Even Math.Min is larger than it.

 Then how do we operate when we create a new node? First create a node according to the parameter (say it is 5) and then place it in the corresponding position. That is to find the largest node smaller than it and then put it behind the largest node smaller than it. We currently only have one null node and it is the smallest, so just put it directly behind it.

 Then we start to increase its "height". The minimum number of layers is 1. For the rest, we use a coin toss to decide whether to add a layer if it is heads and not to add it if it is tails (of course, the bottom layer of the computer is definitely not a coin toss, it is implemented by a random algorithm. ) 

If when we decide the number of layers of five, both judgments are positive, then its height is 3

The third layer is three roads. This road is used to connect to the next node (just like a linked list, but the linked list only has one road). You will know why after a while. 

Then  the height of this null must always be consistent with the height of the highest node.  It was 0 at the beginning and now it will become 3. 

 Then we want to add a 6 node. Find the largest node smaller than it, which is 5, then insert it after 5.

Then we randomize its height. If the height is 1

Hey, do you understand? The height of 6 is 1, so the two roads above 5 cannot connect to it. We can go directly to the back.

Another 3-node with a height of 5

The largest node smaller than three is null

so

 Hey, remember to synchronize null nodes

Insert another 7-node

Find node

If we say find node 6

Start from the top level of null. If you find 3 3<6, you have to go to the right. If you go to the right again, it will be empty.

So go down one level from 3, then go right, go to 7 7>6, then you have to go down one level from 3.

3. We have reached 5 5<6. Then stop at this level 5 and go to the right. It is 7 7>6. No way. 

5. No way to go further down.

5 Go further down and find 6

To sum up, to find a node, go right if you can go right. If you can't go right, go down and go right again.

Its time complexity is O(logN) and the worst complexity is O(N)

Jump table in Redis

Skip tables in Redis compared to classic skip tables

1. There are duplicate values

2. There is a rollback pointer at the bottom layer

3. The probability of random height +1 is 25%

4. There is a maximum number of layers. Redis 5.0 is 64 layers, and Redis 7.0 is 32 layers.

QS

1. What is a skip list and how is it different from an ordinary linked list?

A skip list is a special linked list. It has a higher number of levels than a linked list and can achieve log(N) search efficiency.

2. Will inserting data into the skip table affect the layer height of other nodes?

will affect the first null node

Supongo que te gusta

Origin blog.csdn.net/chara9885/article/details/132155674
Recomendado
Clasificación