SkipList 跳表的原理以及实现

今天要介绍一个这样的数据结构:

  1. 单向链接
  2. 有序保存
  3. 支持添加、删除和检索操作
  4. 链表的元素查询接近线性时间

——跳跃表 Skip List

一、普通链表

对于普通链接来说,越靠前的节点检索的时间花费越低,反之则越高。而且,即使我们引入复杂算法,其检索的时间花费依然为O(n)。为了解决长链表结构的检索问题,一位名叫William Pugh的人于1990年提出了跳跃表结构。基本思想是——以空间换时间。

二、简单跳跃表(Integer结构)

跳跃表的结构是多层的,通过从最高维度的表进行检索再逐渐降低维度从而达到对任何元素的检索接近线性时间的目的O(logn)。

如图:对节点8的检索走红色标记的路线,需要4步。对节点5的检索走蓝色路线,需要4步。由此可见,跳跃表本质上是一种网络布局结构,通过增加检索的维度(层数)来减少链表检索中需要经过的节点数。理想跳跃表应该具备如下特点:包含有N个元素节点的跳跃表拥有log2N层,并且上层链表包含的节点数恰好等于下层链表节点数的1/2。但如此严苛的要求在算法上过于复杂。因此通常的做法是:每次向跳跃表中增加一个节点就有50%的随机概率向上层链表增加一个跳跃节点,并以此类推。

接下来,我们做如下规范说明:

  1. 跳跃表的层数,我们称其维度。自顶向下,我们称为降维,反之亦然。
  2. 表中,处于不同链表层的相同元素。我们称为“同位素”。
  3. 最底层的链表,即包含了所有元素节点的链表是L1层,或称基础层。除此以外的所有链表层都称为跳跃层。

以下是代码实现

  1. #pragma once
  2. #ifndef SKIPLIST_INT_H_
  3. #define SKIPLIST_INT_H_
  4. #include <cstdlib>     /* srand, rand */
  5. #include <ctime>       /* time */
  6. #include <climits>     /* INT_MIN */
  7. /* 简单跳跃表,它允许简单的插入和删除元素,并提供O(logn)的查询时间复杂度。 */
  8. /*
  9.     SkipList_Int的性质
  10.     (1) 由很多层结构组成,level是通过一定的概率随机产生的,基本是50%的产生几率。
  11.     (2) 每一层都是一个有序的链表,默认是升序,每一层的链表头作为跳点。
  12.     (3) 最底层(Level 1)的链表包含所有元素。
  13.     (4) 如果一个元素出现在Level i 的链表中,则它在Level i 之下的链表也都会出现。
  14.     (5) 每个节点包含四个指针,但有可能为nullptr。
  15.     (6) 每一层链表横向为单向连接,纵向为双向连接。
  16. */
  17. // Simple SkipList_Int 表头始终是列表最小的节点
  18. class SkipList_Int {
  19. private:
  20.     /* 节点元素 */
  21.     struct node {
  22.         node(int val = INT_MIN) :value(val), up(nullptr), down(nullptr), left(nullptr), right(nullptr) {}
  23.         int value;
  24.         // 设置4个方向上的指针
  25.         struct node* up; // 上
  26.         struct node* down; // 下
  27.         struct node* left; // 左
  28.         struct node* right; // 右
  29.     };
  30. private:
  31.     node* head; // 头节点,查询起始点
  32.     int lvl_num; // 当前链表层数
  33.     /* 随机判断 */
  34.     bool randomVal();
  35. public:
  36.     SkipList_Int(): lvl_num(1) {
  37.         head = new node();
  38.     }
  39.     /* 插入新元素 */
  40.     void insert(int val);
  41.     /* 查询元素 */
  42.     bool search(int val);
  43.     /* 删除元素 */ 
  44.     void remove(int val);
  45. };
  46. #endif // !SKIPLIST_INT_H_

我们需要实现插入、查询和删除三种操作。为了保证所有插入的元素均处于链表头的右侧。我们使用INT_MIN作为头部节点。并且为了方便在不同维度的链表上转移,链表头节点包含up和down指针,普通整型节点之间的只存在down指针,水平方向上只存在right指针。

  1. #include "SkipList_Int.h"
  2.  
  3. static unsigned int seed = NULL; // 随机种子
  4.  
  5. bool SkipList_Int::randomVal() {    
  6.     if (seed == NULL) {
  7.         seed = (unsigned)time(NULL);
  8.     }
  9.     ::srand(seed);
  10.     int ret = ::rand() % 2;
  11.     seed = ::rand();
  12.     if (ret == 0) {
  13.         return true;
  14.     }
  15.     else {
  16.         return false;
  17.     }
  18. }
  19.  
  20. void SkipList_Int::insert(int val) {
  21.     /* 首先查找L1层 */
  22.     node* cursor = head;
  23.     node* new_node = nullptr;
  24.     while (cursor->down != nullptr) {
  25.         cursor = cursor->down;
  26.     }
  27.     node* cur_head = cursor; // 当前层链表头
  28.     while (cursor->right != nullptr) {
  29.         if (val < cursor->right->value && new_node == nullptr) {
  30.             new_node = new node(val);
  31.             new_node->right = cursor->right;
  32.             cursor->right = new_node;
  33.         }
  34.         cursor = cursor->right; // 向右移动游标
  35.     }
  36.     if (new_node == nullptr) {
  37.         new_node = new node(val);
  38.         cursor->right = new_node;
  39.     }
  40.     /* L1层插入完成 */
  41.     /* 上层操作 */
  42.     int cur_lvl = 1; // 当前所在层
  43.     while (randomVal()) {
  44.         cur_lvl++;
  45.         if (lvl_num < cur_lvl) { // 增加一层
  46.             lvl_num++;
  47.             node* new_head = new node();
  48.             new_head->down = head;
  49.             head->up = new_head;
  50.             head = new_head;
  51.         }
  52.         cur_head = cur_head->up; // 当前链表头上移一层
  53.         cursor = cur_head; // 继续获取游标
  54.         node* skip_node = nullptr; // 非L1层的节点
  55.         while (cursor->right != nullptr) {
  56.             if (val < cursor->right->value && skip_node == nullptr) {
  57.                 skip_node = new node(val);
  58.                 skip_node->right = cursor->right;
  59.                 cursor->right = skip_node;
  60.             }
  61.             cursor = cursor->right;
  62.         }
  63.         if (skip_node == nullptr) {
  64.             skip_node = new node(val);
  65.             cursor->right = skip_node;
  66.         }
  67.         while (new_node->up != nullptr) {
  68.             new_node = new_node->up;
  69.         }
  70.         /* 连接上下两个节点 */
  71.         skip_node->down = new_node;
  72.         new_node->up = skip_node;
  73.     }
  74. }
  75.  
  76. bool SkipList_Int::search(int val) {
  77.     node* cursor = nullptr;
  78.     if (head == nullptr) {
  79.         return false;
  80.     }
  81.     /* 初始化游标指针 */
  82.     cursor = head;
  83.     while (cursor->down != nullptr) { // 第一层循环游标向下
  84.         while (cursor->right != nullptr) { // 第二层循环游标向右
  85.             if (val <= cursor->right->value) { // 定位元素:于当前链表发现可定位坐标则跳出循环...
  86.                 break;
  87.             }
  88.             cursor = cursor->right;
  89.         }
  90.         cursor = cursor->down;
  91.     }
  92.     while (cursor->right != nullptr) { // L1层循环开始具体查询
  93.         if (val > cursor->right->value) {
  94.             cursor = cursor->right; // 如果查找的值大于右侧值则游标可以继续向右
  95.         } 
  96.         else if (val == cursor->right->value) { // 如果等于则表明已经找到节点
  97.             return true;
  98.         }
  99.         else if (val < cursor->right->value) { // 如果小于则表明不存在该节点
  100.             return false;
  101.         }
  102.     }
  103.     return false; // 完成遍历返回false;
  104. }
  105.  
  106. void SkipList_Int::remove(int val) {
  107.     node* cursor = head; // 获得游标
  108.     node* pre_head = nullptr; // 上一行的头指针,删除行时使用
  109.     while (true) {
  110.         node* cur_head = cursor; // 当前行头指针
  111.         if (pre_head != nullptr) {
  112.             cur_head->up = nullptr;
  113.             pre_head->down = nullptr; // 解除上下级的指针
  114.             delete pre_head;
  115.             pre_head = nullptr; // 指针归0
  116.             lvl_num--; // 层数-1
  117.             head = cur_head; // 重新指定起始指针
  118.         }
  119.         while (cursor != nullptr && cursor->right != nullptr) { // 在当前行中查询val
  120.             if (val == cursor->right->value) {
  121.                 node* delptr = cursor->right;
  122.                 cursor->right = cursor->right->right;
  123.                 delete delptr; // 析构找到的节点
  124.             }
  125.             cursor = cursor->right;
  126.         }
  127.         if (cur_head->right == nullptr) { // 判断当前行是否还存在其它元素,如果不存在则删除该行并将整个跳跃表降维
  128.             pre_head = cur_head;
  129.         }
  130.         if (cur_head->down == nullptr) {
  131.             break;
  132.         }
  133.         else {
  134.             cursor = cur_head->down;
  135.         }
  136.     }
  137. }

以上代码演示的是简单整型跳跃表的具体实现方法。它演示了一种最基本的跳跃,而它的问题也显而易见。如果非整型对象,我们如何设计链表头节点?普通对象如何实现排序?以及如何比较相等?为了解决这些问题,我们需要设计一种能够支持各种类型对象的跳跃表。我们的思路是:

  1. 跳跃表应该支持泛型结构
  2. 排序规则由使用者来确定
  3. 链表头节点必须是独立的

三、泛型跳跃表

首先设计一个可直接比较的节点对象,重载运算符是一个不错的选择:

  1. template<typename T>
  2. class Entry {
  3. private:
  4.     int key; // 排序值
  5.     T value; // 保存对象
  6.     Entry* pNext;
  7.     Entry* pDown;
  8. public:
  9.     // The Constructor
  10.     Entry(int k, T v) :value(v), key(k), pNext(nullptr), pDown(nullptr) {}
  11.     // The Copy-constructor
  12.     Entry(const Entry& e) :value(e.value), key(e.key), pNext(nullptr), pDown(nullptr) {}
  13.  
  14. public:
  15.     /* 重载运算符 */
  16.     bool operator<(const Entry& right) {
  17.         return key < right.key;
  18.     }
  19.     bool operator>(const Entry& right) {
  20.         return key > right.key;
  21.     }
  22.     bool operator<=(const Entry& right) {
  23.         return key <= right.key;
  24.     }
  25.     bool operator>=(const Entry& right) {
  26.         return key >= right.key;
  27.     }
  28.     bool operator==(const Entry& right) {
  29.         return key == right.key;
  30.     }
  31.     Entry*& next() {
  32.         return pNext;
  33.     }
  34.     Entry*& down() {
  35.         return pDown;
  36.     }
  37. };

特别说明一下最后两个方法的返回值是指针的引用,它可以直接作为左值。(Java程序员表示一脸懵逼)

然后,还需要设计一个独立于检索节点的链表头对象:

  1. struct Endpoint {
  2.     Endpoint* up;
  3.     Endpoint* down;
  4.     Entry<T>* right;
  5. };

随机判断函数没有太大变化,只是将种子seed的保存位置从函数外放到了对象中。以下是完整代码:

  1. #pragma once
  2. #ifndef SKIPLIST_ENTRY_H_
  3. #define SKIPLIST_ENTRY_H_
  4. /* 一个更具备代表性的泛型版本 */
  5. #include <ctime>
  6. #include <cstdlib>
  7. template<typename T>
  8. class Entry {
  9. private:
  10.     int key; // 排序值
  11.     T value; // 保存对象
  12.     Entry* pNext;
  13.     Entry* pDown;
  14. public:
  15.     // The Constructor
  16.     Entry(int k, T v) :value(v), key(k), pNext(nullptr), pDown(nullptr) {}
  17.     // The Copy-constructor
  18.     Entry(const Entry& e) :value(e.value), key(e.key), pNext(nullptr), pDown(nullptr) {}
  19.  
  20. public:
  21.     /* 重载运算符 */
  22.     bool operator<(const Entry& right) {
  23.         return key < right.key;
  24.     }
  25.     bool operator>(const Entry& right) {
  26.         return key > right.key;
  27.     }
  28.     bool operator<=(const Entry& right) {
  29.         return key <= right.key;
  30.     }
  31.     bool operator>=(const Entry& right) {
  32.         return key >= right.key;
  33.     }
  34.     bool operator==(const Entry& right) {
  35.         return key == right.key;
  36.     }
  37.     Entry*& next() {
  38.         return pNext;
  39.     }
  40.     Entry*& down() {
  41.         return pDown;
  42.     }
  43. };
  44. template<typename T>
  45. class SkipList_Entry {
  46. private:
  47.     struct Endpoint {
  48.         Endpoint* up;
  49.         Endpoint* down;
  50.         Entry<T>* right;
  51.     };
  52.     struct Endpoint* header;
  53.     int lvl_num; // level_number 已存在的层数
  54.     unsigned int seed;
  55.     bool random() {
  56.         srand(seed);
  57.         int ret = rand() % 2;
  58.         seed = rand();
  59.         return ret == 0;
  60.     }
  61. public:
  62.     SkipList_Entry() :lvl_num(1), seed(time(0)) {
  63.         header = new Endpoint();
  64.     }
  65.     /* 插入新元素 */
  66.     void insert(Entry<T>* entry) { // 插入是一系列自底向上的操作
  67.         struct Endpoint* cur_header = header;
  68.         // 首先使用链表header到达L1
  69.         while (cur_header->down != nullptr) {
  70.             cur_header = cur_header->down;
  71.         }
  72.         /* 这里的一个简单想法是L1必定需要插入元素,而在上面的各跳跃层是否插入则根据random确定
  73.            因此这是一个典型的do-while循环模式 */
  74.         int cur_lvl = 0; // current_level 当前层数
  75.         Entry<T>* temp_entry = nullptr; // 用来临时保存一个已经完成插入的节点指针
  76.         do {
  77.             Entry<T>* cur_cp_entry = new Entry<T>(*entry); // 拷贝新对象
  78.             // 首先需要判断当前层是否已经存在,如果不存在增新增
  79.             cur_lvl++;
  80.             if (lvl_num < cur_lvl) {
  81.                 lvl_num++;
  82.                 Endpoint *new_header = new Endpoint();
  83.                 new_header->down = header;
  84.                 header->up = new_header;
  85.                 header = new_header;
  86.             }
  87.             // 使用cur_lvl作为判断标准,!=1表示cur_header需要上移并连接“同位素”指针
  88.             if (cur_lvl != 1) {
  89.                 cur_header = cur_header->up;
  90.                 cur_cp_entry->down() = temp_entry;
  91.             }
  92.             temp_entry = cur_cp_entry;
  93.             // 再需要判断的情况是当前所在链表是否已经有元素节点存在,如果是空链表则直接对右侧指针赋值并跳出循环
  94.             if (cur_header->right == nullptr) {
  95.                 cur_header->right = cur_cp_entry;
  96.                 break;
  97.             }
  98.             else {
  99.                 Entry<T>* cursor = cur_header->right; // 创建一个游标指针
  100.                 while (true) { // 于当前链表循环向右寻找可插入点,并在找到后跳出当前循环
  101.                     if (*cur_cp_entry < *cursor) { // 元素小于当前链表所有元素,插入链表头
  102.                         cur_header->right = cur_cp_entry;
  103.                         cur_cp_entry->next() = cursor;
  104.                         break;
  105.                     }
  106.                     else if (cursor->next() == nullptr) { // 元素大于当前链表所有元素,插入链表尾
  107.                         cursor->next() = cur_cp_entry;
  108.                         break;
  109.                     }
  110.                     else if (*cur_cp_entry < *cursor->next()) { // 插入链表中间
  111.                         cur_cp_entry->next() = cursor->next();
  112.                         cursor->next() = cur_cp_entry;
  113.                         break;
  114.                     }
  115.                     cursor = cursor->next(); // 右移动游标
  116.                 }
  117.             }
  118.         } while(random());
  119.     }
  120.  
  121.     /* 查询元素 */
  122.     bool search(Entry<T>* entry) const {
  123.         if (header->right == nullptr) { // 判断链表头右侧空指针
  124.             return false;
  125.         }
  126.         Endpoint* cur_header = header;
  127.         // 在lvl_num层中首先找到可以接入的点
  128.         for (int i = 0; i < lvl_num; i++) {
  129.             if (*entry < *cur_header->right) {
  130.                 cur_header = cur_header->down;
  131.             }
  132.             else {
  133.                 Entry<T>* cursor = cur_header->right;
  134.                 while (cursor->down() != nullptr) {
  135.                     while (cursor->next() != nullptr) {
  136.                         if (*entry <= *cursor->next()) {
  137.                             break;
  138.                         }
  139.                         cursor = cursor->next();
  140.                     }
  141.                     cursor = cursor->down();
  142.                 }
  143.                 while (cursor->next() != nullptr) {
  144.                     if (*entry > *cursor->next()) {
  145.                         cursor = cursor->next();
  146.                     }
  147.                     else if (*entry == *cursor->next()) {
  148.                         return true;
  149.                     }
  150.                     else {
  151.                         return false;
  152.                     }
  153.                 }
  154.                 return false; // 节点大于L1最后一个元素节点,返回false
  155.             }
  156.         }
  157.         return false; // 找不到接入点,则直接返回false;
  158.     }
  159.     /* 删除元素 */
  160.     void remove(Entry<T>* entry) {
  161.         if (header->right == nullptr) {
  162.             return;
  163.         }
  164.         Endpoint* cur_header = header;
  165.         Entry<T>* cursor = cur_header->right;
  166.         int lvl_counter = lvl_num; // 因为在删除的过程中,跳跃表的层数会中途发生变化,因此应该在进入循环之前要获取它的值。
  167.         for (int i = 0; i < lvl_num; i++) {
  168.             if (*entry == *cur_header->right) {
  169.                 Entry<T>* delptr = cur_header->right;
  170.                 cur_header->right = cur_header->right->next();
  171.                 delete delptr;
  172.             }
  173.             else {
  174.                 Entry<T> *cursor = cur_header->right;
  175.                 while (cursor->next() != nullptr) {
  176.                     if (*entry == *cursor->next()) { // 找到节点->删除->跳出循环
  177.                         Entry<T>* delptr = cursor->next();
  178.                         cursor->next() = cursor->next()->next();
  179.                         delete delptr;
  180.                         break;
  181.                     }
  182.                     cursor = cursor->next();
  183.                 }
  184.             }
  185.             // 向下移动链表头指针的时候需要先判断当前链表中是否还存在Entry节点
  186.             if (cur_header->right == nullptr) {
  187.                 Endpoint* delheader = cur_header;
  188.                 cur_header = cur_header->down;
  189.                 header = cur_header;
  190.                 delete delheader;
  191.                 lvl_num--;
  192.             }
  193.             else {
  194.                 cur_header = cur_header->down;
  195.             }
  196.         }
  197.     }
  198. };
  199. #endif // !SKIPLIST_ENTRY_H_

go跳跃表实现

  1. package main
  2.  
  3. import (
  4. "fmt"
  5. "math"
  6. "math/rand"
  7. "strconv"
  8. "strings"
  9. "time"
  10. )
  11.  
  12. type node struct {
  13. value int
  14. up    *node
  15. down  *node
  16. left  *node
  17. right *node
  18. }
  19.  
  20. func (n node) String() string {
  21. sn := make(map[string][]string)
  22. sn["current"] = append(sn["current"], strconv.Itoa(n.value))
  23. cn := n
  24. for cn.right != nil {
  25. sn["right"] = append(sn["right"], strconv.Itoa(cn.right.value))
  26. cn = *(cn.right)
  27. }
  28. cn = n
  29. for cn.left != nil {
  30. sn["left"] = append(sn["left"], strconv.Itoa(cn.left.value))
  31. cn = *(cn.left)
  32. }
  33. cn = n
  34. for cn.up != nil {
  35. sn["up"] = append(sn["up"], strconv.Itoa(cn.up.value))
  36. cn = *(cn.up)
  37. }
  38. cn = n
  39. for cn.down != nil {
  40. sn["down"] = append(sn["down"], strconv.Itoa(cn.down.value))
  41. cn = *(cn.down)
  42. }
  43. return fmt.Sprintf("node:{%s}", sn)
  44. }
  45.  
  46. type skiplist struct {
  47. head   *node
  48. lvlNum int
  49. }
  50.  
  51. func NewDefaultNode(val int) (*node) {
  52. return &node{
  53. value: val,
  54. }
  55. }
  56.  
  57. func NewDefaultSkipList() (*skiplist) {
  58. return &skiplist{
  59. lvlNum: 1,
  60. head:   NewDefaultNode(math.MinInt64),
  61. }
  62. }
  63.  
  64. func (sp *skiplist) String() string {
  65. var cursor = sp.head
  66. for cursor.down != nil {
  67. cursor = cursor.down
  68. }
  69. skItem := []string{}
  70. for cursor.right != nil {
  71. skItem = append(skItem, strconv.Itoa(cursor.right.value))
  72. cursor = cursor.right
  73. }
  74. return fmt.Sprintf("skiplist:[%s],levelnum:%d", strings.Join(skItem, ","), sp.lvlNum)
  75. }
  76. func (sp *skiplist) RandomVal() (bool) {
  77. rand.Seed(time.Now().UnixNano())
  78. := rand.Intn(100)
  79. if flag := x % 2; flag == 0 {
  80. return true
  81. }
  82. return false
  83. }
  84.  
  85. func (sp *skiplist) insert(value int) {
  86. /* 首先查找L1层 */
  87. var cursor = sp.head
  88. var new_node *node
  89. for cursor.down != nil {
  90. cursor = cursor.down
  91. }
  92. var cur_head = cursor //当前层链表头
  93. var pre_cursor *node //记录前面游标
  94. for cursor.right != nil {
  95. if value == cursor.right.value {
  96. return
  97. } else if value < cursor.right.value && new_node == nil {
  98. new_node = NewDefaultNode(value)
  99. new_node.right = cursor.right
  100. new_node.left = cursor
  101. cursor.right = new_node
  102. }
  103. pre_cursor = cursor
  104. cursor = cursor.right // 向右移动游标
  105. cursor.left = pre_cursor
  106. }
  107. if new_node == nil {
  108. new_node = NewDefaultNode(value)
  109. cursor.right = new_node
  110. new_node.left = cursor
  111. }
  112. /* L1层插入完成 */
  113. /* 上层操作 */
  114. var cur_lvl = 1
  115. for sp.RandomVal() {
  116. cur_lvl++
  117. if sp.lvlNum < cur_lvl { // 增加一层
  118. sp.lvlNum = sp.lvlNum + 1
  119. new_head := NewDefaultNode(math.MinInt64)
  120. new_head.down = sp.head
  121. sp.head.up = new_head
  122. sp.head = new_head
  123. }
  124. cur_head = cur_head.up // 当前链表头上移一层
  125. cursor = cur_head      // 继续获取游标
  126. var skip_node *node    // 非L1层的节点
  127. pre_cursor = nil
  128. for cursor.right != nil {
  129. if value == cursor.right.value {
  130. return
  131. } else if value < cursor.right.value && skip_node == nil {
  132. skip_node = NewDefaultNode(value)
  133. skip_node.right = cursor.right
  134. cursor.right = skip_node
  135. skip_node.left = cursor
  136. }
  137. pre_cursor = cursor
  138. cursor = cursor.right
  139. cursor.left = pre_cursor
  140. }
  141. if skip_node == nil {
  142. skip_node = NewDefaultNode(value)
  143. cursor.right = skip_node
  144. skip_node.left = cursor
  145. }
  146. for new_node.up != nil {
  147. new_node = new_node.up
  148. }
  149. /* 连接上下两个节点 */
  150. skip_node.down = new_node
  151. new_node.up = skip_node
  152. }
  153. }
  154.  
  155. type moveFlag int
  156.  
  157. const (
  158. MOVE_RIGHT moveFlag = 1 << iota
  159. MOVE_LEFT
  160. NONE
  161. )
  162.  
  163. func (sp *skiplist) search(value int) (bool, *node) {
  164. cursor := sp.head
  165. mflag := NONE
  166. for cursor != nil {
  167. if cursor.value == value {
  168. return true, cursor
  169. } else if cursor.value > value && cursor.left != nil && (mflag == NONE || mflag == MOVE_LEFT) {
  170. cursor = cursor.left
  171. mflag = MOVE_LEFT
  172. } else if cursor.value < value && cursor.right != nil && (mflag == NONE || mflag == MOVE_RIGHT) {
  173. cursor = cursor.right
  174. mflag = MOVE_RIGHT
  175. } else {
  176. cursor = cursor.down
  177. mflag = NONE
  178. }
  179. }
  180. return false, nil
  181. }
  182.  
  183. func (sp *skiplist) remove(value int) {
  184. _, rnode := sp.search(value)
  185. for rnode != nil {
  186. var pleft = rnode.left
  187. var pright = rnode.right
  188. pleft.right = pright
  189. if pright != nil {
  190. pright.left = pleft
  191. }
  192. rnode = rnode.down
  193. }
  194. }
  195.  
  196. func main() {
  197. sp := NewDefaultSkipList()
  198. for i := 0; i < 10000; i++ {
  199. sp.insert(rand.Intn(10000))
  200. }
  201. fmt.Println(sp)
  202. sp.remove(14)
  203. fmt.Println(sp.search(14))
  204. fmt.Println(sp.search(758))
  205. }

本文转载自数据结构与算法(c++)——跳跃表(skip list)

跳跃表另一种实现

skiplist for golang

A skip list implementation in Go

Rabin-Karp 算法(字符串快速查找)
golang-socket链接