-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSkipList.java
More file actions
303 lines (243 loc) · 7.2 KB
/
SkipList.java
File metadata and controls
303 lines (243 loc) · 7.2 KB
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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
import java.lang.reflect.Array;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.Random;
/**
* This class implements SkipList data structure and contains an inner SkipNode
* class which the SkipList will make an array of to store data.
*
* @author CS Staff
*
* @version 2021-08-23
* @param <K>
* Key
* @param <V>
* Value
*/
public class SkipList<K extends Comparable<? super K>, V>
implements Iterable<KVPair<K, V>> {
private SkipNode head; // First element of the top level
private int size; // number of entries in the Skip List
/**
* Initializes the fields head, size and level
*/
public SkipList() {
head = new SkipNode(null, 0);
size = 0;
}
/**
* Returns a random level number which is used as the depth of the SkipNode
*
* @return a random level number
*/
int randomLevel() {
int lev;
Random value = new Random();
for (lev = 0; Math.abs(value.nextInt()) % 2 == 0; lev++) {
// Do nothing
}
return lev; // returns a random level
}
/**
* Searches for the KVPair using the key which is a Comparable object.
*
* @param key
* key to be searched for
*/
public ArrayList<KVPair<K, V>> search(K key) {
ArrayList<KVPair<K, V>> searchArray = new ArrayList<KVPair<K, V>>();
if(key == null)return searchArray;
SkipNode x = head;
for (int i = head.level; i >= 0; i--) {
while (x.forward[i] != null && x.forward[i].element().getKey().compareTo(key) < 0) {
x = x.forward[i];
}
}
while (x.forward[0] != null && x.forward[0].element().getKey().compareTo(key) == 0) {
searchArray.add(x.forward[0].element());
x = x.forward[0];
}
return searchArray;
}
/**
* @return the size of the SkipList
*/
public int size() {
return size;
}
/**
* Inserts the KVPair in the SkipList at its appropriate spot as designated
* by its lexicoragraphical order.
*
* @param it
* the KVPair to be inserted
*/
@SuppressWarnings("unchecked")
public void insert(KVPair<K, V> it) {
if(it == null)return;
int newLevel = randomLevel();
if (newLevel > head.level) {
adjustHead(newLevel);
}
SkipNode[] update = (SkipNode[]) Array.newInstance(SkipList.SkipNode.class, head.level + 1);
// SkipList<K, V>.SkipNode[] update = new SkipList.SkipNode[head.level + 1];
SkipNode x = head;
for (int i = head.level; i >= 0; i--) {
while (x.forward[i] != null && x.forward[i].element().compareTo(it) < 0) {
x = x.forward[i];
}
update[i] = x;
}
SkipNode newNode = new SkipNode(it, newLevel);
for (int i = 0; i <= newLevel; i++) {
newNode.forward[i] = update[i].forward[i];
update[i].forward[i] = newNode;
}
size++;
}
/**
* Increases the number of levels in head so that no element has more
* indices than the head.
*
* @param newLevel
* the number of levels to be added to head
*/
@SuppressWarnings("unchecked")
private void adjustHead(int newLevel) {
SkipNode temp = head;
head = new SkipNode(null, newLevel);
for (int i = 0; i <= temp.level; i++) {
head.forward[i] = temp.forward[i];
}
}
/**
* Removes the KVPair that is passed in as a parameter and returns true if
* the pair was valid and false if not.
*
* @param pair
* the KVPair to be removed
* @return returns the removed pair if the pair was valid and null if not
*/
@SuppressWarnings("unchecked")
public KVPair<K, V> remove(K key) {
if(key == null)return null;
SkipNode[] update = (SkipNode[]) Array.newInstance(SkipList.SkipNode.class, head.level + 1);
// SkipList<K, V>.SkipNode[] update = new SkipList.SkipNode[head.level + 1];
SkipNode x = head;
for (int i = head.level; i >= 0; i--) {
while (x.forward[i] != null && x.forward[i].element().getKey().compareTo(key) < 0) {
x = x.forward[i];
}
update[i] = x;
}
x = x.forward[0];
if (x != null && x.element().getKey().compareTo(key) == 0) {
for (int i = 0; i <= x.level; i++) {
update[i].forward[i] = x.forward[i];
}
size--;
return x.element();
} else {
return null;
}
}
/**
* Removes a KVPair with the specified value.
*
* @param val
* the value of the KVPair to be removed
* @return returns true if the removal was successful
*/
public KVPair<K, V> removeByValue(V val) {
if(val == null)return null;
SkipNode x = head;
while (x.forward[0] != null && x.forward[0].element().getValue().equals(val) != true) {
x = x.forward[0];
}
if (x.forward[0] != null) {
SkipNode target = x.forward[0];
SkipNode[] update = (SkipNode[]) Array.newInstance(SkipList.SkipNode.class, target.level + 1);
// SkipList<K, V>.SkipNode[] update = new SkipList.SkipNode[head.level + 1];
x = head;
for (int i = target.level; i >= 0; i--) {
while (x.forward[i] != null && x.forward[i].equals(target) != true) {
x = x.forward[i];
}
update[i] = x;
}
x = x.forward[0];
for (int i = 0; i <= target.level; i++) {
update[i].forward[i] = x.forward[i];
}
size--;
return x.element();
} else {
return null;
}
}
/**
* Prints out the SkipList in a human readable format to the console.
*/
public void dump() {
}
/**
* This class implements a SkipNode for the SkipList data structure.
*
* @author CS Staff
*
* @version 2016-01-30
*/
private class SkipNode {
// the KVPair to hold
private KVPair<K, V> pair;
// what is this
private SkipNode [] forward;
// the number of levels
private int level;
/**
* Initializes the fields with the required KVPair and the number of
* levels from the random level method in the SkipList.
*
* @param tempPair
* the KVPair to be inserted
* @param level
* the number of levels that the SkipNode should have
*/
@SuppressWarnings("unchecked")
public SkipNode(KVPair<K, V> tempPair, int level) {
pair = tempPair;
forward = (SkipNode[])Array.newInstance(SkipList.SkipNode.class,
level + 1);
this.level = level;
}
/**
* Returns the KVPair stored in the SkipList.
*
* @return the KVPair
*/
public KVPair<K, V> element() {
return pair;
}
}
private class SkipListIterator implements Iterator<KVPair<K, V>> {
private SkipNode current;
public SkipListIterator() {
current = head;
}
@Override
public boolean hasNext() {
// TODO Auto-generated method stub
return false;
}
@Override
public KVPair<K, V> next() {
// TODO Auto-generated method stub
return null;
}
}
@Override
public Iterator<KVPair<K, V>> iterator() {
// TODO Auto-generated method stub
return new SkipListIterator();
}
}