-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbinary_tree_template.cpp
More file actions
326 lines (260 loc) · 6.31 KB
/
binary_tree_template.cpp
File metadata and controls
326 lines (260 loc) · 6.31 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
324
325
326
#include <iostream>
#include <stdlib.h>
#include <time.h>
using namespace std;
//Use const instead of macros
const int NumElements = 27;
const int dataLimit = 97;
template <typename T>
class BinaryTree;
// In future I can add a parent pointer for optimization without affecting the users of BinarySearchTree
template <typename T>
class Node
{
private:
T data;
T key;
Node *left;
Node *right;
Node ():left(NULL), right (NULL), data(0), key(0)
{
}
Node (T ckey, T cdata):left(NULL), right (NULL), data(cdata), key(ckey)
{
}
Node (Node &cnode)
{
data = cnode.data;
key = cnode.key;
}
bool isLeaf ()
{
if (left == NULL && right == NULL)
return true;
else
return false;
}
friend class BinaryTree<T>; // class BinaryTree can now access data directly
};
template <typename T>
class BinaryTree
{
private:
Node<T> *root;
Node<T> * find_suitable_parent (Node<T> *parent, T key);
Node<T> * find_node (Node<T> *node, Node<T> **parent, T key);
Node<T> * find_inorder_successor_node (Node<T> *node, Node<T> **parent);
bool remove_leaf (Node<T> *node, Node<T> *parent);
bool remove_node_with_one_child (Node<T> *node);
void in_order_traverse (Node<T> *node, ostream &out);
public:
BinaryTree ():root(NULL)
{
}
bool get_data (T key, T *data);
void insert (T key, T data);
bool remove (T key);
~BinaryTree ();
template <class U>
friend ostream & operator<< (ostream &, BinaryTree<U> *tree);
BinaryTree<T> operator+ (T key);
BinaryTree<T> operator- (T key);
};
template <typename T>
Node<T> * BinaryTree<T>::find_suitable_parent (Node<T> *parent, T key)
{
if (key < parent->key)
{
if (parent->left == NULL)
return parent;
else
return find_suitable_parent (parent->left, key);
}
if (key >= parent->key)
{
if (parent->right == NULL)
return parent;
else
return find_suitable_parent (parent->right, key);
}
}
template <typename T>
void BinaryTree<T>::insert (T key, T data)
{
if (root == NULL)
{
root = new Node<T> (key, data);
return;
}
Node<T> *parent = find_suitable_parent (root, key);
if (key >= parent->key)
parent->right = new Node<T> (key, data);
else
parent->left = new Node<T> (key, data);
}
template <typename T>
Node<T> * BinaryTree<T>::find_node (Node<T> *node, Node<T> **parent, T key)
{
if (node->key == key)
return node;
else if (node->isLeaf ())
return NULL;
*parent = node;
if (node->left && key < node->key)
return find_node (node->left, parent, key);
if (node->right && key >= node->key)
return find_node (node->right, parent, key);
return NULL;
}
template <typename T>
Node<T> * BinaryTree<T>::find_inorder_successor_node (Node<T> *node, Node<T> **parent)
{
if (node->left == NULL)
return node;
*parent = node;
return find_inorder_successor_node (node->left, parent);
}
template <typename T>
bool BinaryTree<T>::remove_leaf (Node<T> *node, Node<T> *parent)
{
// In case node is a leaf node
if (node->isLeaf ())
{
// its a root node
if (parent == NULL)
{
root = NULL;
delete node;
} else
{
if (parent->left == node)
parent->left = NULL;
else
parent->right = NULL;
delete node;
}
return true;
}
return false;
}
template <typename T>
bool BinaryTree<T>::remove_node_with_one_child (Node<T> *node)
{
// In case node has only one child, replace it with the child
if (node->left == NULL || node->right == NULL)
{
Node<T> *repl;
if (node->left == NULL)
repl = node->right;
else
repl = node->left;
node->key = repl->key;
node->data = repl->data;
node->left = repl->left;
node->right = repl->right;
delete repl;
return true;
}
return false;
}
// Ideally we would want to return the node being deleted for the users to handle
// data, key destruction
template <typename T>
bool BinaryTree<T>::remove (T key)
{
Node<T> *parent = NULL;
Node<T> *node = find_node (root, &parent, key);
if (node == NULL)
return false; // or maybe throw an exception
if (remove_leaf (node, parent))
return true;
if (remove_node_with_one_child (node))
return true;
// In case node has two children, find the inorder successor node and replace it with
// with the current node. delete the inorder successor node
{
parent = node;
// finds the left most node of node->right
Node<T> *in_order_suc = find_inorder_successor_node (node->right, &parent);
node->key = in_order_suc->key;
node->data = in_order_suc->data;
if (in_order_suc->isLeaf ())
remove_leaf (in_order_suc, parent);
else
remove_node_with_one_child (in_order_suc);
}
return true;
}
template <typename T>
bool BinaryTree<T>::get_data (T key, T *data)
{
Node<T> *parent = NULL;
Node<T> *node = find_node (root, &parent, key);
if (node)
{
*data = node->data;
return true;
}
return false;
}
template <typename T>
BinaryTree<T>::~BinaryTree ()
{
}
template <typename T>
BinaryTree<T> BinaryTree<T>::operator+ (T key)
{
insert(key, 0);
return *this;
}
template <typename T>
BinaryTree<T> BinaryTree<T>::operator- (T key)
{
remove (key);
return *this;
}
template <typename T>
void BinaryTree<T>::in_order_traverse (Node<T> *node, ostream &out)
{
if (node && node->left)
in_order_traverse (node->left, out);
out << node->key << " ";
if (node && node->right)
in_order_traverse (node->right, out);
}
//
template <typename T>
ostream & operator<< (ostream &out, BinaryTree<T> *tree)
{
out << "Printing Binary Tree InOrder \n";
tree->in_order_traverse (tree->root, out);
out << "\n\n";
}
int main ()
{
BinaryTree<int> *tree = new BinaryTree<int> ();
int a[NumElements];
// Initialize random seed
srand(time(NULL));
for (int i = 0; i < NumElements; i++)
a[i] = 0;
for (int i = 1; i < NumElements; i++) {
a[i] = rand () % dataLimit;
tree->insert (a[i], 799);
}
//Tree with data inserted
cout << tree;
int temp = a[(rand() % NumElements)];
cout << "Removing " << temp << "\n" << endl;
tree->remove (temp);
cout << tree;
// Inserting an element using a fancy overloaded operator
cout << "Inserting 799 using our fancy overloaded operator for fun ;) \n" << endl;
(*tree) + 799;
cout << tree;
// Deleting an element using a fancy overloaded operator
cout << "Removing 799 using our fancy overloaded operator for fun ;) \n" << endl;
(*tree) - 799;
cout << tree;
std::cout << "Demonstrating friend class and friend functions" << std::endl;
}