2019-11-02 | 数据结构 | UNLOCK

斐波那契堆(简单版)

这个只有搞priority_queue的插入和删除,别的没有

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
#ifndef __SET_H__
#define __SET_H__
#include <cstring>
#include <cassert>
typedef size_t size_type;

template<class value_type>
class Set {
private:
const bool RED=1;
const bool BLACK=0;
size_type number;
class Node {
public:
value_type key;
Node *left, *right, *leftList, *rightList;
bool color;
Node(value_type Key, bool Color){
key = Key;
color = Color;
left = nullptr;
right = nullptr;
leftList = this;
rightList = this;
}
};

Node * root;
Node * rotateLeft(Node *h);
Node * rotateRight(Node *h);
Node * colorFlip(Node *h);
Node * insert(Node *h, Node *parent, int direction, value_type key);
Node * insertToLeft(Node *h, Node *parent);
Node * insertToRight(Node *h, Node *parent);
Node * deleteList(Node *h);
Node * fixUp(Node *h);
Node * moveRedRight(Node *h);
Node * moveRedLeft(Node *h);
value_type getMin(Node *h);
Node * deleteIn(Node *h, value_type key);
Node * deleteMin(Node *h);
size_type ifFind(value_type key);
bool isRed(Node *x);
public:
class iterator {
public:
Node *toNode;
iterator() {
toNode = nullptr;
}
iterator(Node *node) {
toNode = node;
}
value_type & operator *() {
return toNode->key;
}
void operator++(int) {
toNode = toNode->rightList;
}
void operator--(int) {
toNode = toNode->leftList;
}
iterator & operator=(const iterator & b) {
toNode = b.toNode;
return *this;
}
};

void dedddbug(Node *h);
Set() {
number = 0;
root = nullptr;
}
bool empty() const;
size_type size() const;
void clear();
void insert(const value_type& value);
size_type erase(const value_type& value);
size_type count(const value_type& value);
iterator find(const value_type& value);
iterator lower_bound(const value_type& value);
iterator upper_bound(const value_type& value);
};

template<class value_type>
bool Set<value_type>::isRed(Node *x) {
if(x==nullptr)
return false;
return x->color==RED;
}

template<class value_type>
typename Set<value_type>::Node * Set<value_type>::rotateLeft(Node *h) {
Node *x=h->right;
h->right = x->left;
x->left = h;
x->color = x->left->color;
x->left->color = RED;

return x;
}

template<class value_type>
typename Set<value_type>::Node * Set<value_type>::rotateRight(Node *h) {
Node *x=h->left;
h->left = x->right;
x->right = h;
x->color = x->right->color;
x->right->color = RED;
return x;
}

template<class value_type>
typename Set<value_type>::Node * Set<value_type>::colorFlip(Node *h) {

h->color = !h->color;
if(h->left!=nullptr)h->left->color = !h->left->color;
if(h->right!=nullptr)h->right->color = !h->right->color;
return h;
}

template<class value_type>
typename Set<value_type>::Node * Set<value_type>::fixUp(Node *h) {
if(isRed(h->right))
h = rotateLeft(h);
if(isRed(h->left) && isRed(h->left->left))
h = rotateRight(h);
if(isRed(h->left) && isRed(h->right))
h = colorFlip(h);

return h;
}

template<class value_type>
typename Set<value_type>::Node * Set<value_type>::insertToLeft(Node *h, Node *parent) {
parent->leftList->rightList = h;
h->leftList = parent->leftList;
parent->leftList = h;
h->rightList = parent;
return h;
}

template<class value_type>
typename Set<value_type>::Node * Set<value_type>::insertToRight(Node *h, Node *parent) {
parent->rightList->leftList = h;
h->rightList = parent->rightList;
parent->rightList = h;
h->leftList = parent;
return h;
}

template<class value_type>
typename Set<value_type>::Node * Set<value_type>::insert(Node *h, Node *parent, int direction, value_type key) {
if(h==nullptr) {
number++;
Node *x=new Node(key, RED);
if(parent!=nullptr) {
if(direction==0) x = insertToLeft(x, parent);
else x = insertToRight(x, parent);
}
return x;
}
if(key==h->key) ;
else if(key<h->key)
h->left = insert(h->left, h, 0, key);
else
h->right = insert(h->right, h, 1, key);
if(isRed(h->right) && !isRed(h->left))
h = rotateLeft(h);
if(h->left!=nullptr && isRed(h->left) && isRed(h->left->left))
h = rotateRight(h);
if(isRed(h->left) && isRed(h->right))
h = colorFlip(h);

return h;
}

template<class value_type>
typename Set<value_type>::Node * Set<value_type>::moveRedRight(Node *h) {
h = colorFlip(h);
if(h->left!=nullptr && isRed(h->left->left)) {
h = rotateRight(h);
h = colorFlip(h);
}
return h;
}

template<class value_type>
typename Set<value_type>::Node * Set<value_type>::moveRedLeft(Node *h) {
h = colorFlip(h);
if(h->right!=nullptr && isRed(h->right->left)) {
h->right = rotateRight(h->right);
h = rotateLeft(h);
colorFlip(h);
}
return h;
}

template<class value_type>
value_type Set<value_type>::getMin(Node *h) {
Node *x=h;
while(x->left!=nullptr) x = x->left;
return x->key;
}

template<class value_type>
typename Set<value_type>::Node * Set<value_type>::deleteList(Node *h) {
h->rightList->leftList = h->leftList;
h->leftList->rightList = h->rightList;
h->leftList = h;
h->rightList = h;
return h;
}

template<class value_type>
typename Set<value_type>::Node * Set<value_type>::deleteMin(Node *h) {
if(h==nullptr) return nullptr;
if(h->left==nullptr) {
h = deleteList(h);
delete h;
number--;
return nullptr;
}
if(!isRed(h->left) && !isRed(h->left->left))
h = moveRedLeft(h);
h->left = deleteMin(h->left);

return h=fixUp(h);
}

template<class value_type>
typename Set<value_type>::Node * Set<value_type>::deleteIn(Node *h, value_type key) {

if(h==nullptr) return nullptr;
if(key<h->key) {
if(h->left!=nullptr && !isRed(h->left) && !isRed(h->left->left))
h = moveRedLeft(h);
h->left = deleteIn(h->left, key);
}
else {
if(isRed(h->left)) {
h = rotateRight(h);}
if(key==h->key && h->right==nullptr) {
h = deleteList(h);
delete h;
number--;
return nullptr;
}

if(h->right!=nullptr && !isRed(h->right) &&!isRed(h->right->left))
h = moveRedRight(h);
if(key==h->key) {
h->key = getMin(h->right);
h->right = deleteMin(h->right);
}
else h->right = deleteIn(h->right, key);

}

return h=fixUp(h);
}

template<class value_type>
size_type Set<value_type>::ifFind(value_type key) {
Node *h=root;
while(h!=nullptr) {
if(key==h->key) return 1;
else if(key<h->key) h = h->left;
else h = h->right;
}
return 0;
}

template<class value_type>
void Set<value_type>::dedddbug(Node *h) {
if(h==nullptr) return;
printf("@%14p, left %14p, right %14p, color %d, value %d\n", h, h->left, h->right, h->color, h->key);
dedddbug(h->left);
dedddbug(h->right);
}

template<class value_type>
void Set<value_type>::insert(const value_type& value) {
root = insert(root, nullptr, 0, value);
root->color = BLACK;
}

template<class value_type>
bool Set<value_type>::empty() const{
return number==0;
}

template<class value_type>
size_type Set<value_type>::size() const{
return number;
}

template<class value_type>
void Set<value_type>::clear() {
while(root!=nullptr) {
root = deleteIn(root, root->key);
}
root = nullptr;
}


template<class value_type>
size_type Set<value_type>::erase(const value_type& value) {
size_type tmp=number;
if(root!=nullptr) {
if(!isRed(root->left) && !isRed(root->right)) root->color = RED;
root = deleteIn(root, value);
if(root!=nullptr)
root->color = BLACK;
}
return tmp-number;
}

template<class value_type>
size_type Set<value_type>::count(const value_type& value) {
return ifFind(value);
}

template<class value_type>
typename Set<value_type>::iterator Set<value_type>::find(const value_type& value) {
Node *h=root;
while(h!=nullptr) {
if(value==h->key) return iterator(h);
else if(value<h->key) h = h->left;
else h = h->right;
}
return iterator(nullptr);
}

template<class value_type>
typename Set<value_type>::iterator Set<value_type>::lower_bound(const value_type& value) {
Node *h=root, *x=nullptr;
while(h!=nullptr) {
if(h->key>=value && (x==nullptr || h->key<x->key)) x = h;
if(value<h->key) h = h->left;
else if(value==h->key) break;
else h = h->right;
}
return iterator(x);
}

template<class value_type>
typename Set<value_type>::iterator Set<value_type>::upper_bound(const value_type& value) {
Node *h=root, *x=nullptr;
while(h!=nullptr) {
if(h->key>value && (x==nullptr || h->key<x->key)) x = h;
if(value<h->key) h = h->left;
else h = h->right;
}
return iterator(x);
}

#endif