-
Notifications
You must be signed in to change notification settings - Fork 0
/
SkipList.java
197 lines (168 loc) · 5.81 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
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
package com.company;
import java.util.*;
public class Main {
static <T extends Comparable<T>> boolean isSorted(List<T> list){
for(int i = 1; i < list.size(); ++i){
int cmp = list.get(i - 1).compareTo(list.get(i));
if(cmp > 0) return false;
}
return true;
}
public static void main(String[] args) {
}
}
// only keys
// keys are ordered
// TODO: consider using arrays of linked lists
class SkipList<T extends Comparable<T>> implements Iterable<T>{
// members
private List<Node> linkedLists;
private List<Integer> sizes;
private int count;
@Override
public Iterator<T> iterator() {
return new SkipListIterator();
}
private class SkipListIterator implements Iterator<T>{
Node it = linkedLists.get(0).right;
@Override
public boolean hasNext() {
return it != null;
}
@Override
public T next() {
T element = it.element;
it = it.right;
return element;
}
}
// auxiliary class
private class Node implements Comparable<Node>{
T element;
Node bottom, left, right;
public Node(T element){
this(element, null, null, null);
}
public Node(T element, Node bottom, Node left, Node right){
this.element = element;
this.bottom = bottom;
this.left = left;
this.right = right;
}
@Override
public int compareTo(Node other) {
return element.compareTo(other.element);
}
public int compareTo(T x){
if(element == null) return -1;
return element.compareTo(x);
}
}
private static class Flip{
static Random rnd = new Random(System.currentTimeMillis());
final static int heads = 0, tails = 1;
public static int flip(){
return (int)rnd.nextInt(2);
}
}
// constructors
public SkipList() {
linkedLists = new ArrayList<>();
linkedLists.add(new Node(null));
sizes = new ArrayList<>();
sizes.add(1);
}
public void insert(T element){
/*if(isEmpty()){
linkedLists.add(new Node(null));
sizes.add(1);
}*/
List<Node> levelNodes = floor(element);
if(levelNodes.get(0).compareTo(element) != 0){
// must be inserted into the first level
Node prev = levelNodes.get(0);
Node next = prev.right;
Node newNode = new Node(element, null, prev, next);
prev.right = newNode;
if(next != null) next.left = newNode;
int curLevel = 0;
sizes.set(curLevel, sizes.get(curLevel) + 1);
curLevel++;
//flip coins
while(Flip.flip() == Flip.heads && curLevel < linkedLists.size()){
System.out.println("linked list size is : " + linkedLists.size());
prev = levelNodes.get(curLevel);
next = prev.right;
newNode = new Node(element, levelNodes.get(curLevel - 1).right, prev, next);
prev.right = newNode;
if(next != null) next.left = newNode;
sizes.set(curLevel, sizes.get(curLevel) + 1);
curLevel++;
}
if(Flip.flip() == Flip.heads && curLevel == linkedLists.size()){
linkedLists.add(new Node(null, linkedLists.get(curLevel - 1), null, null));
prev = linkedLists.get(linkedLists.size() - 1);
//prev = levelNodes.get(curLevel);
//next = prev.right;
newNode = new Node(element, levelNodes.get(curLevel - 1).right, prev, null);
prev.right = newNode;
//if(next != null) next.left = newNode;
//sizes.set(curLevel, sizes.get(curLevel) + 1);
sizes.add(2);
}
count++;
}
}
public void remove(T element){
// TODO: maintain the invariant that empty lists gotta be removed, to avoid curNode != null check in insert
List<Node> levelNodes = floor(element);
Node x = levelNodes.get(0);
if(x.compareTo(element) == 0){ // the element is in the list
//TODO: remove redundancy
for(int i = levelNodes.size() - 1; i >= 0; --i){
Node temp = levelNodes.get(i);
if(temp.compareTo(element) == 0){
if(sizes.get(i) == 2 && i != 0){
sizes.remove(i);
linkedLists.remove(i);
} else{
Node prev = temp.left;
Node next = temp.right;
prev.right = next;
//TODO: check wtf is wrong
if(next != null)
next.left = prev;
sizes.set(i, sizes.get(i) - 1);
}
}
}
count--;
}
}
public boolean contains(T element){
List<Node> levelNodes = floor(element);
Node temp = levelNodes.get(0);
if(temp.compareTo(element) != 0) return false;
return true;
}
public boolean isEmpty(){
return count == 0;
}
public int size(){
return count;
}
// helper methods
private List<Node> floor(T element){
List<Node> list = new ArrayList<>();
Node curNode = linkedLists.get(linkedLists.size() - 1);
while(curNode != null){
while(curNode.right != null && curNode.right.compareTo(element) <= 0){
curNode = curNode.right;
}
list.add(curNode);
curNode = curNode.bottom;
}
Collections.reverse(list);
return list;
}
}