Skip-List - Must Know

79 阅读1分钟

前瞻

skip-list是一个可快速线性搜索logn的、依靠随机算法的、并发友好的、有序的链表

测试表明,相比于红黑树或者AVL树,跳表的内存占用更高,查找的时间复杂度更高

历史

Normally, the sequential array supports binary search, while the linked list does not, as the array supports accessing data by index but linked list not.

So the skip list actually is a list with binary search feature. How to do that?

Add a new index link list on the base of original link list

image.png

image.png

When u wanna get the data 7, using original list u need to traverse 7 times, but using the 1st index, u just need 4 times.

实现

image.png

只有最下层的链表具有完整的数据, 上面的链表都是索引链

level

在插入节点时, 会根据概率判断需不需要在某一level插入这个节点:

以maxHeight=12(官方推荐),为例,可以得到height为1-12的概率:

层高概率
10.75
20.1875
30.046875
40.0117188
50.00292969
60.000732422
70.000183105
84.57764e-05
91.14441e-05
102.86102e-06
117.15256e-07
121.78814e-07

Ref

  1. https://byte_dance.fei_shu.cn/wiki/wikcnUF6dMWnO1EGNaCJzO4HXKg
  2. https://tech.byte_dance.net/articles/6798863476430209031
  3. https://byte_dance.fei_shu.cn/wiki/wikcnUF6dMWnO1EGNaCJzO4HXKg#tolsED