-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSkipList.cpp
More file actions
175 lines (149 loc) · 3.73 KB
/
Copy pathSkipList.cpp
File metadata and controls
175 lines (149 loc) · 3.73 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
#include "SkipList.hpp"
/* PROVIDED FUNCTIONS */
void SkipList::init(int maxHeight) {
m_maxHeight = maxHeight;
m_head = new SkipListNode("", m_maxHeight);
}
SkipList::~SkipList() {
while (m_head != NULL) {
SkipListNode* head = m_head;
m_head = m_head->nextAtLevel(0);
delete head;
}
}
int SkipList::add(const Key& key, bool verbose) {
if (find(key, false) != 0) {
if (verbose) {
cout<<"Node "<<key<<" is already in the list."<<endl;
}
return 0;
}
SkipListNode* newNode = new SkipListNode(key, randHeight());
if (verbose) {
cout<<"Add new node "<<*newNode<<" with height "<<newNode->height()<<endl;
}
int ret = add (m_head, newNode, m_maxHeight-1);
if (ret == 0 ) {
return ret;
}
return 1;
}
int SkipList::find(const Key &key, bool verbose) {
SkipListNode* ret =find (m_head, key, m_maxHeight-1) ;
if (ret == NULL) {
if (verbose) {
cout<< "Node "<<key<<" is not in the list"<<endl;
}
return 0;
} else {
if (verbose) {
cout<< "Node "<<key<<" is in the list"<<endl;
}
return 1;
}
}
int SkipList::del(const Key& key, bool verbose) {
if (key.length() == 0){
return 1;
}
SkipListNode* toBeDeleted = del(m_head, key, m_maxHeight-1);
if (toBeDeleted == NULL) {
if (verbose) {
cout<< "Node "<<key<<" is not in the list"<<endl;
}
} else {
delete toBeDeleted;
if (verbose) {
cout<< "Node "<<key<<" is deleted from the list"<<endl;
}
}
return 1;
}
void SkipList::dump(char sep) {
int length = -1;
cout<<"Current List: ";
for ( SkipListNode* iter = m_head; (iter != NULL); iter=iter->nextAtLevel(0)) {
length++;
cout << string(*iter)<<" ";
cout <<"("<< iter->height() <<":";
unsigned int i;
for (i=0; i< iter->height(); i++) {
if (iter->nextAtLevel(i)) {
cout<<" "<<i<<":"<<*(iter->nextAtLevel(i));
cout.flush();
}
}
cout<<")"<<sep;
}
cout<<length<<" nodes in total."<<endl;
}
/* Returns a random value between 0 and the maximum height */
unsigned int SkipList::randHeight() {
int t = rand() + 1;
int j = 2;
unsigned int i;
for (i = 1; i < m_maxHeight; i++){
if (t > RAND_MAX/j) break;
j = 2 * j;
}
return i;
}
/* Add function */
int SkipList::add(SkipListNode* target, SkipListNode* newNode, unsigned int level) {
if (target->nextAtLevel(level) != NULL &&
(*target->nextAtLevel(level)) < *newNode) {
countAdd++;
}
/* Main insertion */
if (*target == *newNode)
return 0;
SkipListNode * next = target -> nextAtLevel(level);
if (next == NULL || *newNode < *next){
if (level < newNode -> height()){
target -> setNextAtLevel(level, newNode);
newNode -> setNextAtLevel(level, next);
}
if (level > 0){
return add(target, newNode, level-1);
}
return 1;
}
return add(next, newNode, level);
}
/* Find function */
SkipListNode* SkipList::find(SkipListNode* target, const Key& key, unsigned int level) {
if (target->nextAtLevel(level) != NULL && *(target->nextAtLevel(level)) < key) {
countFind++;
}
if (target == NULL)
return NULL;
if (target -> compare(key) == 0)
return target;
SkipListNode * next = target -> nextAtLevel(level);
if (next == 0 || *next > key){
if (level == 0){
return NULL;
}
return find(target, key, level-1);
}
return find(next, key, level);
}
/* Delete function */
SkipListNode* SkipList::del(SkipListNode* target, const Key& key, unsigned int level) {
if (target->nextAtLevel(level) != NULL && *(target->nextAtLevel(level)) < key) {
countDelete++;
}
SkipListNode * next = target -> nextAtLevel(level);
if (next == NULL || *next > key){
if (level > 0)
return del(target, key, level-1);
return NULL;
}
else if (*next == key) {
target -> setNextAtLevel(level, next -> nextAtLevel(level));
if (level == 0)
return next;
return del(target, key, level-1);
}
return del(next, key, level);
}