-
Notifications
You must be signed in to change notification settings - Fork 24
/
Copy pathskip_list_study.cpp
267 lines (208 loc) · 6.22 KB
/
skip_list_study.cpp
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
//
// Created by yche on 10/24/18.
//
#include <iostream>
#include <string>
#include <sstream>
#include <vector>
#include <algorithm>
// https://codereview.stackexchange.com/questions/116345/skip-list-implementation
class Skip_list {
public:
Skip_list();
~Skip_list();
// non-modifying member functions
/*
It prints the key, value, level
of each node of the skip list.
Prints two nodes per line.
*/
void print() const;
/*
It searches the skip list and
returns the element corresponding
to the searchKey; otherwise it returns
failure, in the form of null pointer.
*/
std::string *find(int searchKey) const;
// modifying member functions
/*
It searches the skip list for elements
with seachKey, if there is an element
with that key its value is reassigned to the
newValue, otherwise it creates and splices
a new node, of random level.
*/
void insert(int searchKey, const std::string &newValue);
/*
It deletes the element containing
searchKey, if it exists.
*/
void erase(int searchKey);
private:
struct Node {
int key;
std::string value;
// pointers to successor nodes
std::vector<Node *> forward;
Node(int k, const std::string &v, int level) :
key(k), value(v), forward(level, nullptr) {}
};
// Generates node levels in the range [1, maxLevel).
int randomLevel() const;
//Returns number of incoming and outgoing pointers
static int nodeLevel(const Node *v);
//creates a node on the heap and returns a pointer to it.
static Node *makeNode(int key, std::string val, int level);
// Returns the first node for which node->key < searchKey is false
Node *lower_bound(int searchKey) const;
/*
* Returns a collection of Pointers to Nodes
* result[i] hold the last node of level i+1 for which result[i]->key < searchKey is true
*/
std::vector<Node *> predecessors(int searchKey) const;
// data members
const float probability;
const int maxLevel;
Node *head; // pointer to first node
Node *NIL; // last node
};
//==============================================================================
// Class Skip_list member implementations
Skip_list::Skip_list() :
probability(0.5),
maxLevel(16) {
int headKey = std::numeric_limits<int>::min();
head = new Node(headKey, "head", maxLevel);
int nilKey = std::numeric_limits<int>::max();
NIL = new Node(nilKey, "NIL", maxLevel);
std::fill(head->forward.begin(), head->forward.end(), NIL);
}
Skip_list::~Skip_list() {
auto node = head;
while (node->forward[0]) {
auto tmp = node;
node = node->forward[0];
delete tmp;
}
delete node;
}
std::string *Skip_list::find(int searchKey) const {
std::string *res{};
if (auto x = lower_bound(searchKey)) {
if (x->key == searchKey && x != NIL) {
res = &(x->value);
}
}
return res;
}
void Skip_list::print() const {
Node *list = head->forward[0];
int lineLenght = 0;
std::cout << "{";
while (list != NIL) {
std::cout << "value: " << list->value
<< ", key: " << list->key
<< ", level: " << nodeLevel(list);
list = list->forward[0];
if (list != NIL) std::cout << " : ";
if (++lineLenght % 2 == 0) std::cout << "\n";
}
std::cout << "}\n";
}
void Skip_list::insert(int searchKey, const std::string &newValue) {
auto preds = predecessors(searchKey);
{//reassign value if node exists and return
auto next = preds[0]->forward[0];
if (next->key == searchKey && next != NIL) {
next->value = newValue;
return;
}
}
// create new node
const int newNodeLevel = randomLevel();
auto newNodePtr = makeNode(searchKey, newValue, newNodeLevel);
// connect pointers of predecessors and new node to respective successors
for (int i = 0; i < newNodeLevel; ++i) {
newNodePtr->forward[i] = preds[i]->forward[i];
preds[i]->forward[i] = newNodePtr;
}
}
void Skip_list::erase(int searchKey) {
auto preds = predecessors(searchKey);
//check if the node exists
auto node = preds[0]->forward[0];
if (node->key != searchKey || node == NIL) {
return;
}
// update pointers and delete node
for (size_t i = 0; i < nodeLevel(node); ++i) {
preds[i]->forward[i] = node->forward[i];
}
delete node;
}
//###### private member functions ######
int Skip_list::nodeLevel(const Node *v) {
return v->forward.size();
}
Skip_list::Node *Skip_list::makeNode(int key, std::string val, int level) {
return new Node(key, val, level);
}
int Skip_list::randomLevel() const {
int v = 1;
while (((double) std::rand() / RAND_MAX) < probability &&
v < maxLevel) {
v++;
}
return v;
}
Skip_list::Node *Skip_list::lower_bound(int searchKey) const {
Node *x = head;
for (unsigned int i = nodeLevel(head); i-- > 0;) {
while (x->forward[i]->key < searchKey) {
x = x->forward[i];
}
}
return x->forward[0];
}
std::vector<Skip_list::Node *> Skip_list::predecessors(int searchKey) const {
std::vector<Node *> result(nodeLevel(head), nullptr);
Node *x = head;
for (unsigned int i = nodeLevel(head); i-- > 0;) {
while (x->forward[i]->key < searchKey) {
x = x->forward[i];
}
result[i] = x;
}
return result;
}
//==================================================
int main() {
// 1.Initialize an empty Skip_list object
Skip_list s;
// 2. insert()
for (int i = 0; i < 50; ++i) {
std::stringstream ss;
ss << i;
s.insert(i, ss.str());
}
// 2a. print()
s.print();
std::cout << std::endl;
// 3. find()
auto f = s.find(10);
if (f) std::cout << "Node found!\nvalue: " << f << '\n';
else std::cout << "Node NOT found!\n";
// 4. insert() - reassign
s.insert(40, "TEST");
// 4a. print()
s.print();
std::cout << std::endl;
// 5. erase()
s.erase(40);
// 5a. print();
s.print();
std::cout << std::endl;
std::cout << "\nDone!\n";
// getchar();
}