当前位置:网站首页>Principle of skip table
Principle of skip table
2022-06-23 07:20:00 【51CTO】
Skip list
Jump lists are based on linked lists , A multi-layer index structure is added to the linked list .
The special data result of jump table is Willam Pugh Invented . First appeared in 1990 A paper published in 1987 《Skip Lists: A Probabilistic Alternative to Balanced Trees》
There is a description in the paper :
Skip lists are a data structure that can be used in place of balanced trees.
Skip lists use probabilistic balancing rather than strictly enforced balancing and as a result the algorithms for insertion and deletion in skip lists are much simpler and significantly faster than equivalent algorithms for balanced trees.
- 1.
- 2.
To put it simply , Jump table is a table based on probability .
Let's first look at the structure of an ordinary ordered linked list :

If you want to find 23 Then at least we need to compare 2 Time , lookup 43 Compare 4 Time , lookup 59 Compare 6 Time . Is there any way to solve this problem ? It's easy to think of binary search .
Adopt hierarchical linked list structure , Take some nodes as indexes ,


such as , Extracted 14 34 50 72 As a linked list , lookup 59 When , You can compare 14 24 50 59 common 5 Times found 59 To reduce the number of lookups .
If you add a layer , Basically, you can use the similar dichotomy method to search


Now look at the complete The process of inserting a new element into a fast watch :

Reference code :
public class SkipList {
private static class SkipListNode {
int data;
SkipListNode[] next;
SkipListNode(int d, int level) {
data = d;
next = new SkipListNode[level + 1];
}
}
private int maxLevel;
SkipListNode header;
private static final int INFINITY = Integer.MAX_VALUE;
SkipList(int maxLevel) {
this.maxLevel = maxLevel;
header = new SkipListNode(0, maxLevel);
SkipListNode sentinel = new SkipListNode(INFINITY, maxLevel);
for (int i = 0; i
<
=
maxLevel;
i++)
header.next[i] =
sentinel;
}
public
boolean
find(int
key)
{
SkipListNode
current =
header;
for
(int
i =
maxLevel;
i
>= 0; i--) {
SkipListNode next = current.next[i];
while (next.data
<
key)
{
current =
next;
next =
current.next[i];
}
}
current =
current.next[0];
if
(current.data =
=
key)
return
true;
else
return
false;
}
public
void
insert(int
searchKey,
int
newValue)
{
SkipListNode[]
update =
new
SkipListNode[maxLevel
+
1];
SkipListNode
current =
header;
for
(int
i =
maxLevel;
i
>= 0; i--) {
SkipListNode next = current.next[i];
while (next.data
<
searchKey)
{
current =
next;
next =
current.next[i];
}
update[i] =
current;
}
current =
current.next[0];
if
(current.data =
=
searchKey)
current.data =
newValue;
else
{
int
v =
generateRandomLevel();
SkipListNode
node =
new
SkipListNode(newValue,
maxLevel);
for
(int
i =
0;
i
<= v; i++) {
node.next[i] = update[i].next[i];
update[i].next[i] = node;
}
update = null;
}
}
private int generateRandomLevel() {
int newLevel = 0;
while (newLevel
<
maxLevel
&&
Math.random()
< 0.5)
newLevel++;
return newLevel;
}
public boolean delete(int searchKey) {
SkipListNode[] update = new SkipListNode[maxLevel + 1];
SkipListNode current = header;
for (int i = maxLevel; i >= 0; i--) {
SkipListNode next = current.next[i];
while (next.data
<
searchKey)
{
current =
next;
next =
current.next[i];
}
update[i] =
current;
}
current =
current.next[0];
if
(current.data =
=
searchKey)
{
for
(int
i =
0;
i
<= maxLevel; i++) {
if (update[i].next[i] == current) {
update[i].next[i] = current.next[i];
current.next[i] = null;
} else
current.next[i] = null;
}
return true;
}
return false;
}
public static void main(String[] args) {
}
}
- 1.
- 2.
- 3.
- 4.
- 5.
- 6.
- 7.
- 8.
- 9.
- 10.
- 11.
- 12.
- 13.
- 14.
- 15.
- 16.
- 17.
- 18.
- 19.
- 20.
- 21.
- 22.
- 23.
- 24.
- 25.
- 26.
- 27.
- 28.
- 29.
- 30.
- 31.
- 32.
- 33.
- 34.
- 35.
- 36.
- 37.
- 38.
- 39.
- 40.
- 41.
- 42.
- 43.
- 44.
- 45.
- 46.
- 47.
- 48.
- 49.
- 50.
- 51.
- 52.
- 53.
- 54.
- 55.
- 56.
- 57.
- 58.
- 59.
- 60.
- 61.
- 62.
- 63.
- 64.
- 65.
- 66.
- 67.
- 68.
- 69.
- 70.
- 71.
- 72.
- 73.
- 74.
- 75.
- 76.
- 77.
- 78.
- 79.
- 80.
- 81.
- 82.
- 83.
- 84.
- 85.
- 86.
- 87.
- 88.
- 89.
- 90.
- 91.
- 92.
- 93.
- 94.
- 95.
- 96.
- 97.
- 98.
- 99.
- 100.
- 101.
- 102.
- 103.
- 104.
- 105.
- 106.
- 107.
- 108.
Official account :【 Programmer development community 】

边栏推荐
猜你喜欢

UNET code implementation

Regular expression graph and text ultra detailed summary without rote memorization (Part 1)

Deeplab V3 code structure diagram

How to migrate virtual machines from VirtualBox to hype-v

深度学习系列46:人脸图像超分GFP-GAN

Analysis of personalized learning progress in maker Education

MYSQL牛客刷题

U-Net: Convolutional Networks for Biomedical Image Segmentation

Interpreting the spirit of unity and cooperation in maker Education

深度学习系列47:styleGAN总结
随机推荐
307. area and retrieval - array modifiable
407-栈与队列(232.用栈实现队列、225. 用队列实现栈)
TensorFlow中的数据类型
406 double pointer (27. remove elements, 977. square of ordered array, 15. sum of three numbers, 18. sum of four numbers)
899. ordered queue
JUnit unit test reports an error org junit. runners. model. InvalidTestClassError: Invalid test class ‘xxx‘ . No runnable meth
leetcode210. 课程表 II 207. 课程表 拓扑排序 dfs bfs
316. 去除重复字母
深度学习系列46:人脸图像超分GFP-GAN
SSTable详解
【AI实战】xgb.XGBRegressor之多回归MultiOutputRegressor调参1
csrf攻击在laravel中如何解决
Nacos adapts to oracle11g- modify the source code of Nacos
MySQL (VIII) - explain
【AI实战】xgb.XGBRegressor之多回归MultiOutputRegressor调参2(GPU训练模型)
The List
322. change exchange
[AI practice] data normalization and standardization of machine learning data processing
Nacos适配oracle11g-建表ddl语句
897. incremental sequential search tree