-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTreeMap.h
132 lines (98 loc) · 2.84 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
#ifndef TREEMAP_H
#define TREEMAP_H
#include <iostream>
#include <vector>
#include "KeyValuePair.h"
#include "ScapegoatTree.h"
template<class K, class V>
class TreeMap {
public: // DO NOT CHANGE THIS PART.
TreeMap();
void clear();
const V &get(const K &key) const;
bool pop(const K &key);
bool update(const K &key, const V &value);
const KeyValuePair<K, V> &ceilingEntry(const K &key);
const KeyValuePair<K, V> &floorEntry(const K &key);
const KeyValuePair<K, V> &firstEntry();
const KeyValuePair<K, V> &lastEntry();
void pollFirstEntry();
void pollLastEntry();
std::vector<KeyValuePair<K, V> > subMap(K fromKey, K toKey) const;
void print() const;
private: // YOU MAY ADD YOUR OWN UTILITY MEMBER FUNCTIONS HERE.
private: // DO NOT CHANGE THIS PART.
ScapegoatTree<KeyValuePair<K, V> > stree;
};
template<class K, class V>
TreeMap<K, V>::TreeMap() {}
template<class K, class V>
void TreeMap<K, V>::clear() {
/* TODO */
stree.removeAllNodes();
}
template<class K, class V>
const V &TreeMap<K, V>::get(const K &key) const {
/* TODO */
return(stree.get(key).getValue());
}
template<class K, class V>
bool TreeMap<K, V>::pop(const K &key) {
/* TODO */
return(stree.remove(key));
}
template<class K, class V>
bool TreeMap<K, V>::update(const K &key, const V &value) {
/* TODO */
//printf("girdim");
const KeyValuePair<K,V> newKeyValue(key,value);
return(stree.insert(newKeyValue));
}
template<class K, class V>
const KeyValuePair<K, V> &TreeMap<K, V>::ceilingEntry(const K &key) {
/* TODO */
return stree.getCeiling(key);
}
template<class K, class V>
const KeyValuePair<K, V> &TreeMap<K, V>::floorEntry(const K &key) {
/* TODO */
return stree.getFloor(key);
}
template<class K, class V>
const KeyValuePair<K, V> &TreeMap<K, V>::firstEntry() {
/* TODO */
return stree.getMin();
}
template<class K, class V>
const KeyValuePair<K, V> &TreeMap<K, V>::lastEntry() {
/* TODO */
return stree.getMax();
}
template<class K, class V>
void TreeMap<K, V>::pollFirstEntry() {
/* TODO */
stree.remove(firstEntry());
}
template<class K, class V>
void TreeMap<K, V>::pollLastEntry() {
/* TODO */
stree.remove(lastEntry());
}
template<class K, class V>
std::vector<KeyValuePair<K, V> > TreeMap<K, V>::subMap(K fromKey, K toKey) const {
/* TODO */
K tempkey = fromKey;
std::vector<KeyValuePair<K, V> > vec;
while(tempkey <= fromKey){
vec.push_back(stree.get(tempkey));
tempkey = ceilingEntry(tempkey).getKey();
}
}
template<class K, class V>
void TreeMap<K, V>::print() const {
std::cout << "# Printing the tree map ..." << std::endl;
std::cout << "# ScapegoatTree<KeyValuePair<K, V> > stree:" << std::endl;
stree.printPretty();
std::cout << "# Printing is done." << std::endl;
}
#endif //TREEMAP_H