-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtreemap.h
86 lines (49 loc) · 1.35 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
#ifndef TREEMAP_H
#define TREEMAP_H
#include "tree.h"
template<typename Key, typename Value>
class KeyValuePair {
public:
const Key k;
Value v;
KeyValuePair():k(0) {
}
KeyValuePair(Key key, Value value):k(key) {
this->v = value;
}
KeyValuePair(Key key): k(key) {
}
bool operator<( KeyValuePair<Key, Value> &obj1 ) const {
return this->k < obj1.k ;
}
// TODO your code for KeyValuePair goes here
};
template<typename Key, typename Value>
ostream & operator<< (ostream & o, const KeyValuePair<Key,Value> & kv){
o << kv.k << "," << kv.v;
return o;
}
template<typename Key, typename Value>
class TreeMap {
private:
BinarySearchTree<KeyValuePair<Key,Value> > tree;
public:
KeyValuePair<Key,Value> * insert( const Key & k, const Value & v) {
return &(tree.insert(KeyValuePair<Key,Value>(k,v))->data);
}
KeyValuePair<Key,Value> * find(Key key) {
KeyValuePair<Key, Value> keyValuePair(key);
return &((tree.find(keyValuePair))->data);
}
void write(ostream & o) const {
tree.write(o);
}
// TODO your code for TreeMap goes here:
/*
class TreeNodeIterator{
unique_ptr<BinarySearchTree<KeyValuePair<Key,Value> > > root;
};
*/
};
// do not edit below this line
#endif