This repository was archived by the owner on Feb 7, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathInterval_Skip_List.cpp
More file actions
111 lines (111 loc) · 3.34 KB
/
Interval_Skip_List.cpp
File metadata and controls
111 lines (111 loc) · 3.34 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
#include <iostream>
#include <limits>
class IntervalSkipList {
private:
struct Node {
int start;
int end;
Node** next;
int level;
Node(int s, int e, int lev)
: start(s), end(e), level(lev) {
next = new Node*[level + 1];
std::fill(next, next + level + 1, nullptr);
}
~Node() {
delete[] next;
}
};
const int MAX_LEVEL = 16;
const float PROBABILITY = 0.5;
Node* header;
public:
IntervalSkipList() {
header = new Node(std::numeric_limits<int>::min(), std::numeric_limits<int>::max(), MAX_LEVEL);
}
~IntervalSkipList() {
deleteList();
}
void insert(int start, int end) {
Node* update[MAX_LEVEL + 1];
Node* current = header;
for (int i = MAX_LEVEL; i >= 0; --i) {
while (current->next[i] != nullptr && current->next[i]->start < start) {
current = current->next[i];
}
update[i] = current;
}
int newLevel = randomLevel();
if (newLevel > MAX_LEVEL) {
newLevel = MAX_LEVEL;
}
Node* newNode = new Node(start, end, newLevel);
for (int i = 0; i <= newLevel; ++i) {
newNode->next[i] = update[i]->next[i];
update[i]->next[i] = newNode;
}
}
void remove(int start, int end) {
Node* update[MAX_LEVEL + 1];
Node* current = header;
for (int i = MAX_LEVEL; i >= 0; --i) {
while (current->next[i] != nullptr && current->next[i]->start < start) {
current = current->next[i];
}
update[i] = current;
}
current = current->next[0];
if (current != nullptr && current->start == start && current->end == end) {
for (int i = 0; i <= MAX_LEVEL; ++i) {
if (update[i]->next[i] != current) {
break;
}
update[i]->next[i] = current->next[i];
}
delete current;
}
}
void search(int start, int end) {
Node* current = header;
for (int i = MAX_LEVEL; i >= 0; --i) {
while (current->next[i] != nullptr && current->next[i]->start < end) {
if (current->next[i]->end > start) {
std::cout << "[" << current->next[i]->start << ", " << current->next[i]->end << "] ";
}
current = current->next[i];
}
}
std::cout << std::endl;
}
private:
int randomLevel() {
int level = 0;
while (level < MAX_LEVEL && ((double)std::rand() / RAND_MAX) < PROBABILITY) {
level++;
}
return level;
}
void deleteList() {
Node* current = header->next[0];
while (current != nullptr) {
Node* temp = current;
current = current->next[0];
delete temp;
}
delete header;
}
};
int main() {
IntervalSkipList skipList;
skipList.insert(15, 25);
skipList.insert(5, 12);
skipList.insert(7, 17);
skipList.insert(8, 23);
skipList.insert(16, 27);
std::cout << "Skip List after insertion:" << std::endl;
skipList.search(10, 20);
skipList.remove(8, 23);
std::cout << "Skip List after removal:" << std::endl;
skipList.search(10, 20);
return 0;
}