-
Notifications
You must be signed in to change notification settings - Fork 0
/
TreeMap.h
142 lines (97 loc) · 2.9 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
#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 */
KeyValuePair<K, V> newKVP(key);
return stree.get(newKVP).getValue();
}
template<class K, class V>
bool TreeMap<K, V>::pop(const K &key) {
/* TODO */
KeyValuePair<K, V> newKVP(key);
return stree.remove(newKVP);
}
template<class K, class V>
bool TreeMap<K, V>::update(const K &key, const V &value) {
/* TODO */
KeyValuePair<K, V> newKVP(key, value);
return stree.insert(newKVP);
}
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() {
return stree.getMin();
}
template<class K, class V>
const KeyValuePair<K, V> &TreeMap<K, V>::lastEntry() {
return stree.getMax();
}
template<class K, class V>
void TreeMap<K, V>::pollFirstEntry() {
stree.remove(stree.getMin());
}
template<class K, class V>
void TreeMap<K, V>::pollLastEntry() {
stree.remove(stree.getMax());
}
template<class K, class V>
std::vector<KeyValuePair<K, V> > TreeMap<K, V>::subMap(K fromKey, K toKey) const {
/* TODO */
std::vector<KeyValuePair<K, V> > KVPvector;
KeyValuePair<K, V> KVP = stree.get(fromKey);
while(KVP.getKey() < toKey){
KVPvector.push_back(KVP);
KVP = stree.getNext(KVP);
}
KVPvector.push_back(KVP);
return KVPvector;
}
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