-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSplayTree.cpp
345 lines (308 loc) · 9.89 KB
/
SplayTree.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
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
/*
Author: Gabriel Sellés Salvà
UCM Student 2016-2017
*/
#include "SplayTreeHeader.h"
//Public methods implementation
SplayTree::SplayTree() {
root = NULL;
}
bool SplayTree::insert(int element) {
if (isEmpty()) return insert_if_empty(element);
return insert_if_not_empty(element);
}
bool SplayTree::search(int elem) {
//If it's empty, we won't find it.
if (isEmpty()) return false;
return search_if_not_empty(elem);
}
bool SplayTree::remove(int elem) {
//If its empty, we can't remove anything.
if (isEmpty()) return false;
return remove_if_not_empty(elem);
}
//Private methods implementation.
bool SplayTree::isEmpty() {
return root == NULL;
}
void SplayTree::get_biggest(Node* &actualNode) {
//If it has a left son, we look for the node that's more to the right from his right son.
actualNode = root->leftSon;
while (actualNode->rightSon != NULL) {
//While it has a right son...
actualNode = actualNode->rightSon;
}
//Once this finished, actualNode is going to point to a node that it hasn't a right son (it's going to be the bigger from the original actualNode's left son).
}
bool SplayTree::study_right_son(Node* &actualNode, int element) {
//If it has not a right son, we have a place to put it.
if (actualNode->rightSon == NULL) {
insert_to_right_son(actualNode, element);
return true;
}
//If not, we change the node we are studying. Now it's going to be his right son.
actualNode = actualNode->rightSon;
return false;
}
bool SplayTree::study_left_son(Node* &actualNode, int element) {
//If the node we are evaluating has a bigger value than the one we want to introduce, we study his right son.
if (actualNode->leftSon == NULL) {
insert_to_left_son(actualNode, element);
return true;
}
//If not, we change the node we are studying. Now it's going to be his left son.
actualNode = actualNode->leftSon;
return false;
}
void SplayTree::insert_to_right_son(Node* &actualNode, int element) {
actualNode->rightSon = new Node;
actualNode->rightSon->rightSon = NULL;
actualNode->rightSon->leftSon = NULL;
actualNode->rightSon->father = actualNode;
actualNode->rightSon->value = element;
floation(actualNode->rightSon);
}
void SplayTree::insert_to_left_son(Node* &actualNode, int element) {
actualNode->leftSon = new Node;
actualNode->leftSon->rightSon = NULL;
actualNode->leftSon->leftSon = NULL;
actualNode->leftSon->father = actualNode;
actualNode->leftSon->value = element;
floation(actualNode->leftSon);
}
void SplayTree::updateParents(Node* &n, Node* &aux) {
aux = n->father;
n->father = n->father->father;
aux->father = n;
}
void SplayTree::updateGrandpa(Node* &n, Node* &aux) {
if (n->father != NULL) {
if (n->father->rightSon == aux) {
//If the father was grandpa's right son...
n->father->rightSon = n;
}
else {
//If the father was grandpa's left son...
n->father->leftSon = n;
}
}
}
/*
Represents an L rotation.
Described here: http://www.ics.uci.edu/~dan/class/165/notes/splay.html
*/
void SplayTree::L(Node* n) {
Node* aux;
updateParents(n, aux);
aux->rightSon = n->leftSon;
if (n->leftSon != NULL) {
aux->rightSon->father = aux;
}
n->leftSon = aux;
updateGrandpa(n, aux);
}
/*
Represents a R rotation.
Described here: http://www.ics.uci.edu/~dan/class/165/notes/splay.html
*/
void SplayTree::R(Node* n) {
Node* aux;
updateParents(n, aux);
aux->leftSon = n->rightSon;
if (aux->leftSon != NULL) {
aux->leftSon->father = aux;
}
n->rightSon = aux;
updateGrandpa(n, aux);
}
bool SplayTree::remove_node(Node* &actualNode) {
floation(actualNode);
//If it doesn't have a left son.
if (root->leftSon == NULL) {
if (root->rightSon != NULL) root->rightSon->father = NULL;
root = root->rightSon;
return true;
}
//If it has a left son, we look for the node that's more to the right from his right son.
get_biggest(actualNode);
//Once this finished, actualNode is going to point to a node that it hasn't a right son (it's going to be the bigger from the original actualNode's left son).
//Root's right son, if it existed, it's going to be new actualNode's right son.
if (root->rightSon != NULL) {
actualNode->rightSon = root->rightSon;
root->rightSon->father = actualNode;
}
//Root's left son will be the new root.
root = root->leftSon;
root->father = NULL;
return true;
}
bool SplayTree::insert_if_empty(int element) {
//We create a node in the root. We put the correct values in every field ( references=NULL and value=element).
root = new Node;
root->rightSon = NULL;
root->leftSon = NULL;
root->father = NULL;
root->value = element;
//There's no need to fload, because the tree has only one node.
//Everything worked correctly, so we return true.
return true;
}
bool SplayTree::insert_if_not_empty(int element) {
/*Pointer we are going to use to move through the tree. Points to the node we are studying every moment.
It's going to start pointing to the root.*/
Node* actualNode;
actualNode = root;
//This loop won't stop until a return statement is executed.
while (true) {
if (actualNode->value == element) {
//Insertion didn't work correctly, we've found the element that we are trying to insert. We return false.
floation(actualNode);
return false;
}
else if (actualNode->value < element) {
//If the node we are evaluating has a lower value than the one we want to introduce, we study his right son.
if (study_right_son(actualNode, element)) return true;
}
else {
//If the node we are evaluating has a bigger value than the one we want to introduce, we study his left son.
if (study_left_son(actualNode, element)) return true;
}
}
}
bool SplayTree::rotate_if_father(Node* &actualNode) {
//If it's father's right son.
if (actualNode->father->rightSon == actualNode) {
L(actualNode);
root = actualNode;
//After the rotation, we've finished.
return true;
}
//If it's father's left son.
else if (actualNode->father->leftSon == actualNode) {
R(actualNode);
root = actualNode;
//After the rotation, we've finished.
return true;
}
return false;
}
void SplayTree::rotate_if_grandpa(Node* &actualNode) {
//We use it to avoid the entrance to both ifs.
bool entered = false;
//If it has a grandpa and his father is his grandpa's left son.
if (actualNode->father->father->leftSon != NULL) {
//If actualNode is the left son from the grandpa's left son...
if (actualNode->father->father->leftSon->leftSon == actualNode) {
R(actualNode->father);
R(actualNode);
entered = true;
}
//If actualNode is the right son from grandpa's left son.
else if (actualNode->father->father->leftSon->rightSon == actualNode) {
L(actualNode);
R(actualNode);
entered = true;
}
}
//If it has a grandpa and his father is his grandpa's right son.
if (!entered && actualNode->father->father->rightSon != NULL) {
//If actualNode is the right son from grandpa's right son.
if (actualNode->father->father->rightSon->rightSon == actualNode) {
L(actualNode->father);
L(actualNode);
}
//If actualNode is the left son from grandpa's right son.
else if (actualNode->father->father->rightSon->leftSon == actualNode) {
R(actualNode);
L(actualNode);
}
}
}
void SplayTree::floation(Node* n) {
//Pointer we are going to use to move through the tree. Points to the node we are studying every moment
Node* actualNode = n;
//This loop won't stop until a return statement is executed.
while (true) {
//If a node has a grandpa, we will float differently ( consult link above).
if (actualNode->father != NULL && actualNode->father->father != NULL) {
rotate_if_grandpa(actualNode);
}
//If it has a father but not a grandpa...
else if (actualNode->father != NULL) {
if (rotate_if_father(actualNode)) break;
}
else {
//If it doesn't have father, it means we've finished.
root = actualNode;
break;
}
}
}
bool SplayTree::remove_if_not_empty(int elem) {
Node* actualNode = root;
//This loop won't stop until a return statement is executed.
while (true) {
//If the node we're studying has the value we're looking for, we proceed to remove the element transforming the subtree.
if (actualNode->value == elem) {
return remove_node(actualNode);
}
else if (actualNode->value < elem) {
//If it doesn't have a right son, we won't find what we are looking for.
if (actualNode->rightSon == NULL) {
//If we don't find the node we are trying to remove, we only floate the last node visited.
floation(actualNode);
return false;
}
else {
//If it has a right son, the next node we're going to study it's going to be his right son.
actualNode = actualNode->rightSon;
}
}
else {
//If it doesn't have a left son, we won't find what we are looking for.
if (actualNode->leftSon == NULL) {
//If we don't find the node we are trying to remove, we only floate the last node visited.
floation(actualNode);
return false;
}
else {
//If it has a left son, the next node we're going to study it's going to be his left son.
actualNode = actualNode->leftSon;
}
}
}
}
bool SplayTree::search_if_not_empty(int elem) {
Node* actualNode = root;
//This loop won't stop until a return statement is executed.
while (true) {
//If the node we're studying has the value we're looking for, we stop.
if (actualNode->value == elem) {
floation(actualNode);
//We've found what we were looking for, we return true.
return true;
}
else if (actualNode->value < elem) {
if (actualNode->rightSon == NULL) {
//If it doesn't have a right son, we won't find anything.
floation(actualNode);
return false;
}
else {
//If it has a right son, the next node we're going to study it's going to be his right son.
actualNode = actualNode->rightSon;
}
}
else {
if (actualNode->leftSon == NULL) {
//If it doesn't have a left son, we won't find anything.
floation(actualNode);
return false;
}
else {
//If it has a left son, the next node we're going to study it's going to be his left son.
actualNode = actualNode->leftSon;
}
}
}
}