• 数据结构之:跳表


    跳表(Skip List)是一种概率性数据结构,它通过在普通有序链表的基础上增加多级索引层来实现快速的查找、插入和删除操作。跳表的效率可以与平衡树相媲美,其操作的时间复杂度也是O(log n),但跳表的结构更简单,更易于实现。

    跳表的核心特征

    • 多层结构:跳表包含多个层级。最底层(第0层)包含所有的元素。每一层都是下一层的“快速通道”,每个元素出现在上层的概率通常是1/2。
    • 头节点:跳表有一个头节点(head),它在所有层级中都存在。头节点的值通常不存储实际的数据,它的目的是为了搜索、插入和删除操作提供一个统一的起点。
    • 随机层级:每个新插入的节点的层数是通过随机过程决定的,以确保跳表的平衡性。这意味着高层索引不会过于密集或稀疏。

    数据结构组件

    1. 节点(SkipListNode):每个节点包含的信息有:
      • 值(value):存储的数据值。
      • 前进指针(forward):一个指针数组,指向同一层级的下一个节点以及上层对应的节点。
    2. 头节点(Head):是一个特殊的节点,它的forward指针数组的长度等于跳表的最大层数。它在所有层级上都指向该层的第一个实际节点(如果存在)。
    3. 层数(Level):跳表当前的最大层数。这个值是动态的,随着新节点的插入可以增加。

    操作原理

    • 搜索(Search):从头节点开始,在最高层级搜索,如果当前节点的下一个节点的值小于目标值,则向前移动;如果大于目标值,则下降到下一层级继续搜索,直至找到目标值或搜索失败。
    • 插入(Insert):首先通过随机过程确定新节点的层数。然后从最高层开始寻找插入位置,逐层向下直到达到新节点应存在的最低层级。在每一层,将新节点插入到适当的位置,并更新相关节点的指针。
    • 删除(Delete):与搜索类似,首先定位要删除的节点。然后从其所在的最高层开始,逐层向下删除节点,并更新指针。

    优点与应用

    • 简单性:跳表的数据结构和算法相对简单,特别是与平衡树和B树等结构相比。
    • 动态性:跳表可以很容易地支持动态数据集合的操作,如实时插入和删除。
    • 效率:对于大多数操作,跳表可以提供对数时间复杂度的性能,适用于需要快速搜索操作的场景,如数据库索引和内存数据库。

    跳表通过简单的随机化过程来避免复杂的重平衡操作,使得它成为一种既高效又易于实现的数据结构选项。

    简单的跳表实现示例

    1. import java.util.Random;
    2. class SkipListNode {
    3. int value;
    4. SkipListNode[] forward; // 指向不同层的指针数组
    5. public SkipListNode(int value, int level) {
    6. this.value = value;
    7. this.forward = new SkipListNode[level + 1];
    8. }
    9. }
    10. public class SkipList {
    11. private static final float P = 0.5f;
    12. private static final int MAX_LEVEL = 16;
    13. private SkipListNode head;
    14. private int level;
    15. private Random random;
    16. public SkipList() {
    17. level = 0;
    18. head = new SkipListNode(0, MAX_LEVEL);
    19. random = new Random();
    20. }
    21. // 随机生成节点的层数
    22. private int randomLevel() {
    23. int lvl = 1;
    24. while (random.nextFloat() < P && lvl < MAX_LEVEL) {
    25. lvl++;
    26. }
    27. return lvl;
    28. }
    29. // 插入节点
    30. public void insert(int value) {
    31. int lvl = randomLevel();
    32. SkipListNode newNode = new SkipListNode(value, lvl);
    33. SkipListNode current = head;
    34. SkipListNode[] update = new SkipListNode[MAX_LEVEL + 1];
    35. for (int i = level; i >= 0; i--) {
    36. while (current.forward[i] != null && current.forward[i].value < value) {
    37. current = current.forward[i];
    38. }
    39. update[i] = current;
    40. }
    41. for (int i = 0; i <= lvl; i++) {
    42. newNode.forward[i] = update[i].forward[i];
    43. update[i].forward[i] = newNode;
    44. }
    45. if (lvl > level) {
    46. level = lvl;
    47. }
    48. }
    49. // 查找节点
    50. public boolean search(int value) {
    51. SkipListNode current = head;
    52. for (int i = level; i >= 0; i--) {
    53. while (current.forward[i] != null && current.forward[i].value < value) {
    54. current = current.forward[i];
    55. }
    56. }
    57. current = current.forward[0];
    58. return current != null && current.value == value;
    59. }
    60. // 删除节点
    61. public void delete(int value) {
    62. SkipListNode[] update = new SkipListNode[MAX_LEVEL + 1];
    63. SkipListNode current = head;
    64. for (int i = level; i >= 0; i--) {
    65. while (current.forward[i] != null && current.forward[i].value < value) {
    66. current = current.forward[i];
    67. }
    68. update[i] = current;
    69. }
    70. current = current.forward[0];
    71. if (current.value == value) {
    72. for (int i = 0; i <= level; i++) {
    73. if (update[i].forward[i] != current) break;
    74. update[i].forward[i] = current.forward[i];
    75. }
    76. while (level > 0 && head.forward[level] == null) {
    77. level--;
    78. }
    79. }
    80. }
    81. // 打印跳表的内容
    82. public void display() {
    83. System.out.println("SkipList: ");
    84. for (int i = 0; i <= level; i++) {
    85. SkipListNode node = head.forward[i];
    86. System.out.print("Level " + i + ": ");
    87. while (node != null) {
    88. System.out.print(node.value + " ");
    89. node = node.forward[i];
    90. }
    91. System.out.println();
    92. }
    93. }
    94. }
    95. // 使用示例
    96. public class Main {
    97. public static void main(String[] args) {
    98. SkipList list = new SkipList();
    99. list.insert(3);
    100. list.insert(6);
    101. list.insert(7);
    102. list.insert(9);
    103. list.insert(12);
    104. list.insert(19);
    105. list.insert(17);
    106. list.display();
    107. System.out.println("Searching 6: " + list.search(6));
    108. System.out.println("Searching 15: " + list.search(15));
    109. list.delete(6);
    110. System.out.println("After deleting 6: ");
    111. list.display();
    112. }
    113. }

    这段代码首先定义了SkipListNode类,它是跳表节点的结构,包括节点值和一个数组forward,数组中每个元素是对应层级的下一个节点的引用。SkipList类实现了跳表,包括初始化、插入、查找、删除和打印跳表的方法。

    • insert方法用于插入新的节点。
    • search方法用于查找一个值,如果找到,则返回true
    • delete方法用于删除一个值。
    • display方法用于打印跳表的所有层级和节点。

    通过一个具体的例子来说明跳表的插入过程

    假设我们有一个跳表,它当前的状态如下,其中每一行代表一个层级(层级0是最底层,包含所有元素):

    1. 层级31 --------------------------------> 9
    2. 层级21 ------------> 5 ------------> 9
    3. 层级11 ----> 3 ----> 5 ----> 7 ----> 9
    4. 层级01 -> 2 -> 3 -> 4 -> 5 -> 6 -> 7 -> 9

    现在,我们想要插入一个新的节点,值为8,并假设通过随机过程,决定新节点8将出现在层级0、1和2上(不出现在层级3上)。下面是插入过程的步骤:

    步骤1:寻找每一层的插入位置

    从跳表的最高层(在这个例子中是层级3)开始寻找,直到找到比插入值小的最大节点。因为8不会被插入到层级3,我们直接从层级2开始:

    • 层级2:从1开始,遍历到5,因为9大于8,所以5是层级2的插入位置。
    • 层级1:同样从1开始,遍历到5,然后到7,因为9大于8,所以7是层级1的插入位置。
    • 层级0:从1开始,按顺序遍历,直到7,因为9大于8,所以7是层级0的插入位置。
    步骤2:插入节点并更新指针
    • 层级2:在59之间插入8,更新5的下一个指针为88的下一个指针为9
    • 层级1:在79之间插入8,更新7的下一个指针为88的下一个指针为9
    • 层级0:在79之间插入8,更新7的下一个指针为88的下一个指针为9

    插入8后,跳表变为:

    1. 层级31 --------------------------------> 9
    2. 层级21 ------------> 5 -------> 8 ----> 9
    3. 层级11 ----> 3 ----> 5 ----> 7 -> 8 -> 9
    4. 层级01 -> 2 -> 3 -> 4 -> 5 -> 6 -> 7 -> 8 -> 9
    步骤3:调整跳表的总层数(如果需要)

    在这个例子中,新插入的节点8并没有增加跳表的总层数,因此不需要调整。

    通过这个例子,你可以看到插入过程如何在每一层找到正确的插入位置,并更新指针来维护跳表的结构。这个过程确保了跳表的搜索效率,使得搜索、插入和删除操作的时间复杂度都为O(log n)。

  • 相关阅读:
    阿里P8整理出了这份444页深入浅出SpringBoot2.X笔记
    安装element-plus
    用Python自动生成数据日报!
    国家开放大学 试题练习
    Java项目:SSM企业OA管理系统
    1052 Linked List Sorting
    JavaScript 16 JavaScript 对象
    使用 CSS 实现毛玻璃效果
    痞子衡嵌入式:浅析IAR下调试信息输出机制之半主机(Semihosting)
    Vue入门(二)
  • 原文地址:https://blog.csdn.net/www_tlj/article/details/136280803