跳跃表

跳跃列表是一种数据结构。它允许快速查询一个有序连续元素的数据链表。跳跃列表的平均查找和插入时间复杂度都是 O(log n) ,优于普通队列的 O(n)

快速查询是通过维护一个多层次的链表,且每一层链表中的元素是前一层链表元素的子集。一开始时,算法在最稀疏的层次进行搜索,直至需要查找的元素在该层两个相邻的元素中间。这时,算法将跳转到下一个层次,重复刚才的搜索,直到找到需要查找的元素为止。跳过的元素的方法可以是 随机性选择确定性选择,其中前者更为常见。

跳跃表 - 图1

在查找目标元素时,从顶层列表、头元素起步。算法沿着每层链表搜索,直至找到一个大于或等于目标的元素,或者到达当前层列表末尾。如果该元素等于目标元素,则表明该元素已被找到;如果该元素大于目标元素或已到达链表末尾,则退回到当前层的上一个元素,然后转入下一层进行搜索。

跳跃列表不像平衡树等数据结构那样提供对最坏情况的性能保证:由于用来建造跳跃列表采用随机选取元素进入更高层的方法,在小概率情况下会生成一个不平衡的跳跃列表(最坏情况例如最底层仅有一个元素进入了更高层,此时跳跃列表的查找与普通列表一致)。但是在实际中它通常工作良好,随机化平衡方案也比平衡二叉查找树等数据结构中使用的确定性平衡方案容易实现。跳跃列表在并行计算中也很有用:插入可以在跳跃列表不同的部分并行地进行,而不用对数据结构进行全局的重新平衡。

跳跃表插入一个元素:

跳跃表 - 图2

实现

因为跳跃列表中的元素可以在多个列表中,所以每个元素可以有多于一个指针。跳跃列表的插入和删除的实现与普通的链表操作类似,但高层元素必须在进行多个链表中进行插入或删除。

  1. package io.github.hadyang.leetcode.algo;
  2. import lombok.Getter;
  3. import lombok.Setter;
  4. import java.util.Arrays;
  5. import java.util.Random;
  6. /**
  7. * @author haoyang.shi
  8. */
  9. public class SkipList<K extends Comparable<K>, V> {
  10. @Getter
  11. @Setter
  12. static final class Node<K extends Comparable<K>, V> {
  13. private K key;
  14. private V value;
  15. private Node<K, V> up, down, pre, next;
  16. Node(K key, V value) {
  17. this.key = key;
  18. this.value = value;
  19. }
  20. @Override
  21. public String toString() {
  22. return "Node{" +
  23. "key=" + key +
  24. ", value=" + value +
  25. ", hashcode=" + hashCode() +
  26. ", up=" + (up == null ? "null" : up.hashCode()) +
  27. ", down=" + (down == null ? "null" : down.hashCode()) +
  28. ", pre=" + (pre == null ? "null" : pre.hashCode()) +
  29. ", next=" + (next == null ? "null" : next.hashCode()) +
  30. '}';
  31. }
  32. }
  33. private Node<K, V> head;//k,v都是NULL
  34. private Integer levels = 0;
  35. private Integer length = 0;
  36. private Random random = new Random(System.currentTimeMillis());
  37. public SkipList() {
  38. createNewLevel();
  39. }
  40. public void put(K key, V value) {
  41. if (key == null || value == null) {
  42. return;
  43. }
  44. Node<K, V> newNode = new Node<>(key, value);
  45. insertNode(newNode);
  46. }
  47. private void insertNode(Node<K, V> newNode) {
  48. Node<K, V> curNode = findNode(newNode.getKey());
  49. if (curNode.getKey() == null) {
  50. insertNext(curNode, newNode);
  51. } else if (curNode.getKey().compareTo(newNode.getKey()) == 0) {
  52. //update
  53. curNode.setValue(newNode.getValue());
  54. return;
  55. } else {
  56. insertNext(curNode, newNode);
  57. }
  58. int currentLevel = 1;
  59. Node<K, V> oldTop = newNode;
  60. while (random.nextInt(100) < 50) {
  61. Node<K, V> newTop = new Node<>(newNode.getKey(), null);
  62. if (currentLevel >= levels) {
  63. createNewLevel();
  64. }
  65. while (curNode.getPre() != null && curNode.getUp() == null) {
  66. curNode = curNode.getPre();
  67. }
  68. if (curNode.getUp() == null) {
  69. continue;
  70. }
  71. curNode = curNode.getUp();
  72. Node<K, V> curNodeNext = curNode.getNext();
  73. curNode.setNext(newTop);
  74. newTop.setPre(curNode);
  75. newTop.setDown(oldTop);
  76. oldTop.setUp(newTop);
  77. newTop.setNext(curNodeNext);
  78. oldTop = newTop;
  79. currentLevel++;
  80. }
  81. }
  82. private void createNewLevel() {
  83. Node<K, V> newHead = new Node<>(null, null);
  84. if (this.head == null) {
  85. this.head = newHead;
  86. this.levels++;
  87. return;
  88. }
  89. this.head.setUp(newHead);
  90. newHead.setDown(this.head);
  91. this.head = newHead;
  92. this.levels++;
  93. }
  94. private void insertNext(Node<K, V> curNode, Node<K, V> newNode) {
  95. Node<K, V> curNodeNext = curNode.getNext();
  96. newNode.setNext(curNodeNext);
  97. if (curNodeNext != null) {
  98. curNodeNext.setPre(newNode);
  99. }
  100. curNode.setNext(newNode);
  101. newNode.setPre(curNode);
  102. this.length++;
  103. }
  104. public V get(K key) {
  105. Node<K, V> node = findNode(key);
  106. if (key.equals(node.getKey())) {
  107. return node.getValue();
  108. }
  109. return null;
  110. }
  111. private Node<K, V> findNode(K key) {
  112. Node<K, V> curNode = this.head;
  113. for (; ; ) {
  114. while (curNode.getNext() != null && curNode.getNext().getKey().compareTo(key) <= 0) {
  115. curNode = curNode.getNext();
  116. }
  117. if (curNode.getDown() != null) {
  118. curNode = curNode.getDown();
  119. } else {
  120. break;
  121. }
  122. }
  123. return curNode;
  124. }
  125. public void print() {
  126. Node<K, V> curI = this.head;
  127. String[][] strings = new String[levels][length + 1];
  128. for (String[] string : strings) {
  129. Arrays.fill(string, "0");
  130. }
  131. while (curI.getDown() != null) {
  132. curI = curI.getDown();
  133. }
  134. System.out.println("levels:" + levels + "_" + "length:" + length);
  135. int i = 0;
  136. while (curI != null) {
  137. Node<K, V> curJ = curI;
  138. int j = levels - 1;
  139. while (curJ != null) {
  140. strings[j][i] = String.valueOf(curJ.getKey());
  141. if (curJ.getUp() == null) {
  142. break;
  143. }
  144. curJ = curJ.getUp();
  145. j--;
  146. }
  147. if (curI.getNext() == null) {
  148. break;
  149. }
  150. curI = curI.getNext();
  151. i++;
  152. }
  153. for (String[] string : strings) {
  154. System.out.println(Arrays.toString(string));
  155. }
  156. }
  157. public static void main(String[] args) {
  158. SkipList<Integer, String> skipList = new SkipList<>();
  159. skipList.put(2, "B");
  160. skipList.put(1, "A");
  161. skipList.put(3, "C");
  162. skipList.print();
  163. System.out.println(skipList.get(2));
  164. }
  165. }