-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathskiplist.cpp
More file actions
267 lines (227 loc) · 7.27 KB
/
skiplist.cpp
File metadata and controls
267 lines (227 loc) · 7.27 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
/*
Skip list is a probabilistic data structure that maintains a sorted collection of elements.
It uses multiple levels of linked lists to achieve O(log n) average time complexity for
search, insertion, and deletion operations. Elements are inserted with randomly determined
heights, creating express lanes for faster traversal.
Standard library alternatives:
- C++: std::set / std::map (red-black tree, O(log n) guaranteed)
- Python: No built-in sorted set (use bisect module for sorted lists)
- Java: TreeSet / TreeMap (red-black tree, O(log n) guaranteed)
Time complexity: O(log n) average for search, insert, and delete operations.
Space complexity: O(n) on average, where n is the number of elements.
*/
#include <cassert>
#include <cstdlib>
#include <ctime>
#include <iostream>
#include <string>
#include <vector>
template <typename T>
class SkipListNode {
public:
T value;
std::vector<SkipListNode*> forward;
SkipListNode(const T& val, int level) : value(val), forward(level + 1, nullptr) {}
};
template <typename T>
class SkipList {
private:
int max_level;
float p;
int level;
SkipListNode<T>* header;
int random_level() {
int lvl = 0;
while ((float)rand() / RAND_MAX < p && lvl < max_level) { lvl++; }
return lvl;
}
public:
SkipList(int max_lvl = 16, float prob = 0.5) : max_level(max_lvl), p(prob), level(0) {
header = new SkipListNode<T>(T(), max_level);
}
~SkipList() {
SkipListNode<T>* current = header;
while (current != nullptr) {
SkipListNode<T>* next = current->forward[0];
delete current;
current = next;
}
}
// Delete copy and move operations (not needed for competition)
SkipList(const SkipList&) = delete;
SkipList& operator=(const SkipList&) = delete;
SkipList(SkipList&&) = delete;
SkipList& operator=(SkipList&&) = delete;
SkipList& insert(const T& value) {
std::vector<SkipListNode<T>*> update(max_level + 1);
SkipListNode<T>* current = header;
for (int i = level; i >= 0; i--) {
while (current->forward[i] != nullptr && current->forward[i]->value < value) {
current = current->forward[i];
}
update[i] = current;
}
int lvl = random_level();
if (lvl > level) {
for (int i = level + 1; i <= lvl; i++) { update[i] = header; }
level = lvl;
}
SkipListNode<T>* new_node = new SkipListNode<T>(value, lvl);
for (int i = 0; i <= lvl; i++) {
new_node->forward[i] = update[i]->forward[i];
update[i]->forward[i] = new_node;
}
return *this;
}
bool search(const T& value) {
SkipListNode<T>* current = header;
for (int i = level; i >= 0; i--) {
while (current->forward[i] != nullptr && current->forward[i]->value < value) {
current = current->forward[i];
}
}
current = current->forward[0];
return current != nullptr && current->value == value;
}
bool remove(const T& value) {
std::vector<SkipListNode<T>*> update(max_level + 1);
SkipListNode<T>* current = header;
for (int i = level; i >= 0; i--) {
while (current->forward[i] != nullptr && current->forward[i]->value < value) {
current = current->forward[i];
}
update[i] = current;
}
current = current->forward[0];
if (current == nullptr || current->value != value) { return false; }
for (int i = 0; i <= level; i++) {
if (update[i]->forward[i] != current) { break; }
update[i]->forward[i] = current->forward[i];
}
delete current;
while (level > 0 && header->forward[level] == nullptr) { level--; }
return true;
}
// Optional functionality (not always needed during competition)
int size() const {
int count = 0;
SkipListNode<T>* current = header->forward[0];
while (current != nullptr) {
count++;
current = current->forward[0];
}
return count;
}
std::vector<T> to_vector() const {
std::vector<T> result;
SkipListNode<T>* current = header->forward[0];
while (current != nullptr) {
result.push_back(current->value);
current = current->forward[0];
}
return result;
}
bool contains(const T& value) {
return search(value);
}
};
void test_main() {
srand(42);
SkipList<int> sl;
sl.insert(10).insert(20).insert(5).insert(15);
assert(sl.search(10));
assert(sl.search(20));
assert(!sl.search(25));
assert(sl.remove(10));
assert(!sl.search(10));
assert(!sl.remove(30));
// Optional functionality (not always needed during competition)
srand(42);
SkipList<int> sl2;
sl2.insert(3).insert(1).insert(4).insert(1).insert(5);
assert(sl2.size() == 5);
std::vector<int> expected = {1, 1, 3, 4, 5};
assert(sl2.to_vector() == expected);
assert(sl2.contains(3));
assert(!sl2.contains(7));
}
// Don't write tests below during competition.
void test_basic_operations() {
srand(123);
SkipList<int> sl;
assert(!sl.search(1));
sl.insert(5);
assert(sl.search(5));
assert(!sl.search(4));
}
void test_multiple_inserts() {
srand(456);
SkipList<int> sl;
std::vector<int> values = {10, 5, 15, 3, 7, 12, 20};
for (int v : values) { sl.insert(v); }
for (int v : values) { assert(sl.search(v)); }
assert(!sl.search(1));
assert(!sl.search(100));
}
void test_delete_operations() {
srand(789);
SkipList<int> sl;
sl.insert(10).insert(20).insert(30);
assert(sl.remove(20));
assert(!sl.search(20));
assert(sl.search(10));
assert(sl.search(30));
assert(!sl.remove(20));
assert(!sl.remove(40));
}
void test_duplicate_values() {
srand(101);
SkipList<int> sl;
sl.insert(5).insert(5).insert(5);
assert(sl.size() == 3);
std::vector<int> expected = {5, 5, 5};
assert(sl.to_vector() == expected);
}
void test_ordered_insertion() {
srand(202);
SkipList<int> sl;
for (int i = 1; i <= 10; i++) { sl.insert(i); }
std::vector<int> expected;
for (int i = 1; i <= 10; i++) { expected.push_back(i); }
assert(sl.to_vector() == expected);
}
void test_reverse_insertion() {
srand(303);
SkipList<int> sl;
for (int i = 10; i >= 1; i--) { sl.insert(i); }
std::vector<int> expected;
for (int i = 1; i <= 10; i++) { expected.push_back(i); }
assert(sl.to_vector() == expected);
}
void test_empty_skiplist() {
srand(404);
SkipList<int> sl;
assert(sl.size() == 0);
assert(sl.to_vector().empty());
assert(!sl.remove(5));
}
void test_strings() {
srand(505);
SkipList<std::string> sl;
sl.insert("dog").insert("cat").insert("bird").insert("ant");
assert(sl.search("cat"));
std::vector<std::string> expected = {"ant", "bird", "cat", "dog"};
assert(sl.to_vector() == expected);
}
int main() {
test_basic_operations();
test_multiple_inserts();
test_delete_operations();
test_duplicate_values();
test_ordered_insertion();
test_reverse_insertion();
test_empty_skiplist();
test_strings();
test_main();
return 0;
}