-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTreeMap.h
491 lines (457 loc) · 12 KB
/
TreeMap.h
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
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
/** @file */
#ifndef __TREEMAP_H
#define __TREEMAP_H
#include "Utility.h"
#include <cstdlib>
#include <ctime>
/**
* A map is a sequence of (key, value) entries that provides fast retrieval
* based on the key. At most one value is held for each key.
*
* TreeMap is the balanced-tree implementation of map. The iterators must
* iterate through the map in the natural order (operator<) of the key.
*/
template<class K, class V> class TreeMap {
private:
static const int MAXN = 1000000000;
struct node{
Entry<K, V> data;
int aux;
node *lf, *rh;
node() {
srand(time(NULL));
aux = rand() % MAXN;
lf = rh = NULL;
}
node(const K& x, const V& y) : data(x, y){
srand(time(NULL));
aux = rand() % MAXN;
lf = rh = NULL;
}
node(const Entry<K, V>& x) : data(x.key, x.value){
srand(time(NULL));
aux = rand() % MAXN;
lf = rh = NULL;
}
node(const node* const y) {
data = y->data;
lf = y->lf;
rh = y->rh;
aux = y->aux;
}
};
node *Root;
int siz;
public:
void RotateL(node* &x) {
node *y = x->rh;
x->rh = y->lf;
y->lf = x;
x = y;
}
void RotateR(node* &x) {
node *y = x->lf;
x->lf = y->rh;
y->rh = x;
x = y;
}
void Insert(node* &nd, const Entry<K, V>& e) {
if (nd == NULL) {
nd = new node(e);
++siz;
return;
}
if (e.key < nd->data.key) {
Insert(nd->lf, e);
if (nd->lf->aux < nd->aux) RotateR(nd);
}
else {
Insert(nd->rh, e);
if (nd->rh->aux < nd->aux) RotateL(nd);
}
}
void Delete(node* &nd, const K& key) {
if (nd == NULL) return;
if (nd->data.key == key) {
if (nd->lf == NULL || nd->rh == NULL) {
if (nd->lf == NULL) {
--siz;
node *tmp = nd;
nd = nd->rh;
delete tmp;
}
else {
--siz;
node *tmp = nd;
nd = nd->lf;
delete tmp;
}
}
else {
if (nd->lf->aux < nd->rh->aux) {
RotateR(nd);
Delete(nd->rh, key);
}
else {
RotateL(nd);
Delete(nd->lf, key);
}
}
}
else {
if (key < nd->data.key) Delete(nd->lf, key);
else Delete(nd->rh, key);
}
}
class ConstIterator {
const TreeMap *parent;
Entry<K, V> *value;
bool flag;
public:
ConstIterator(const TreeMap* const x) {
if (x->Root == NULL) throw;
flag = false;
parent = x;
node *p = x->Root;
while (p->lf != NULL) {
p = p->lf;
}
value = new Entry<K, V>(p->data);
}
/**
* Returns true if the iteration has more elements.
* Amortized O(1).
*/
bool hasNext() {
if (!flag) return true;
if (value->key < parent->lastKey()) return true;
return false;
}
/**
* Returns a const reference to the next element in the iteration.
* Amortized O(1).
* @throw ElementNotExist
*/
const Entry<K, V>& next() {
if (!flag) {
flag = true;
return *value;
}
node *p = parent->getRoot();
node *tag = p;
while (p->data.key != value->key) {
tag = p;
if (value->key < p->data.key) p = p->lf;
else p = p->rh;
}
if (p->rh != NULL) {
p = p->rh;
while (p->lf != NULL) {
p = p->lf;
}
}
if (p->data.key != value->key) tag = p;
delete value;
value = new Entry<K, V>(tag->data);
return *value;
}
};
class Iterator {
TreeMap *parent;
Entry<K, V> *value;
bool flag;
public:
Iterator(TreeMap* const x) {
parent = x;
node *p = x->getRoot();
while (p->lf != NULL) {
p = p->lf;
}
value = new Entry<K, V>(p->data);
flag = false;
}
/**
* Returns true if the iteration has more elements.
* Amortized O(1).
*/
bool hasNext() {
if (!flag) return true;
if (value->key < parent->lastKey()) return true;
return false;
}
/**
* Returns a reference to the next element in the iteration.
* Amortized O(1).
* @throw ElementNotExist
*/
Entry<K, V>& next() {
if (!flag) {
flag = true;
return *value;
}
node *p = parent->getRoot();
node *tag = p;
while (p->data.key != value->key) {
if (value->key < p->data.key) {
p = p->lf;
tag = p;
}
else p = p->rh;
}
if (p->rh != NULL) {
p = p->rh;
while (p->lf != NULL) {
p = p->lf;
}
}
if (value->key < p->data.key) tag = p;
delete value;
value = new Entry<K, V>(tag->data);
return *value;
}
/**
* Removes from the underlying collection the last element
* returned by the iterator
* Amortized O(1).
* @throw ElementNotExist
*/
void remove() {
node *p = parent->getRoot();
node *tag = p;
while (p->data.key != value->key) {
tag = p;
if (value->key < p->data.key) p = p->lf;
else p = p->rh;
}
if (p->lf != NULL) {
p = p->lf;
while (p->rh != NULL) {
p = p->rh;
}
}
if (p->data.key < value->key) tag = p;
K tmp = value->key;
delete value;
value = new Entry<K, V>(tag->data);
parent->remove(tmp);
}
~Iterator() {
delete value;
}
};
/**
* Constructs an empty map
*/
TreeMap() {
Root = NULL;
siz = 0;
}
/**
* Copy constructor
*/
TreeMap(const TreeMap &c) {
Root = NULL;
siz = 0;
ConstIterator iter = c.constIterator();
while (iter.hasNext())
put(iter.next());
}
/**
* Destructor
*/
~TreeMap() {
clear();
}
void makeTree(node* &x, node* y){
if (y == NULL) {
x = NULL;
return;
}
x = new node(y);
makeTree(x->lf, y->lf);
makeTree(x->rh, y->rh);
}
/**
* Assignment operator
*/
TreeMap& operator=(const TreeMap &c) {
clear();
ConstIterator iter = c.constIterator();
while (iter.hasNext())
put(iter.next());
return *this;
}
/**
* Constructs a new tree map containing the same mappings as the
* given map
*/
template <class C> TreeMap(const C& c) {
typename C::ConstIterator iter = c.constIterator();
while (iter.hasNext())
put(iter.next());
}
/**
* Returns an iterator over the elements in this map.
* O(1).
*/
Iterator iterator() {
Iterator *tmp = new Iterator(this);
return *tmp;
}
/**
* Returns an const iterator over the elements in this map.
* O(1).
*/
ConstIterator constIterator() const {
ConstIterator *tmp = new ConstIterator(this);
return *tmp;
}
/**
* Removes all of the mappings from this map.
* O(n).
*/
void clear(node *p) {
if (p == NULL) return;
clear(p->lf);
clear(p->rh);
delete p;
}
void clear() {
clear(Root);
siz = 0;
Root = NULL;
}
bool contain(node *p, const K& key) const {
if (p == NULL) return false;
if (p->data.key == key) return true;
if (key < p->data.key) return contain(p->lf, key);
else return contain(p->rh, key);
}
/**
* Returns true if this map contains a mapping for the specified key.
* O(logn).
*/
bool containsKey(const K& key) const {
return contain(Root, key);
}
/**
* Returns true if this map contains a mapping for the specified value.
* O(n).
*/
bool containsValue(const V& value) const {
ConstIterator iter = constIterator();
while (iter.hasNext())
if (iter.next().value == value)
return true;
return false;
}
/**
* Returns a key-value mapping associated with the least key in
* this map.
* O(logn).
* @throw ElementNotExist
*/
const Entry<K, V>& firstEntry() const {
node *tmp = Root;
while (tmp->lf != NULL) {
tmp = tmp->lf;
}
return tmp->data;
}
/**
* Returns the first (lowest) key currently in this map.
* O(logn).
* @throw ElementNotExist
*/
const K& firstKey() const {
return firstEntry().key;
}
V& getValue(node *p, const K& key) const {
if (p == NULL) throw;
if (p->data.key == key) return p->data.value;
if (key < p->data.key) return getValue(p->lf, key);
else return getValue(p->rh, key);
}
/**
* Returns a reference to the value which the specified key is mapped
* O(logn).
* @throw ElementNotExist
*/
V& get(const K& key) {
return getValue(Root, key);
}
/**
* Returns a reference to the value which the specified key is mapped
* O(logn).
* @throw ElementNotExist
*/
const V& get(const K& key) const {
return getValue(Root, key);
}
/**
* Returns a key-value mapping associated with the greatest key
* in this map.
* O(logn).
* @throw ElementNotExist
*/
const Entry<K, V>& lastEntry() const {
if (Root == NULL) throw;
node *tmp = Root;
while (tmp->rh != NULL) {
tmp = tmp->rh;
}
return tmp->data;
}
/**
* Returns the last (highest) key currently in this map.
* O(logn).
* @throw ElementNotExist
*/
const K& lastKey() const {
return lastEntry().key;
}
/**
* Associates the specified value with the specified key in this map.
* Returns the previous value, if not exist, a value returned by the
* default-constructor.
* O(logn).
*/
V put(const K& key, const V& value) {
Entry<K, V> e(key, value);
V tmp;
if (contain(Root, e.key)) {
tmp = get(e.key);
Delete(Root, e.key);
}
Insert(Root, e);
return tmp;
}
V put(const Entry<K, V>& e) {
V tmp;
if (contain(Root, e.key)) {
tmp = get(e.key);
Delete(Root, e.key);
}
Insert(Root, e);
return tmp;
}
/**
* Removes the mapping for this key from this TreeMap if present.
* O(logn).
* @throw ElementNotExist
*/
V remove(const K& key) {
Delete(Root, key);
}
/**
* Returns the number of key-value mappings in this map.
* O(logn).
*/
int size() const {
return siz;
}
node* getRoot() const {
return Root;
}
};
#endif