-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSkipList.java
139 lines (132 loc) · 4.24 KB
/
SkipList.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
package skiplist;
import java.util.Random;
import java.util.Stack;
/**
* @author zhang fan
* @date 2021/5/23 11:37
*/
public class SkipList<T> {
SkipNode headNode; //头节点,入口
int highLevel; //层数:1<=highLevel<=32
Random random;//添加元素时随机添加索引
final int MAX_LEVEL = 32;
SkipList() {
headNode = new SkipNode(Integer.MIN_VALUE,null);
highLevel = 0;
random = new Random();
}
public SkipNode search(int key){
SkipNode team = headNode;
while (team!=null){
if(team.key==key){
return team;
}else if(team.right==null){
team =team.down;
}else if(team.right.key>key){ //右侧大于key,下降
team =team.down;
}else{ //右侧小于key,向右
team =team.right;
}
}
return null;
}
public void delete(int key){
SkipNode team = headNode;
while (team!=null){
if(team.right==null){
team =team.down;
}else if(team.right.key==key){//右侧即为待删除节点
team.right = team.right.right;
team =team.down;
}else if(team.right.key>key){
team =team.down;
}else {
team = team.right;
}
}
}
public void add(SkipNode node){
int key = node.key;
SkipNode findNode = search(key);//查找是否存在相应的节点,存在则更新节点的值
if(findNode!=null){
findNode.value = node.value;
return;
}
Stack<SkipNode> stack = new Stack<>();//用栈记录向下和向右的节点
SkipNode team = headNode;
while (team!=null){
if(team.right==null){
stack.add(team);
team = team.down;
}else if(team.right.key>key){
stack.add(team);
team = team.down;
}else {
team = team.right;
}
}
int level = 1;
SkipNode downNode = null;//保持前驱节点(即down的指向,初始为null)
while (!stack.isEmpty()){
team = stack.pop();
SkipNode nodeTeam = new SkipNode(node.key,node.value);
nodeTeam.down = downNode;//处理竖向
downNode = nodeTeam;//标记新的节点下次使用
if(team.right==null){
team.right = nodeTeam;
}else{//水平方向处理
nodeTeam.right = team.right;
team.right = nodeTeam;
}
if(level>MAX_LEVEL) break;
double num = random.nextDouble();
if(num>0.5) break;;
level++;
if(level>highLevel){//比最大高度还高,但<=32
highLevel =level;
SkipNode highHeadNode = new SkipNode(Integer.MIN_VALUE,null);
highHeadNode.down = headNode;
headNode = highHeadNode;
stack.add(headNode);
}
}
}
public void printList(){
SkipNode teamNode=headNode;
int index=1;
SkipNode last=teamNode;
while (last.down!=null){
last=last.down;
}
while (teamNode!=null) {
SkipNode enumNode=teamNode.right;
SkipNode enumLast=last.right;
System.out.printf("%-8s","head->");
while (enumLast!=null&&enumNode!=null) {
if(enumLast.key==enumNode.key)
{
System.out.printf("%-5s",enumLast.key+"->");
enumLast=enumLast.right;
enumNode=enumNode.right;
}
else{
enumLast=enumLast.right;
System.out.printf("%-5s","");
}
}
teamNode=teamNode.down;
index++;
System.out.println();
}
}
public static void main(String[] args) {
SkipList<Integer> skipList = new SkipList<>();
for (int i = 1; i < 20; i++) {
skipList.add(new SkipNode(i,"zhangfan"));
}
skipList.printList();
skipList.delete(4);
skipList.delete(8);
skipList.printList();
}
}