-
Notifications
You must be signed in to change notification settings - Fork 0
/
BinaryTree.hpp
237 lines (215 loc) · 4.96 KB
/
BinaryTree.hpp
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
#ifndef _BINARYTREE_HPP
#define _BINARYTREE_HPP
#include <iostream>
template <typename T>
class BinaryTree
{
private:
class TreeNode
{
friend class BinaryTree<typename T>;
private:
TreeNode(T value) :
m_data(value),
m_left(nullptr),
m_right(nullptr) {}
T m_data;
TreeNode* m_left;
TreeNode* m_right;
};
TreeNode* m_root;
bool insert(TreeNode*& tree, T value)
{
if (tree == nullptr) //value not found
{
tree = new TreeNode(value);
return true;
}
else if (tree->m_data == value) //value found
{
return false;
}
else //continue search on left subtree
{
if (value < tree->m_data)
return insert(tree->m_left, value);
else
return insert(tree->m_right, value);
}
}
void display(TreeNode* tree)
{
if (tree)
{
display(tree->m_left);
std::cout << tree->m_data << std::endl;
display(tree->m_right);
}
}
void remove(TreeNode*& tree, T value)
{
if (tree == nullptr) return;
if (value < tree->m_data)
remove(tree->m_left, value);
else if (value > tree->m_data)
remove(tree->m_right, value);
else
// We have found the node to delete.
makeDeletion(tree);
}
void makeDeletion(TreeNode*& tree)
{
TreeNode* nodeToDelete = tree;
TreeNode* attachPoint;
if (tree->m_right == nullptr)
{
tree = tree->m_left;
}
else if (tree->m_left == nullptr)
{
tree = tree->m_right;
}
else
{
attachPoint = tree->m_right;
while (attachPoint->m_left != nullptr)
{
attachPoint = attachPoint->m_left;
}
attachPoint->m_left = tree->m_left;
tree = tree->m_right;
}
delete nodeToDelete;
}
bool search(TreeNode* tree, T value)
{
if (tree == nullptr)
{
return false;
}
else if (tree->m_data == value)
{
return true;
}
else if (value < tree->m_data)
{
return search(tree->m_left, value);
}
else if (value > tree->m_data)
{
return search(tree->m_right, value);
}
else
return false;
}
unsigned int numberNodes(TreeNode* tree)
{
if (tree)
{
if (tree->m_left == nullptr && tree->m_right == nullptr)
{
return 1;
}
else if (tree->m_left == nullptr && tree->m_right != nullptr)
{
return 1 + numberNodes(tree->m_right);
}
else if (tree->m_right == nullptr && tree->m_left != nullptr)
{
return 1 + numberNodes(tree->m_left);
}
else if (tree->m_right != nullptr && tree->m_left != nullptr)
{
return 1 + numberNodes(tree->m_left) + numberNodes(tree->m_right);
}
}
}
unsigned int numberLeafNodes(TreeNode* tree)
{
if (tree)
{
if (tree->m_left == nullptr && tree->m_right == nullptr)
{
return 1;
}
else if (tree->m_left == nullptr && tree->m_right != nullptr)
{
return numberLeafNodes(tree->m_right);
}
else if (tree->m_right == nullptr && tree->m_left != nullptr)
{
return numberLeafNodes(tree->m_left);
}
else if (tree->m_right != nullptr && tree->m_left != nullptr)
{
return numberLeafNodes(tree->m_left) + numberLeafNodes(tree->m_right);
}
}
}
unsigned int height(TreeNode* tree)
{
if (tree)
{
if (tree->m_left == nullptr && tree->m_right == nullptr)
{
return 1 ;
}
else if (tree->m_left == nullptr)
{
return 1+ height(tree->m_right);
}
else if (tree->m_right == nullptr)
{
return 1 + height(tree->m_left);
}
else
{
return 1 + std::max(height(tree->m_left), height(tree->m_right));
}
}
}
public:
BinaryTree() :
m_root(nullptr){} // constructor
~BinaryTree()
{
destructorHelper(m_root);
}
void destructorHelper(TreeNode*& tree)
{
if (tree->m_left != nullptr)
{
destructorHelper(tree->m_left);
delete tree->m_left;
}
if (tree->m_right != nullptr)
{
destructorHelper(tree->m_right);
delete tree->m_right;
}
}
bool insert(T value){return insert(m_root, value);} // inserts a value into the tree, does not allow duplicates. If the value is already in the tree, false is returned, true otherwise.
void display(){display(m_root);} // displays all contents from left to right
void remove(T value){remove(m_root, value);} // deletes a value from the tree.
bool search(T value){return search(m_root, value);} // tests to see if a value exists in the tree.
unsigned int numberNodes(){return numberNodes(m_root);} // returns a count of all nodes in the tree.
unsigned int numberLeafNodes(){return numberLeafNodes(m_root);} // returns a count of all the leaf nodes in the tree
unsigned int height() {return height(m_root);} // returns the height of the tree.
void statInfo()
{
std::cout
<< "-- Tree Stats --"
<< std::endl
<< std::endl
<< "Total Nodes: "
<< numberNodes()
<< std::endl
<< "Leaf Nodes: "
<< numberLeafNodes()
<< std::endl
<< "Tree Height: "
<< height()
<< std::endl;
}
};
#endif