package ds;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;
public class SkipList<T> {
private static class SkipNode<T>{
int key;
T value;
SkipNode<T> next;
SkipNode<T> down;
public SkipNode() {
this(Integer.MIN_VALUE,null);
}
public SkipNode(int key, T value) {
this.key = key;
this.value = value;
}
}
private static final int MAX_LEVEL = 1 << 6;
private int level = 0;
private SkipNode<T> top = null;
private Random random = new Random();
public SkipList() {
this(4);
}
public SkipList(int level) {
this.level = level;
SkipNode<T> tail = null;
while(level-- != 0){
SkipNode<T> tempTop = new SkipNode<>();
SkipNode<T> tempTail = new SkipNode<>(Integer.MAX_VALUE,null);
tempTop.next = tempTail;
tempTop.down = top;
tempTail.down = tail;
top = tempTop;
tail = tempTop;
}
}
private int getRqandomLevel(){
int lev = 1;
while(random.nextInt() % 2 == 0){
++lev;
}
return lev > MAX_LEVEL ? MAX_LEVEL : lev;
}
public T get(int key){
SkipNode<T> temp = top;
while(temp != null){
while(key < temp.next.key){
temp = temp.next;
}
if(key == temp.next.key){
return temp.next.value;
}
temp = temp.down;
}
return null;
}
@Override
public String toString(){
StringBuilder sb = new StringBuilder();
SkipNode<T> temp = top;
while(temp.down != null){
temp = temp.down;
}
while(temp.next.key != Integer.MIN_VALUE){
sb.append(" (" + temp.next.key + "->" + temp.next.value + ") ");
temp = temp.next;
}
return sb.toString();
}
public void printLevel(){
SkipNode<T> temp = top;
int lev = level;
while(temp != null){
SkipNode<T> tempStart = temp;
StringBuilder sb = new StringBuilder();
while(tempStart.next.key != Integer.MAX_VALUE){
sb.append(" (" + tempStart.next.key + "->" + tempStart.next.value + ") ");
tempStart = tempStart.next;
}
System.out.print("第" + lev-- + "层:");
System.out.println(sb.toString());
temp = temp.down;
}
}
/**
* 返回插入key的前去节点链表
*/
private List<SkipNode<T>> getPreNodeList(int key){
boolean isExisted = false;
SkipNode<T> temp = top;
List<SkipNode<T>> ret = new ArrayList<>();
while(temp != null){
while(key < temp.next.key){
temp = temp.next;
}
if(isExisted == false && temp.next.key == key){
isExisted = true;
ret.clear();
}
ret.add(temp);
temp = temp.down;
}
if(isExisted == false){
ret.add(null);
}
return ret;
}
public boolean delete(int key){
List<SkipNode<T>> preNodeList = getPreNodeList(key);
if(preNodeList.get(preNodeList.size() - 1) == null){
return false;
}
for(SkipNode<T> node : preNodeList){
SkipNode<T> del = node.next;
node.next = del.next;
del.down = null;
}
return true;
}
public void put(int key,T value){
List<SkipNode<T>> preNodeList = getPreNodeList(key);
if(preNodeList.get(preNodeList.size() - 1) != null){
preNodeList.stream().forEach(node ->node.next.value = value);
return ;
}
int lev = getRqandomLevel();
int len = preNodeList.size() - 1;
SkipNode<T> down = null;
while(lev-- != 0 && len-- != 0){
SkipNode<T> node = new SkipNode<>(key, value);
SkipNode<T> node1 = preNodeList.get(len);
node.next = node1.next;
node1.next = node;
node.down = down;
down = node;
}
if(lev == -1){
return;
}
++lev;
SkipNode<T> tempTop = null;
SkipNode<T> tempTail = null;
SkipNode<T> tail = top;
while(tail.next != null){
tail = tail.next;
}
while(lev-- != 0){
tempTop = new SkipNode<>(Integer.MIN_VALUE, null);
SkipNode<T> temp = new SkipNode<>(key, value);
tempTail = new SkipNode<>(Integer.MAX_VALUE,null);
tempTop.next = temp;
temp.next = tempTail;
tempTop.down = top;
temp.down = down;
tempTail.down = tail;
top = tempTop;
down = temp;
tail = tempTail;
}
}
}
java SkipList
最新推荐文章于 2023-12-06 14:33:41 发布