-
Notifications
You must be signed in to change notification settings - Fork 0
/
treemm.h
187 lines (161 loc) · 4.7 KB
/
treemm.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
#ifndef TREEMULTIMAP_INCLUDED
#define TREEMULTIMAP_INCLUDED
#include <vector>
// TODO delete after
#include <iostream>
using namespace std;
// template <typename KeyType, typename ValueType>
template <typename KeyType, typename ValueType>
class TreeMultimap
{
private:
struct Node{
KeyType m_key;
std::vector<ValueType> m_value;
Node *m_left;
Node *m_right;
Node(const KeyType& key, const ValueType& value)
{
m_left = nullptr;
m_right = nullptr;
m_key = key;
m_value.push_back(value);
}
};
public:
class Iterator
{
public:
Iterator()
{
m_it = nullptr;
m_index = 0;
}
Iterator(Node *curr)
{
m_it = curr;
m_index = 0;
}
ValueType& get_value() const
{
return m_it->m_value[m_index];
}
bool is_valid() const
{
if( m_it == nullptr || m_index == m_it->m_value.size() )
{
return false;
}
return true;
}
void advance()
{
if(m_index == m_it->m_value.size())
{
m_index = 0;
return;
}
else
{
m_index++;
return;
}
}
private:
Node *m_it;
int m_index;
};
TreeMultimap()
{
m_root = nullptr;
}
~TreeMultimap()
{
freeTheTree(m_root);
}
void insert(const KeyType& key, const ValueType& value)
{
// tree is empty, allocate new node and have m_root point to the new ode
if(m_root == nullptr)
{
m_root = new Node(key, value);
return;
}
Node *curr = m_root;
for(;;)
{
// if the key already exists, append the value to the vector assosiated with the key
if(key == curr->m_key)
{
curr->m_value.push_back(value);
return;
}
// if the key is less than the current node's key
if(key < curr->m_key)
{
// if there is a left child go left
if(curr->m_left != nullptr)
{
curr = curr->m_left;
}
else // otherwise allocate a new node and set the current node's left pointer to point to it
{
curr->m_left = new Node(key, value);
return;
}
}
else if(key > curr->m_key) // if the key is greater than the current node's key
{
// if there is a right child go right
if(curr->m_right != nullptr)
{
curr = curr->m_right;
}
else // otherwise allocate a new node and set the ucrrent node's right pointer to point to it
{
curr->m_right = new Node(key, value);
return;
}
}
}
}
Iterator find(const KeyType& key) const
{
Node* curr = m_root;
while(curr != nullptr)
{ // if the current key equals the key we want to find, we return the iterator pointing to that key
if(key == curr->m_key)
{
Iterator validIt(curr);
return validIt;
}
else if(key < curr->m_key) // if it's less than, move down to the left
{
curr = curr->m_left;
}
else // if it's greater than, move down to the right
{
curr = curr->m_right;
}
}
Iterator inValidIt; // calls default constructor for iterator where m_it = nullptr, no matching key found
return inValidIt;
}
private:
void freeTheTree(Node *curr)
{
// if empty, return
if(curr == nullptr)
{
return;
}
// delete nodes in left sub tree
freeTheTree(curr->m_left);
// delete nodes in right sub tree
freeTheTree(curr->m_right);
// free the current node
delete curr;
}
Node *m_root;
};
#endif // TREEMULTIMAP_INCLUDED