-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtreemm.h
159 lines (142 loc) · 3.39 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
#ifndef TREEMULTIMAP_INCLUDED
#define TREEMULTIMAP_INCLUDED
#include <vector>
template <typename KeyType, typename ValueType>
class TreeMultimap
{
private:
struct Node
{
Node(const KeyType& key, const ValueType& value)
{
m_key = key;
m_values.push_back(value);
m_right = nullptr;
m_left = nullptr;
}
void addValue(const ValueType& value)
{
m_values.push_back(value);
}
// Key
KeyType m_key;
// Vector of values
std::vector<ValueType> m_values;
Node* m_right;
Node* m_left;
};
public:
class Iterator
{
public:
Iterator()
{
m_valid = false;
}
Iterator(Node* p)
{
it = p->m_values.begin();
m_valid = true;
m_treeNode = p;
}
ValueType& get_value() const
{
return *it;
}
bool is_valid() const
{
return m_valid;
}
void advance()
{
it++;
if (it != m_treeNode->m_values.end())
m_valid = true;
else
m_valid = false;
}
private:
typename std::vector<ValueType>::iterator it;
bool m_valid;
Node* m_treeNode;
};
TreeMultimap()
{
// Initialize root to null pointer
m_root = nullptr;
}
~TreeMultimap()
{
// Delete all nodes in tree
freeTree(m_root);
}
void insert(const KeyType& key, const ValueType& value)
{
// If empty, insert at root
if (m_root == nullptr)
{
m_root = new Node(key, value);
return;
}
// Traverse tree looking for place to insert value
Node* p = m_root;
for ( ; ; )
{
// If key is found, add to vector at node
if (key == p->m_key)
{
p->addValue(value);
return;
}
// Else, search for place to insert new node with value
else if (key < p->m_key)
{
if (p->m_left != nullptr)
p = p->m_left;
else
{
p->m_left = new Node(key, value);
return;
}
}
else
{
if (p->m_right != nullptr)
p = p->m_right;
else
{
p->m_right = new Node(key, value);
return;
}
}
}
}
Iterator find(const KeyType& key) const
{
// Traverse tree looking for key
Node* p = m_root;
while (p != nullptr)
{
if (key == p->m_key)
return Iterator(p);
else if (key < p->m_key)
p = p->m_left;
else
p = p->m_right;
}
// If key not found, return invalid iterator
return Iterator();
}
private:
Node* m_root;
// Recursive helper function for tree destructor
void freeTree(Node* p)
{
if (p == nullptr)
return;
freeTree(p->m_left);
freeTree(p->m_right);
delete p;
}
};
#endif // TREEMULTIMAP_INCLUDED