C language jump table (skiplist) implementation, skiplist
I. Introduction
The Skip table (skiplist) is an excellent data structure, which is easy to implement. The complexity of insertion, deletion, and search is O (logN ). The core data structure of LevelDB is implemented by using the jump table, and the sorted set Data Structure of redis is also implemented by using the jump table. Code here: http://flyingsnail.blog.51cto.com/5341669/1020034
Ii. Jump table diagram
Consider an ordered table:
Search for <23, 43, 59> elements from the ordered table. The number of comparisons is <2, 4, 6>.
2 + 4 + 6 = 12 times. Are there any optimized algorithms? The linked list is ordered, but binary search cannot be used. Similar to binary
In the search tree, we extract some nodes as indexes. The following structure is obtained:
Here we extract <14, 34, 50, 72> as a primary index, so that the number of comparisons can be reduced during the search.
We can also extract some elements from the first-level index as a secondary index and change it to the following structure:
There are not many elements here, but they do not reflect the advantages. If there are enough elements, this index structure will reflect the advantages.
This is basically the core idea of table jumping. In fact, it is also an algorithm that exchanges space for time. By adding a forward pointer to each node, this improves the search efficiency.
Jump table
The following structure is the jump table:
-1 indicates INT_MIN, the minimum value of the linked list, 1 indicates INT_MAX, and the maximum value of the linked list.
Table jumping has the following properties:
(1) consists of multiple layers
(2) Each layer is an ordered linked list.
(3) The Bottom-layer (Level 1) linked list contains all elements
(4) If an element appears in the Level I linked list, it also appears in the linked list under Level I.
(5) Each node contains two pointers, one pointing to the next element in the same linked list and the other pointing to the following element.
Iii. Source Code Implementation
https://github.com/begeekmyfriend/skiplist