-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBinaryTree.h
More file actions
323 lines (255 loc) · 7.41 KB
/
BinaryTree.h
File metadata and controls
323 lines (255 loc) · 7.41 KB
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
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
#ifndef BINARYTREE_HEADER
#define BINARYTREE_HEADER
#include <functional>
#include <vector>
#include <iostream>
template <typename ObjectT, typename KeyT=ObjectT>
class BinaryTree
{
private:
class Node
{
private:
ObjectT value;
Node *left;
Node *right;
public:
Node(ObjectT value): value(value)
{
this->left = this->right = nullptr;
}
~Node()
{
delete this->left;
delete this->right;
}
Node &getLeft(){return *this->left;}
Node &getRight(){return *this->right;}
bool hasLeftChild(){return this->left != nullptr;}
bool hasRightChild(){return this->right != nullptr;}
void setLeft(Node *node){this->left = node;}
void setRight(Node *node){this->right = node;}
void setLeft(Node &node){this->left = &node;}
void setRight(Node &node){this->right = &node;}
ObjectT getData(){return this->value;}
void setData(ObjectT value){this->value = value;}
KeyT getKey(){return this->getKey(this->value);}
void clear_for_removal()
{
setLeft(nullptr);
setRight(nullptr);
}
};
Node *root;
unsigned int size;
bool insert(Node &node, ObjectT value);
Node *remove(Node &node, ObjectT value);
Node &find_min(Node &node);
void copy(Node &to, Node &from);
unsigned int getHeight(Node &node);
bool contains(Node &node, ObjectT value);
std::function<KeyT(ObjectT &)> getKey;
public:
Node &getRoot(){return *this->root;}
BinaryTree()
{
this->root = nullptr;
this->size = 0;
this->getKey = [](ObjectT &object){return object;};
}
BinaryTree(std::function<KeyT(ObjectT &)> getKey)
{
this->root = nullptr;
this->size = 0;
this->getKey = getKey;
}
~BinaryTree(){delete this->root;}
unsigned int getSize(){return this->size;}
unsigned int getHeight();
bool isEmpty(){return this->getSize() == 0;}
void insert(ObjectT value);
void remove(ObjectT value);
bool contains(ObjectT value);
std::vector<ObjectT> getInOrderTraversal();
};
unsigned int max(unsigned int first, unsigned int second){return first > second ? first: second;}
template <typename ObjectT, typename KeyT>
unsigned int BinaryTree<ObjectT, KeyT>::getHeight(Node &node)
{
if(node.hasLeftChild() && node.hasRightChild())
return max(getHeight(node.getLeft()), getHeight(node.getRight())) + 1;
else if(node.hasLeftChild() && !node.hasRightChild())
return getHeight(node.getLeft()) + 1;
else if(!node.hasLeftChild() && node.hasRightChild())
return getHeight(node.getRight()) + 1;
else return 1;
}
template <typename ObjectT, typename KeyT>
unsigned int BinaryTree<ObjectT, KeyT>::getHeight()
{
return getHeight(getRoot());
}
template<typename ObjectT, typename KeyT>
bool BinaryTree<ObjectT, KeyT>::contains(Node &node, ObjectT value)
{
ObjectT data = node.getData();
if(value == data)
return true;
if(getKey(value) < getKey(data))
if(node.hasLeftChild())
return contains(node.getLeft(), value);
else return false;
else
if(node.hasRightChild())
return contains(node.getRight(), value);
else return false;
}
template <typename ObjectT, typename KeyT>
bool BinaryTree<ObjectT, KeyT>::contains(ObjectT value)
{
return contains(getRoot(), value);
}
template <typename ObjectT, typename KeyT>
void BinaryTree<ObjectT, KeyT>::copy(Node &to, Node &from)
{
to.setData(from.getData());
if(from.hasLeftChild())
to.setLeft(from.getLeft());
else to.setLeft(nullptr);
if(from.hasRightChild())
to.setRight(from.getRight());
else to.setRight(nullptr);
}
template <typename ObjectT, typename KeyT>
typename BinaryTree<ObjectT, KeyT>::Node &BinaryTree<ObjectT, KeyT>::find_min(Node &node)
{
Node *ptr = &node;
while(ptr->hasLeftChild())
ptr = &ptr->getLeft();
return *ptr;
}
template <typename ObjectT, typename KeyT>
bool BinaryTree<ObjectT, KeyT>::insert(Node &node, ObjectT value)
{
ObjectT data = node.getData();
if(getKey(value) < getKey(data))
{
if(node.hasLeftChild())
return this->insert(node.getLeft(), value);
else
{
node.setLeft(new Node(value));
return true;
}
}
else if(getKey(value) > getKey(data))
{
if(node.hasRightChild())
return this->insert(node.getRight(), value);
else
{
node.setRight(new Node(value));
return true;
}
}
else
return false;
}
template <typename ObjectT, typename KeyT>
void BinaryTree<ObjectT, KeyT>::insert(ObjectT value)
{
bool is_new;
if(isEmpty())
{
this->root = new Node(value);
is_new = true;
}
else {
is_new = this->insert(this->getRoot(), value);
}
if(is_new)
this->size++;
}
template <typename ObjectT, typename KeyT>
typename BinaryTree<ObjectT, KeyT>::Node *BinaryTree<ObjectT, KeyT>::remove(Node &node, ObjectT value)
{
// std::cout << "Node " << node.getData() << std::endl;
// if(node.hasRightChild())
// std::cout << "right " << node.getRight().getData() << std::endl;
ObjectT data = node.getData();
if(getKey(data) != getKey(value))
{
if(getKey(value) < getKey(data) && node.hasLeftChild())
{
node.setLeft(remove(node.getLeft(), value));
return &node;
}
else if(getKey(value) > getKey(data) && node.hasRightChild())
{
node.setRight(remove(node.getRight(), value));
return &node;
}
}
else
{
if(!node.hasLeftChild() && !node.hasRightChild())
{
delete &node;
this->size--;
return nullptr;
}
else if(node.hasLeftChild() && !node.hasRightChild())
{
Node *left = &node.getLeft();
copy(node, *left);
left->clear_for_removal();
delete left;
this->size--;
return &node;
}
else if(node.hasRightChild() && !node.hasLeftChild())
{
Node *right = &node.getRight();
copy(node, *right);
right->clear_for_removal();
delete right;
this->size--;
return &node;
}
else
{
Node *right_min = &find_min(node.getRight());
node.setData(right_min->getData());
node.setRight(remove(node.getRight(), node.getData()));
return &node;
}
}
return &node;
}
template <typename ObjectT, typename KeyT>
void BinaryTree<ObjectT, KeyT>::remove(ObjectT value)
{
this->root = remove(getRoot(), value);
}
template <typename ObjectT, typename KeyT>
std::vector<ObjectT> BinaryTree<ObjectT, KeyT>::getInOrderTraversal()
{
std::vector<ObjectT> res_vector;
res_vector.reserve(this->getSize());
std::function<void(Node &)> func;
func = [&res_vector, &func](Node &node)
{
if(node.hasLeftChild())
{
func(node.getLeft());
}
res_vector.push_back(node.getData());
if(node.hasRightChild())
{
func(node.getRight());
}
};
func(this->getRoot());
return res_vector;
}
#endif