-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSkipLists.java
More file actions
313 lines (253 loc) · 9.87 KB
/
Copy pathSkipLists.java
File metadata and controls
313 lines (253 loc) · 9.87 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
304
305
306
307
308
//
// Created by Isaias Perez
// ====================
// Skip Lists
//
// Reads from text file a set of commands, one per line.
// Commands: i, s, d, p (insert, search, delete, and print respectevly)
// All but the print command are expected as the following format: i 24
// Command should be followed by a space and an integer.
// Insert/Delete/Search operations achieved in O(log n) best.
// To run: java SkipList commands.txt or java SkipList commands.txt x (seed set to 42)
import java.io.File;
import java.util.Scanner;
import java.util.Random;
import java.lang.Math;
// -------------------------------------- //
// Implememtns all necessary methods for //
// insert, search, delete, print //
public class SkipLists {
public static void main(String[] args) {
// Check there is more than one command line argument
if (args.length > 0) {
// Random number seed
long seed = 42;
int epoch = 0;
File fileName = new File(args[0]);
// 2nd command line argument sets the random generators seed to 42
if (args.length == 2) {
epoch = 1;
}
// Creating skiplist and reading input file
SkipLists mClass = new SkipLists();
skipList list = new SkipLists().new skipList();
mClass.readFile(fileName, list, epoch);
} else {
System.err.println("Input file not specified!, try again");
}
}
// ---------------------------------------- //
// Node class used for bulding the skiplist //
public class Node {
public String key;
public long value;
public Node left;
public Node right;
public Node up;
public Node down;
// Initializing Node values
public Node(long value){
this.left = null;
this.right = null;
this.up = null;
this.down = null;
this.value = value;
}
}
// ------------------------------------------------------- //
// Class creates default skiplist, with infinity endpoints //
// and functionality for adding levels //
public class skipList {
public long posInf = 100000000;
public long negInf = -100000000;
public Node head, tail;
public int size, maxLevel;
public long flip;
// Initializing SkipList
public skipList() {
Node nInf = new Node(negInf);
Node pInf = new Node(posInf);
nInf.right = pInf;
pInf.left = nInf;
this.head = nInf;
this.tail = pInf;
this.maxLevel = 1;
this.size = 0;
}
// Creates empty level
public void addLevel() {
Node newnInf = new Node(this.negInf);
Node newpInf = new Node(this.posInf);
// Linking to lower level
newnInf.down = head;
newnInf.right = newpInf;
newpInf.down = tail;
newpInf.left = newnInf;
head.up = newnInf;
tail.up = newpInf;
// Update logic markers
head = newnInf;
tail = newpInf;
maxLevel++;
}
}
// ------------------------------------------------- //
// Reads input file and executes commands one by one //
public void readFile(File fileName, skipList list, int epoch) {
try {
// Random sequence
Random randomizer = new Random();
if (epoch == 0) {
randomizer.setSeed(42);
}
Node tmp;
Scanner scan = new Scanner(fileName);
System.out.println("For the input file named " + fileName);
if (epoch == 0) {
System.out.println("With the RNG unseeded,");
} else {
System.out.println("With the RNG seeded,");
}
while (scan.hasNextLine()) {
String line = scan.nextLine();
String[] command = line.split(" ");
// Execute commands
switch(command[0]) {
case "i": // Insert to SkipList
insert(list, Integer.parseInt(command[1]), randomizer);
break;
case "s": // Search through SkipList
tmp = search(list, Integer.parseInt(command[1]));
if (tmp.value == Integer.parseInt(command[1])) {
System.out.println(tmp.value + " found");
} else {
System.out.println(command[1] + " NOT FOUND");
}
break;
case "d": // Delete from SkipList
delete(list, Integer.parseInt(command[1]));
break;
case "p": // Print all values of SkipList
printList(list);
break;
}
}
} catch (Exception e) {
e.printStackTrace();
}
}
// ---------------------------------------------------- //
// Finds the node with the value <= key at lowest level //
public Node search(skipList list, int key) {
Node x = list.head;
// Traverse Levels, top to bottom
for (int y = list.maxLevel; y > 0; y--) {
// Traverse Level
while (x.right.value != list.posInf && x.right.value <= key) {
x = x.right;
}
// Attempt to go down the levels
if (x.down != null) {
x = x.down;
} else {
break; // Reached bottom level
}
}
return x;
}
// -------------------------------------------------------------------- //
// Inserts new value into skiplist, will discard any duplicate attempts //
public void insert(skipList list, int value, Random randomizer) {
Node newEntry = new Node(value);
Node current = search(list, value);
int stackHeight;
// Already in skipList, discard. Add otherwise
if (current.value != value) {
newEntry.left = current;
newEntry.right = current.right;
current.right = newEntry;
newEntry.right.left = newEntry;
// Promote node randomly
stackHeight = 1;
while ((randomizer.nextInt() % 2) == 1) {
// Add level if necessary
if (stackHeight >= list.maxLevel) {
list.addLevel();
}
// From current position, climb up
while (current.up == null) {
current = current.left;
}
current = current.up;
// New level Linkeage
Node levelUp = new Node(value);
levelUp.left = current;
levelUp.right = current.right;
current.right.left = levelUp;
current.right = levelUp;
levelUp.down = newEntry;
newEntry.up = levelUp;
// Link in case value gets promoted again
newEntry = levelUp;
stackHeight++;
}
// Update logic markers
list.size++;
if (stackHeight > list.maxLevel) {
list.maxLevel = stackHeight;
}
}
}
// ------------------------------------- //
// Prints all vaules in current skiplist //
public void printList(skipList list) {
Node current = list.head;
// Level 1 contains all values in list, level down
while (current.down != null) {
current = current.down;
}
// At level 1, traverse level and print each node stack
System.out.println("the current Skip List is shown below:");
System.out.println("---infinity");
while (current.value != list.tail.value) {
current = current.right;
if (current.value != list.posInf) {
printStack(current);
}
}
System.out.println("+++infinity");
System.out.println("---End of Skip List---");
}
// ---------------------------------------------- //
// Helps print the skiplist, print only one stack //
public void printStack(Node stackBottom) {
System.out.print(" " + stackBottom.value + "; ");
if (stackBottom.up == null) {
System.out.print("\n");
}
while (stackBottom.up != null) {
stackBottom = stackBottom.up;
System.out.print(" " + stackBottom.value + "; ");
if (stackBottom.up == null) {
System.out.print("\n");
break;
}
}
}
// -------------------------------------------//
// Deletes value from all levels if it exists //
public void delete(skipList list, int key) {
Node current = search(list, key);
if (current.value == key) {
while (current != null) {
current.left.right = current.right;
current.right.left = current.left;
current = current.up;
}
System.out.println(key + " deleted");
list.size--;
} else {
System.out.println(key + " integer not found - delete not successful");
}
}
}