|
| 1 | +package skiplist; |
| 2 | + |
| 3 | +import java.util.Random; |
| 4 | + |
| 5 | +/** |
| 6 | + * 跳表的一种实现方法。 |
| 7 | + * 跳表中存储的是正整数,并且存储的是不重复的。 |
| 8 | + * |
| 9 | + * Author:ZHENG |
| 10 | + */ |
| 11 | +public class SkipList { |
| 12 | + |
| 13 | + private static final int MAX_LEVEL = 16; |
| 14 | + |
| 15 | + private int levelCount = 1; |
| 16 | + |
| 17 | + private Node head = new Node(); // 带头链表 |
| 18 | + |
| 19 | + private Random r = new Random(); |
| 20 | + |
| 21 | + public Node find(int value) { |
| 22 | + Node p = head; |
| 23 | + for (int i = levelCount - 1; i >= 0; --i) { |
| 24 | + while (p.forwords[i] != null && p.forwords[i].data < value) { |
| 25 | + p = p.forwords[i]; |
| 26 | + } |
| 27 | + } |
| 28 | + |
| 29 | + if (p.forwords[0] != null && p.forwords[0].data == value) { |
| 30 | + return p.forwords[0]; |
| 31 | + } else { |
| 32 | + return null; |
| 33 | + } |
| 34 | + } |
| 35 | + |
| 36 | + public void insert(int value) { |
| 37 | + int level = randomLevel(); |
| 38 | + Node newNode = new Node(); |
| 39 | + newNode.data = value; |
| 40 | + newNode.maxLevel = level; |
| 41 | + Node update[] = new Node[level]; |
| 42 | + for (int i = 0; i < level; ++i) { |
| 43 | + update[i] = head; |
| 44 | + } |
| 45 | + |
| 46 | + Node p = head; |
| 47 | + for (int i = level - 1; i >= 0; --i) { |
| 48 | + while (p.forwords[i] != null && p.forwords[i].data < value) { |
| 49 | + p = p.forwords[i]; |
| 50 | + } |
| 51 | + update[i] = p; |
| 52 | + } |
| 53 | + |
| 54 | + for (int i = 0; i < level; ++i) { |
| 55 | + newNode.forwords[i] = update[i].forwords[i]; |
| 56 | + update[i].forwords[i] = newNode; |
| 57 | + } |
| 58 | + |
| 59 | + if (levelCount < level) levelCount = level; |
| 60 | + } |
| 61 | + |
| 62 | + public void delete(int value) { |
| 63 | + Node[] update = new Node[levelCount]; |
| 64 | + Node p = head; |
| 65 | + for (int i = levelCount - 1; i >= 0; --i) { |
| 66 | + while (p.forwords[i] != null && p.forwords[i].data < value) { |
| 67 | + p = p.forwords[i]; |
| 68 | + } |
| 69 | + update[i] = p; |
| 70 | + } |
| 71 | + |
| 72 | + if (p.forwords[0] != null && p.forwords[0].data == value) { |
| 73 | + for (int i = levelCount - 1; i >= 0; --i) { |
| 74 | + if (update[i].forwords[i] != null && update[i].forwords[i].data == value) { |
| 75 | + update[i].forwords[i] = update[i].forwords[i].forwords[i]; |
| 76 | + } |
| 77 | + } |
| 78 | + } |
| 79 | + } |
| 80 | + |
| 81 | + private int randomLevel() { |
| 82 | + int level = 1; |
| 83 | + for (int i = 1; i < MAX_LEVEL; ++i) { |
| 84 | + if (r.nextInt() % 2 == 1) { |
| 85 | + level++; |
| 86 | + } |
| 87 | + } |
| 88 | + |
| 89 | + return level; |
| 90 | + } |
| 91 | + |
| 92 | + public void printAll() { |
| 93 | + Node p = head; |
| 94 | + while (p.forwords[0] != null) { |
| 95 | + System.out.print(p.forwords[0] + " "); |
| 96 | + p = p.forwords[0]; |
| 97 | + } |
| 98 | + System.out.println(); |
| 99 | + } |
| 100 | + |
| 101 | + public class Node { |
| 102 | + private int data = -1; |
| 103 | + private Node forwords[] = new Node[MAX_LEVEL]; |
| 104 | + private int maxLevel = 0; |
| 105 | + |
| 106 | + @Override |
| 107 | + public String toString() { |
| 108 | + StringBuilder builder = new StringBuilder(); |
| 109 | + builder.append("{ data: "); |
| 110 | + builder.append(data); |
| 111 | + builder.append("; levels: "); |
| 112 | + builder.append(maxLevel); |
| 113 | + builder.append(" }"); |
| 114 | + |
| 115 | + return builder.toString(); |
| 116 | + } |
| 117 | + } |
| 118 | + |
| 119 | +} |
0 commit comments