-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmap.cpp
471 lines (411 loc) · 11.9 KB
/
map.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
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
/** CS 515 Assignment 8
* File: map.cpp
* Name: Xavier McNulty
* Section: 1
* Date: 24 March 2015
* Collaboration:
*/
#include "map.h"
/**
* Default constructor.
*/
template <typename KEY, typename T>
Map<KEY, T>::Map() {
_root = new Elem(KEY(), T(), _root, 0, 0, false);
_size = 0;
}
/**
* Copy constructor.
*
* @param v map being copied
*/
template <typename KEY, typename T>
Map<KEY, T>::Map(const Map<KEY,T> &v){
// if empty tree
if (v._root == v._root->left){
_root = new Elem(KEY(), T(), _root, 0, 0, false);
_size = 0;
} else {
_root = new Elem(KEY(), T(), _root, 0, 0, false);
copyCode(_root->left, v._root->left); // to deep copy the tree without dummy nodes
copyThread(_root, v._root); // to copy the threading; must pass dummy nodes to complete the threads
_size = v._size;
}
}
/**
* Overloaded assignment operator.
*
* @param rhs Map being copied
* @return reference to this
*/
template<typename KEY, typename T>
Map<KEY, T>& Map<KEY,T>::operator=(const Map &rhs) {
if (this != &rhs) {
if (&rhs != 0) {
if(_size != 0)
destructCode(_root->left);
delete _root;
// create a dummy root node
_root = new Elem(KEY(), T(), _root, 0, 0, false);
_size = 0;
copyCode(_root->left, rhs._root->left);
copyThread(_root, rhs._root);
_size = rhs.size();
} else {
if(_size != 0)
destructCode(_root->left);
delete _root;
// create a dummy root node
_root = new Elem(KEY(), T(), _root, 0, 0, false);
_size = 0;
}
}
return * this;
}
/**
* Destructor.
*/
template <typename KEY, typename T>
Map<KEY, T>::~Map() {
if(_size == 0) // empty map.
delete _root;
else {
destructCode(_root->left);
delete _root;
}
}
/**
* Destructs the map tree.
*
* @param p pointer to node being removed
*/
template <typename KEY, typename T>
void Map<KEY, T>::destructCode(Elem *& p) {
// destroy left subtree.
if (p->left) {
destructCode(p->left);
}
// destroy right subtree.
if (!p->rightThread) {
destructCode(p->right);
}
delete p;
}
// construct a key-element map to rethread the new tree
// the map contains all nodes key values and their corresonding elem node address
// for furture lookup in setting up threads
template <typename KEY, typename T>
void Map<KEY, T>::addToMap(Elem* root, map<KEY, Elem*> &keyElemMap){
if (root) {
keyElemMap[root->key] = root;
addToMap(root->left, keyElemMap);
if (!root->rightThread)
addToMap(root->right, keyElemMap);
}
}
/**
* Common copyCode
*/
template <typename KEY, typename T>
void Map<KEY,T>::copyCode(Elem* &newRoot, Elem* origRoot){
if (origRoot == 0)
newRoot = 0;
else{
newRoot = new Elem(origRoot->key, origRoot->data, 0, 0, origRoot->height, origRoot->rightThread);
newRoot->key = origRoot->key;
newRoot->data = origRoot->data;
newRoot->height = origRoot->height;
newRoot->rightThread = origRoot->rightThread;
copyCode(newRoot->left, origRoot->left);
if (!origRoot->rightThread)
copyCode(newRoot->right, origRoot->right);
}
}
// copy threads for the tree
template <typename KEY, typename T>
void Map<KEY, T>::copyThread(Elem* &newRoot, Elem* origRoot){
// construct the key-element map for new and orig tree
map<KEY, Elem*> newKeyElemMap;
map<KEY, Elem*> origKeyElemMap;
addToMap(newRoot->left, newKeyElemMap);
addToMap(origRoot->left, origKeyElemMap);
// start at the last element in the tree, which threads to root
typename std::map<KEY, Elem*>::reverse_iterator it = origKeyElemMap.rbegin();
newKeyElemMap[it->first] -> rightThread = true;
newKeyElemMap[it->first] -> right = newRoot;
// then thread the rest of the tree backwardly
it++;
while(it != origKeyElemMap.rend()){
if (it->second->rightThread){
newKeyElemMap[it->first] -> rightThread = true;
newKeyElemMap[it->first] -> right = newKeyElemMap[ origKeyElemMap[it->first]->right->key ];
}
it++;
}
}
/**
* Overloaded [] operator.
*
* @param k key being searched for
* @return reference to data that belongs to k
*/
template<typename KEY, typename T>
T& Map<KEY, T>::operator[](KEY k) {
Iterator it = find(k);
// insert element if not in the map.
if (it == end()) {
insert(k, T());
it = find(k);
}
return it->data;
}
/**
* find min
*/
template<typename KEY, typename T>
typename Map<KEY, T>::Elem* Map<KEY, T>::findMin(Map::Elem *node) {
if (node == 0)
return node;
while (node->left) // move to the leftmost node
node = node->left;
return node;
}
template<typename KEY, typename T>
int Map<KEY, T>::size() const {
return _size;
}
/**
* Find an element in the map.
*
* @param key key of element being searched for
* @return Iterator to found element
*/
template <typename KEY, typename T>
typename Map<KEY, T>::Iterator Map<KEY, T>::find(KEY key) const {
Iterator it = begin();
while(it != end() && it->key != key)
it ++;
return it;
}
/**
* Insert into the tree.
*
* @param k key of new element
* @param t data of new element
* @return true if the data element was inserted
*/
template <typename KEY, typename T>
bool Map<KEY, T>::insert(KEY k, T t) {
if (_size == 0) { // insert into an empty set.
_root->left = new Elem(k, t, 0, _root);
_size ++;
return true;
} else if(find(k) != end()) {
return false;
} else { // use helper method.
insert(k, t, _root->left, _root);
return true;
}
}
/**
* Insert helper method.
*
* @param k key for new element
* @param t data for new element
* @param cur curent pointer
* @param lastLeft for rightThread
* @return true if element inserted
*/
template <typename KEY, typename T>
void Map<KEY, T>::insert(KEY k, T t, Elem *& cur, Elem * lastLeft) {
Elem * node;
if(cur->left && k < cur->key) { // insert at left.
node = cur->left;
insert(k, t, cur->left, cur);
} else if(!cur->rightThread && k > cur->key) { // insert at right.
node = cur->right;
insert(k, t, cur->right, lastLeft);
} else if (!cur->left && k < cur->key) { // insert at the left.
cur->left = new Elem(k, t, 0, cur);
node = cur->left;
_size ++;
} else { // insert at the right.
cur->rightThread = false;
cur->right = new Elem(k, t, 0, lastLeft);
node = cur->right;
_size ++;
}
if (height(cur->left, false) - height(cur->right, cur->rightThread) == 2) { // calculate load factor
if(k < cur->left->key) // outside case
rotateRight(cur);
else // inside case
doubleRotateRight(cur);
}
if (height(cur->left, false) - height(cur->right, cur->rightThread) == -2) { // calculate load factor
if (k > cur->right->key) // outside case
rotateLeft(cur);
else // inside case
doubleRotateLeft(cur);
}
cur->height = max(height(cur->left, false), height(cur->right, cur->rightThread)) + 1; // update height
}
template <typename KEY, typename T>
int Map<KEY, T>::height(Elem *node, bool rThread) {
if (node == 0 || rThread) {
return -1;
} else {
return node->height;
}
}
// output the structure of tree. The tree is output as "lying down"
// output each node's key, value and its tree height
template <typename KEY, typename T>
void Map<KEY, T>::printTree(ostream& out, int level, Elem *p) const{
int i;
if (p) {
if (p->right && !p->rightThread)
printTree(out, level+1,p->right);
for(i=0;i<level;i++) {
out << "\t";
}
out << p->key << " " << p->data << "(" << p->height << ")" << '\n';
printTree(out, level+1,p->left);
}
}
// outputs information in tree in inorder traversal order
template <typename KEY, typename T>
ostream& Map<KEY, T>::dump(ostream& out) const{
if ( _size == 0) {// tree empty
return out;
}
printTree(out, 0, _root->left); // print tree structure
return out;
}
// outputs using overloaded << operator
template<typename KEY, typename T>
ostream& operator<< (ostream& out, const Map<KEY, T>& v){
v.dump(out);
return out;
}
/*
* Map iterator implementation.
*/
template <typename KEY, typename T>
typename Map<KEY, T>::Iterator Map<KEY, T>::begin() const { // return the left most (smallest) tree node
// fill in here
if(_size == 0) // empty tree
return Iterator(_root);
Elem * cur = _root->left;
while(cur->left)
cur = cur->left;
return Iterator(cur);
}
/**
* Rotate left method.
*
* @param node node being rotated about
*/
template <typename KEY, typename T>
void Map<KEY, T>::rotateLeft(Elem *& node) {
//cout << "rotating left on " << node->key << endl;
Elem * tmp = node->right;
if(tmp->left)
node->right = tmp->left;
else {
node->rightThread = true;
node->right = tmp;
}
tmp->left = node;
node = tmp;
node->left->height = max(height(node->left->left, false), height(node->left->right, node->left->rightThread)) +1;
}
/**
* Rotate right method.
*
* @param node node being rotated about
*/
template <typename KEY, typename T>
void Map<KEY, T>::rotateRight(Elem *& node) {
//cout << "rotating right "<< node->key << endl;
Elem * tmp = node->left;
if(!tmp->rightThread)
node->left = tmp->right;
else
node->left = 0;
tmp->right = node;
tmp->rightThread = false;
node = tmp;
if(!node->rightThread)
node->right->height = max(height(node->right->left, false), height(node->right->right, node->right->rightThread)) +1;
else
node->right->height = -1;
}
/**
* Double rotate right.
*/
template <typename KEY, typename T>
void Map<KEY, T>::doubleRotateRight(Elem *& node) {
//cout << "Double rotate right" << endl;
rotateLeft(node->left);
rotateRight(node);
}
/**
* Double rotate left.
*/
template <typename KEY, typename T>
void Map<KEY, T>::doubleRotateLeft(Elem *& node) {
//cout << "Double rotate left" << endl;
rotateRight(node->right);
rotateLeft(node);
}
//------------------------Iterator methods--------------------------
template <typename KEY, typename T>
typename Map<KEY, T>::Iterator Map<KEY, T>::end() const { // return the dummy root node
// fill in here
return Iterator(_root);
}
template <typename KEY, typename T>
typename Map<KEY, T>::Iterator Map<KEY, T>::Iterator::operator++(int){
if (_cur->right == 0) {
return *this;
}
// fill in here
if(_cur->rightThread)
_cur = _cur->right;
else {
_cur = _cur->right;
while(_cur->left)
_cur = _cur->left;
}
return *this;
}
template <typename KEY, typename T>
typename Map<KEY, T>::Elem& Map<KEY, T>::Iterator::operator*(){
// fill in here
return *_cur;
}
template <typename KEY, typename T>
typename Map<KEY, T>::Elem* Map<KEY, T>::Iterator::operator->(){
// fill in here
return _cur;
}
/**
* Iterator comparison.
*
* @param it Iterator being compared to this
* @return true if the Iterators are equal
*/
template <typename KEY, typename T>
bool Map<KEY, T>::Iterator::operator==(Iterator it) {
return (_cur == it.operator->());
}
/**
* Iterator comparison.
*
* @param it Iterator being compared to this
* @return true if the Iterators are equal
*/
template <typename KEY, typename T>
bool Map<KEY, T>::Iterator::operator!=(Iterator it) {
return (_cur != it.operator->());
}