有关跳跃表的概念,自行搜索,谢谢
跳跃表节点对象
package com.xll.other;
/**
* @Author: xialonglei469
* @Date: 2019-10-10 13:43
* @Version 1.0
*
* 跳跃表节点
*/
public class SkipListNode {
Integer data;
/** 指向下一层 */
SkipListNode down;
/** 指向后一个节点 */
SkipListNode forward;
/** 指向前一个节点 */
SkipListNode backward;
/** 是否是每层的头节点 true.是 false.否 */
boolean headNode;
public SkipListNode(Integer data) {
this.data = data;
}
public SkipListNode(boolean headNode) {
this.headNode = headNode;
}
public SkipListNode() {}
}
跳跃表
package com.xll.other;
/**
* @Author: xialonglei469
* @Date: 2019-10-10 13:46
* @Version 1.0
*/
public class SkipList {
/** 跳跃表最大层数 */
int maxLevel;
/** 跳跃表实际层数 */
int level = 4;
/** 跳跃表元素个数 */
int size;
/** 顶层头节点 */
SkipListNode top;
public SkipList(int maxLevel) {
if (maxLevel < 4) {
this.maxLevel = 4;
}
else if (maxLevel > 32) {
this.maxLevel = 32;
} else {
this.maxLevel = maxLevel;
}
init(4);
}
public void insert(int v) {
int k = randLevel();
int tempLevel = level;
SkipListNode downHead = top;
// 如果随机的k大于level,则生成level到k的头节点
if (k > level) {
while ((++tempLevel) <= k) {
SkipListNode node = new SkipListNode(true);
node.down = downHead;
downHead = node;
}
top = downHead;
level = k;
}
SkipListNode topK = top;
tempLevel = level;
// 找到插入的起始层
while ((tempLevel--) != k) {
topK = topK.down;
}
SkipListNode upLevelNode = null;
// 从插入的起始层,自顶向下插入
while (k-- > 0) {
SkipListNode node = new SkipListNode(v);
SkipListNode curLevelNode = topK;
while (curLevelNode.forward != null && curLevelNode.forward.data <= v) {
curLevelNode = curLevelNode.forward;
}
if (upLevelNode != null) {
upLevelNode.down = node;
}
SkipListNode curLevelNodeForward = curLevelNode.forward;
curLevelNode.forward = node;
node.forward = curLevelNodeForward;
node.backward = curLevelNode;
upLevelNode = node;
topK = topK.down;
}
size++;
}
public Integer delete(int v) {
SkipListNode cur = top;
while (cur != null) {
if (cur.headNode) {
cur = (cur.forward == null) ? cur.down : cur.forward;
}
else if (cur.data == v) {
while (cur != null) {
cur.backward.forward = cur.forward;
if (cur.forward != null) {
cur.forward.backward = cur.backward;
}
cur = cur.down;
}
return v;
}
else if (cur.data < v) {
cur = (cur.forward == null) ? cur.down : cur.forward;
}
else {
cur = cur.backward.down;
}
}
return null;
}
public Integer find(int v) {
SkipListNode head = top;
SkipListNode pre = head;
SkipListNode cur = head.forward;
while (cur != null) {
if (cur.headNode) {
pre = cur;
cur = cur.forward == null ? cur.down : cur.forward;
}
else if (cur.data == v) {
return v;
}
else if (cur.data < v) {
pre = cur;
cur = cur.forward == null ? cur.down : cur.forward;
} else {
cur = pre.down;
}
}
return null;
}
public void print() {
System.out.println("level:" + level + " size:" + size);
SkipListNode head = top;
int tempLevel = level;
while ((tempLevel--) > 0) {
SkipListNode cur = head;
while (cur.forward != null) {
System.out.print(cur.forward.data + " ");
cur = cur.forward;
}
head = head.down;
System.out.println();
}
}
private int randLevel() {
int level = 1;
while (Math.random() > 0.5) {
level++;
}
return level <= maxLevel ? level : maxLevel;
}
private void init(int level) {
int tempLevel = level;
SkipListNode upHead = null;
while ((tempLevel--) > 0) {
SkipListNode node = new SkipListNode(true);
if (upHead == null) {
top = node;
} else {
upHead.down = node;
}
upHead = node;
}
}
public static void main(String[] args) {
SkipList skipList = new SkipList(32);
for (int i = 0 ; i < 10 ; i++) {
skipList.insert(i + 1);
}
skipList.print();
skipList.delete(8);
skipList.print();
skipList.delete(10);
skipList.print();
}
}