Application of skip table - redis

Why choose the skip
table The balanced data structures that are often used at present are: B-tree, red-black tree, AVL tree, Splay Tree, Treep, etc.
Imagine, given you a scratch paper, a pen, and an editor, can you immediately implement a red-black tree
, ? It's difficult, it takes time, and there are many details to consider
. It is quite troublesome to refer to a bunch of trees such as algorithms and data structures, and to refer to the code on the Internet.
Let’s use the skip table. The skip table is a randomized data structure. Currently, open source software Redis and LevelDB both use it.
Its efficiency is comparable to that of the red-black tree and AVL tree, but the principle of the skip table is quite simple, as long as If you are proficient in operating linked lists,
you can easily implement a SkipList.
Searching an Ordered List
Consider an ordered list:
clip_image001
searches for elements < 23, 43, 59 > from the ordered list, and the number of comparisons required is < 2, 4, 6 > respectively, and the total number of comparisons
is 2 + 4 + 6 = 12 times. Is there an optimized algorithm? Linked lists are ordered, but binary search cannot be used. Similar to a binary
search tree, we extract some nodes as an index. The following structure is obtained:
clip_image002
Here we extract < 14, 34, 50, 72 > as the first-level index, so that the number of comparisons can be reduced when searching.
We can also extract some elements from the first-level index and use it as a second-level index to form the following structure:
clip_image003
There are not many elements here, which does not reflect the advantages. If there are enough elements, this index structure can reflect the advantages. .
This is basically the core idea of ​​the skip table. In fact, it is also an algorithm of "exchanging space for time". By adding a forward pointer in each node, the efficiency of search is improved.
Jump table
The following structure is the jump table:
-1 means INT_MIN, the minimum value of the linked list, 1 means INT_MAX, the maximum value of the linked list.
The clip_image005
skip list has the following properties:
(1) It consists of many layers
(2) Each layer is an ordered linked list
(3) The bottommost (Level 1) linked list contains all elements
(4) If an element appears in In the linked list of Level i, the linked list under Level i will also appear.
(5) Each node contains two pointers, one points to the next element in the same linked list, and the other points to the element one level below.
Example of skip list search
clip_image007
: Find element 117
(1) Compare 21, larger than 21, look back
(2) Compare 37, larger than 37, smaller than the maximum value of the linked list, start from the layer below 37
(3) Compare 71, larger than 71, smaller than the maximum value of the linked list, start from the lower layer of 71 to find
(4) compare 85, larger than 85, find
(5) compare 117 from the back, equal to 117, find the node.
The specific search algorithm is as follows:
C code clip_image009
1.
3. find(x) 
4. {
5. p = top;
6. while (1) {
7. while (p->next->key < x)
8. p = p->next;
9. if (p->down == NULL ) 
10. return p->next;
11. p = p->down;
12. }
13. }
Insertion of the skip table
First determine the number of layers K that the element will occupy (in the way of tossing a coin, which is completely random )
and then insert elements in the linked list of each level of Level 1 ... Level K.
Example: Insert 119, K = 2
clip_image011
If K is greater than the number of layers in the linked list, a new layer will be added.
Example: Insert 119, K = 4
clip_image013 When a
coin is tossed to determine K to
insert an element, the number of layers occupied by the element is completely random, which is generated by the following random algorithm:
C code clip_image009[1]
1. int random_level()
2. {
3. K = 1;
4.
5. while (random(0,1))
6. K++;
7.
8. return K;
9. }
is equivalent to doing an experiment of tossing a coin. If you encounter heads, continue tossing, and if you encounter tails, stop, and
use the number of coins tossing K in the experiment as the element occupied. layers. Obviously, the random variable K satisfies the geometric distribution with the parameter p = 1/2, and
the expected value of K is E[K] = 1/p = 2. That is to say, the number of layers of each element, the expected value is 2 layers.
The height of the jump table.
For a skip list of n elements, an experiment is performed when each element is inserted to determine the number of layers K occupied by the element.
The height of the skip list is equal to the maximum K generated in these n experiments, to be continued. . .
Analysis of Space Complexity of Jump List
According to the above analysis, the expected height of each element is 2, and the
expected value of the number of nodes in a jump list of size n is 2n.
Skip List Deletion Find the node containing x
in each layer and delete the node using the standard delete from list method.
Example: delete 71
clip_image015

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=326614657&siteId=291194637