-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSkipList.cpp
188 lines (171 loc) · 5.51 KB
/
SkipList.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
#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;
}
/***********************************************************/
/*************** FUNCTIONS TO BE COMPLETED ****************/
/***********************************************************/
/////////////////////////////////////////////////////////////
///////////////////// ADD FUNCTIONS ////////////////////////
/////////////////////////////////////////////////////////////
unsigned int SkipList::randHeight() {
////////////// Write your code below ////////////////////////
int t = rand();
unsigned int i;
int j;
for ( i = 1, j = 2; i < m_maxHeight; ++i, j+=j ) {
if ( t > RAND_MAX/j ) {
break;
}
}
return i;
}
int SkipList::add(SkipListNode* target, SkipListNode* newNode, unsigned int level) {
if (target->nextAtLevel(level) != NULL &&
(*target->nextAtLevel(level)) < *newNode) {
countAdd++;
}
////////////// Write your code below ////////////////////////
SkipListNode *tempNode = target->nextAtLevel(level);
if ( tempNode == NULL || *newNode < *tempNode ) {
if (level < newNode->height()) {
newNode->setNextAtLevel(level, tempNode);
target->setNextAtLevel(level, newNode);
}
if (level > 0) {
add( target, newNode, level - 1 );
}
return 1;
}
/*if ( level < 0 ) {
return 0;
}*/
return add( tempNode, 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++;
}
////////////// Write your code below ////////////////////////
/*if ( target == NULL ) {
return NULL;
}*/
if ( *target == key ) {
return target;
}
SkipListNode *tempNode = target->nextAtLevel(level);
if ( tempNode == NULL || key < *tempNode ) {
if ( level == 0 ) {
return NULL;
}
return find( target, key, level - 1 );
}
return find( tempNode, key, level );
}
/////////////////////////////////////////////////////////////
///////////////////// DEL FUNCTION ////////////////////////
/////////////////////////////////////////////////////////////
SkipListNode* SkipList::del(SkipListNode* target, const Key& key, unsigned int level) {
if (target->nextAtLevel(level) != NULL && *(target->nextAtLevel(level)) < key) {
countDelete++;
}
////////////// Write your code below ////////////////////////
/*if ( find( key, false ) != 1 ) {
return NULL;
}*/
SkipListNode *tempNode = target->nextAtLevel( level );
if ( tempNode == NULL || key < *tempNode ) {
if ( level == 0 ) {
return NULL;
}
return del( target, key, level - 1);
}
if ( key == *tempNode ) {
target->setNextAtLevel( level, tempNode->nextAtLevel( level ) );
if ( level > 0 ) {
del( target, key, level - 1);
}
return tempNode;
}
return del ( tempNode, key, level );
}