-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathskiplist.cpp
More file actions
159 lines (154 loc) · 4.01 KB
/
skiplist.cpp
File metadata and controls
159 lines (154 loc) · 4.01 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
#include "skiplist.h"
#include "random.h"
#include <cassert>
#include <climits>
#include <cstdlib>
#include <iostream>
#include <unordered_map>
using namespace std;
// checks if the value needs to be inserted at a higher level
bool SkipList::shouldInsertAtHigherLevel() const {
return probability >= Random::random() % 100;
}
// you code goes here
// skiplist ostream
ostream &operator<<(ostream &out, const SkipList &skip) {
for (int i = skip.levels - 1; i >= 0; i--) {
out << "[level: " << i + 1 << "] ";
SNode *curr = skip.head->next[i];
while (curr != nullptr) {
out << curr->val << "-->";
curr = curr->next[i];
}
out << "nullptr" << endl;
}
return out;
}
// constructor snode
SNode::SNode(int val) : val{val} { next.resize(3); }
// copy constructor snode
SNode::SNode(const SNode &other) {
val = other.val;
next = other.next;
}
// constructor skiplist
SkipList::SkipList(int levels, int probability) {
this->levels = levels;
this->probability = probability;
head = new SNode;
for (int i = 0; i < levels; i++) {
head->next.push_back(nullptr);
}
}
// copy constructor skiplist
SkipList::SkipList(const SkipList &other) {
levels = other.levels;
probability = other.probability;
head = new SNode{other.head->val};
for (int i = 0; i < levels; i++) {
head->next.push_back(nullptr);
}
for (int i = levels - 1; i >= 0; i--) {
SNode *temp = other.head->next[i];
SNode *newHead = head;
while (temp != nullptr) {
SNode *curr = new SNode{temp->val};
newHead->next[i] = curr;
temp = temp->next[i];
newHead = newHead->next[i];
}
}
}
// skiplist destructor
SkipList::~SkipList() {
SNode *temp = head;
SNode *next;
while (temp != nullptr) {
next = temp->next[0];
delete temp;
temp = next;
}
head = nullptr;
}
// skiplist getBefore nodes
vector<SNode *> SkipList::getBeforeNodes(int val) const {
vector<SNode *> prevNode;
for (int i = 0; i < levels; i++) {
SNode *start = head;
while (start != nullptr && start->next[i] != nullptr &&
start->next[i]->val < val) {
start = start->next[i];
}
prevNode.push_back(start);
}
return prevNode;
}
// skiplist add
void SkipList::add(int val) {
SNode *start = head;
SNode *insertVal = new SNode{val};
vector<SNode *> prevNode = getBeforeNodes(val);
for (int i = 0; i < levels; i++) {
start = prevNode[i];
while (start != nullptr) {
// if (start->val == prevNode[i]->val) {
insertVal->next[i] = start->next[i];
start->next[i] = insertVal;
start = start->next[i]; // at inserted node
while (i < levels - 1 && shouldInsertAtHigherLevel()) {
i = i + 1;
SNode *curr = prevNode[i];
if (curr->val == prevNode[i]->val && curr->next[i] != nullptr) {
insertVal->next[i] = curr->next[i];
curr->next[i] = start;
} else if (curr->val == prevNode[i]->val && curr->next[i] == nullptr) {
curr->next[i] = start;
}
}
return;
}
}
delete insertVal;
}
// skiplist add vector
void SkipList::add(const vector<int> &values) {
for (int i = 0; i < values.size(); i++) {
add(values[i]);
}
}
// skiplist remove
bool SkipList::remove(int val) {
if (!contains(val)) {
return false;
}
SNode *start;
SNode *removeVal = nullptr;
vector<SNode *> prevNode = getBeforeNodes(val);
bool success = false;
for (int i = levels - 1; i >= 0; i--) {
start = prevNode[i];
if (start->val == prevNode[i]->val && start->next[i]->val == val) {
removeVal = start->next[i];
start->next[i] = start->next[i]->next[i];
success = true;
} else {
start = start->next[i];
}
}
delete removeVal;
return success;
}
// skiplist contains
bool SkipList::contains(int val) const {
for (int i = levels - 1; i >= 0; i--) {
SNode *start = head;
start = start->next[i];
while (start != nullptr && start->val < val) {
start = start->next[i];
}
if (start != nullptr && start->val == val) {
return true;
}
}
return false;
}