-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfine_grained_avl.cpp
405 lines (342 loc) · 8.1 KB
/
fine_grained_avl.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
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
#include <iostream>
#include <pthread.h>
#include <limits>
using namespace std;
class Node_L {
public:
int key;
Node_L *left;
Node_L *right;
Node_L *parent;
bool removed;
int left_height, right_height;
Node_L *succ, *pred;
pthread_mutex_t tree_lock, succ_lock;
};
Node_L* root;
void tree_lock(Node_L *N)
{
pthread_mutex_lock(&N->tree_lock);
//printf("tree locked node %d \n", N->key);
}
void tree_unlock(Node_L *N)
{
//printf("tree unlocked node %d \n", N->key);
pthread_mutex_unlock(&N->tree_lock);
}
void succ_lock(Node_L *N)
{
pthread_mutex_lock(&N->succ_lock);
//printf("succ locked node %d \n", N->key);
}
void succ_unlock(Node_L *N)
{
//printf("succ unlocked node %d \n", N->key);
pthread_mutex_unlock(&N->succ_lock);
}
Node_L* search(int k)
{
if (root == NULL) return NULL;
Node_L *curr = root;
Node_L *curr_child = root;
int curr_key = root->key;
while (true)
{
curr_key = curr->key;
if (curr_key == k) return curr;
curr_child = curr_key < k ? curr->right : curr->left;
if (curr_child == NULL) return curr;
curr = curr_child;
}
return NULL; //absurd
}
bool contains(int k)
{
Node_L *res = search(k);
while (res->key > k)
{
res = res->pred;
}
while (res->key < k)
{
res = res->succ;
}
bool ans = (res->key == k && !res->removed);
return ans;
}
int get_max (int x, int y) {
return (x > y) ? x : y;
}
int get_bfactor(Node_L *N) {
if (N == NULL) return 0;
return N->left_height - N->right_height;
}
bool fix_height(Node_L *N) {
tree_lock(N);
int old_left = N->left_height;
int old_right = N->right_height;
if (N->left)
N->left_height = get_max(N->left->left_height, N->left->right_height) + 1;
else N->left_height = 0;
if (N->right)
N->right_height = get_max(N->right->left_height, N->right->right_height) + 1;
else N->right_height = 0;
bool ans = (old_left != N->left_height) || (old_right != N->right_height);
tree_unlock(N);
return ans;
//N->height = get_max(N->left_height, N->right_height) + 1;
}
void fix_height_no_lock(Node_L *N) {
if (N->left)
N->left_height = get_max(N->left->left_height, N->left->right_height) + 1;
else N->left_height = 0;
if (N->right)
N->right_height = get_max(N->right->left_height, N->right->right_height) + 1;
else N->right_height = 0;
}
void replace(Node_L *P, Node_L *O, Node_L *C)
{
//if (C) printf("Node %d's original child Node %d is now replaced by Node %d \n", P->key, O->key, C->key);
if (P->left == O) P->left = C;
else P->right = C;
if (C) C->parent = P;
}
Node_L* create_node(int k) {
Node_L *N = new Node_L();
N->key = k;
N->left = NULL;
N->right = NULL;
N->parent = NULL;
N->removed = false;
pthread_mutex_init(&N->succ_lock, NULL);
pthread_mutex_init(&N->tree_lock, NULL);
N->left_height = 0;
N->right_height = 0;
return N;
}
void init_tree()
{
root = create_node(numeric_limits<int>::max());
root->pred = create_node(numeric_limits<int>::min());
(root->pred)->succ = root;
}
Node_L* rotate_right(Node_L *N) {
if(!N || !N->left) return NULL;
tree_lock(N);
tree_lock(N->parent);
if (N->left) tree_lock(N->left);
if (N->left->right) tree_lock(N->left->right);
Node_L *L = N->left;
N->left = L->right;
if (L->right) L->right->parent = N;
L->right = N;
Node_L *old_parent = N->parent;
replace(N->parent, N, L);
N->parent = L;
N->left_height = L->right_height;
L->right_height = get_max(N->left_height, N->right_height) + 1;
if (L) tree_unlock(L);
if (N->left) tree_unlock(N->left);
tree_unlock(old_parent);
tree_unlock(N);
return L;
}
Node_L* rotate_left(Node_L *N) {
if(!N || !N->right) return NULL;
tree_lock(N);
tree_lock(N->parent);
if (N->right) tree_lock(N->right);
if (N->right->left) tree_lock(N->right->left);
Node_L *R = N->right;
N->right = R->left;
if (R->left) R->left->parent = N;
R->left = N;
N->right_height = R->left_height;
R->left_height = get_max(N->left_height, N->right_height) + 1;
Node_L *old_parent = N->parent;
replace(N->parent, N, R);
N->parent = R;
if (R) tree_unlock(R);
if (N->right) tree_unlock(N->right);
tree_unlock(old_parent);
tree_unlock(N);
return R;
}
void balance(Node_L *N) {
if (N == NULL) return;
if(get_bfactor(N) >= 2)
{
//printf("right rotation on key %d \n", N->key);
if(get_bfactor(N->left) < 0)
N->left = rotate_left(N->left);
fix_height(N);
N = rotate_right(N);
}
if(get_bfactor(N) <= -2)
{
//printf("left rotation on key %d \n", N->key);
if(get_bfactor(N->right) > 0)
N->right = rotate_right(N->right);
fix_height(N);
N = rotate_left(N);
}
}
Node_L* find_parent(Node_L *P, Node_L *S, Node_L *K)
{
Node_L* curr = (K == S) ? K : P;
while (true)
{
tree_lock(curr);
if (curr == P)
{
if (curr->right == NULL) return curr;
tree_unlock(curr);
curr = S;
}
else
{
if (curr->left == NULL) return curr;
tree_unlock(curr);
curr = P;
}
}
return NULL; //absurd
}
bool insert(int k)
{
//printf("inserting node with key %d \n", k);
while (true)
{
Node_L *N = search(k);
Node_L *L = (N->key > k && N->pred) ? N->pred : N;
succ_lock(L);
Node_L *R = L->succ; //L and R are the lower and upper bounds for k
if (L->key < k <= R->key && !L->removed)
{
if (R->key == k)
{
succ_unlock(L);
return false;
}
Node_L *K = create_node(k);
Node_L *P = find_parent(L, R, N);
tree_unlock(P); //just in case it returned before unlock-- don't want to unlock before in case of change
K->succ = R;
K->pred = L;
R->pred = K;
L->succ = K;
succ_unlock(L);
//begin reparenting
K->parent = P;
if (k < P->key)
{
P->left = K;
P->left_height = 1;
}
else if (k > P->key)
{
P->right = K;
P->right_height = 1;
}
//end reparenting
balance(K);
while (P != root)
{
Node_L *PP = P->parent;
if (P->parent == PP && !PP->removed)
{
fix_height(P);
fix_height(PP);
balance(P);
P = PP;
tree_unlock(PP);
}
}
//balance(K);
//fix_height(root);
return true;
}
succ_unlock(L);
}
return false; //absurd
}
void clone(Node_L *original, Node_L *copy)
{
copy->key = original->key;
copy->left = original->left;
copy->right = original->right;
copy->left_height = original->left_height;
copy->right_height = original->right_height;
}
bool remove_tree(Node_L *N)
{
Node_L *parent;
Node_L *original_parent = N->parent;
tree_lock(N);
tree_lock(N->parent);
if ((!N->left || N->left->removed) || (!N->right || N->right->removed))
{
Node_L *original_left = N->left;
Node_L *original_right = N->right;
if (original_left) tree_lock(original_left);
if (original_right) tree_lock(original_right);
parent = N->parent;
if (N->left) N->left->parent = parent;
if (N->right) N->right->parent = parent;
Node_L* child = N->left ? N->left : N->right;
replace(parent, N, child);
if (original_left) tree_unlock(original_left);
if (original_right) tree_unlock(original_right);
}
else
{
Node_L *succ = N->succ;
Node_L *temp = NULL;
if (succ->parent != N) tree_lock(succ->parent);
tree_lock(succ);
if (succ->right)
{
tree_lock(succ->right);
temp = succ->right;
}
Node_L *child = succ->right;
parent = succ->parent;
replace(parent, succ, child);
succ->left = N->left;
succ->right = N->right;
succ->left_height = N->left_height;
succ->right_height= N->right_height;
N->left->parent = succ;
if (N->right) N->right->parent = succ;
replace(N->parent, N, succ);
if (parent == N) parent = succ;
else tree_unlock(succ);
tree_unlock(N->parent);
tree_unlock(parent);
if (temp) tree_unlock(temp);
}
tree_unlock(N);
tree_unlock(original_parent);
balance(parent);
return true;
}
bool remove (int k)
{
//printf("removing key %d \n", k);
Node_L *N = search(k);
while (N->key > k) N = N->pred;
while (N->key < k) N = N->succ;
if (N->key != k) return false;
Node_L *P = N->pred;
succ_lock(P);
Node_L *S = P->succ;
succ_lock(S);
S->removed = true;
Node_L *S_succ = S->succ;
S_succ->pred = P;
P->succ = S_succ;
if (!remove_tree(S)) return false;
succ_unlock(S);
succ_unlock(P);
return true;
}