原文在github搜数据结构 星星最多的那个哟~
import java.util.*;
import java.util.concurrent.ConcurrentSkipListMap;
public class skipList {
private static final float levelUp_p = 0.5f;
private static final int Max_level = 16;
int nowLevel = 1;
public int levelUp(){
int level = 1;
while (Math.random() < levelUp_p && level < Max_level){
level += 1;
}
return level;
}
private Node head = new Node();
public Node find(int value){
Node p = head;
for(int i = nowLevel - 1; i >= 0 ;i--){
while (p.forwards[i] != null && p.forwards[i].data < value){
p = p.forwards[i];
}
}
if (p.forwards[0] != null && p.forwards[0].data == value) {
return p.forwards[0];
} else {
return null;
}
}
public void insert(int value){
int level = levelUp();
Node newNode = new Node(value, level);
Node[] update = new Node[level];
Arrays.fill(update, head);
Node p = head;
for(int i = level - 1; i >= 0; i--){
while (p.forwards[i] != null && p.forwards[i].data < value){
p = p.forwards[i];
}
update[i] = p;
}
for(int i = 0; i < update.length; i++){
newNode.forwards[i] = update[i].forwards[i];
update[i].forwards[i] = newNode;
}
if(level > nowLevel) nowLevel = level;
}
public void delete(int value){
Node[] update = new Node[nowLevel];
Node p = head;
for(int i = nowLevel - 1; i >= 0; i--){
while (p.forwards[i] != null && p.forwards[i].data < value){
p = p.forwards[i];
}
update[i] = p;
}
if(p.forwards[0] != null && p.forwards[0].data == value){
for(int i = 0; i < update.length; i++){
if(update[i].forwards[i] != null && update[i].forwards[i].data == value) {
update[i].forwards[i] = update[i].forwards[i].forwards[i];
}
}
}
while (nowLevel > 1 && head.forwards[nowLevel] == null) nowLevel--;
}
public void printAll_beautiful() {
Node p = head;
Node[] c = p.forwards;
Node[] d = c;
//int[] data = new int[all_count];
int maxLevel = c.length;
Node[] s = c;
HashMap<Integer,Integer> map = new HashMap<>();
for(int i = 0; s[0] != null; i++){
map.put(i, s[0].data);
s = s[0].forwards;
}
//System.out.println(map);
//System.out.println(Arrays.toString(data));
for (int i = 0; i < maxLevel - 1; i++) {
System.out.printf("%2d层:",i);
int j = 0;
do {
int value;
if(d[i] != null) {
value = d[i].data;
while (value != map.get(j++)) {
System.out.print("-----");
}
System.out.printf("%2d---", value);
}
} while (d[i] != null && (d = d[i].forwards)[i] != null);
System.out.println();
d = c;
}
}
class Node{
private int data = -1;
private int level = 0;
//相同层级的节点
private Node[] forwards = new Node[Max_level];
public Node(int data, int level) {
this.data = data;
this.level = level;
}
public Node(){
}
@Override
public String toString() {
StringBuilder builder = new StringBuilder();
builder.append("{ data: ");
builder.append(data);
builder.append("; levels: ");
builder.append(level);
builder.append(" }");
return builder.toString();
}
}
public static void main(String[] args) {
skipList skip = new skipList();
//List<Integer> list = new ArrayList<>(100000);
//int[] count = new int[16];
int all_count = 25;
// for (int i = 0; i < all_count; i++){
// int val = skip.levelUp();
// //list.add(val);
// count[--val]++;
// }
// for(int i = 0; i < count.length; i++){
// System.out.println((i+1) + " " + (float)count[i]/all_count);
// }
skipList list = new skipList();
Set<Integer> set = new HashSet<>();
for(int i = 0; i < all_count; i++){
int value = (int)(Math.random() * 100);
if(!set.contains(value)) {
list.insert(value);
set.add(value);
}
else i--;
}
System.out.println();
list.printAll_beautiful();
//list.delete(2);
//System.out.println();
//list.printAll_beautiful();
Node node = list.find(4);
// for(int i = 0; i < Max_level; i++){
// Node p = node;
// while (p != null) {
// System.out.print(p.forwards[i].data+" ");
// p = p.forwards[i];
// }
// System.out.println();
// }
}
}