-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbintree.cpp
More file actions
274 lines (247 loc) · 6.27 KB
/
bintree.cpp
File metadata and controls
274 lines (247 loc) · 6.27 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
/*********************
Name: Kendall Shearman
Coding 06
Purpose: Binary Tree
**********************/
#include "bintree.h"
//public methods
BinTree::BinTree(){
count = 0;
root = nullptr;
}
BinTree::~BinTree(){
clear();
}
bool BinTree::isEmpty(){
bool isEmpty = false;
if(count == 0){
isEmpty = true;
}
return isEmpty;
}
int BinTree::getCount(){
return count;
}
bool BinTree::getRootData(Data * data){
bool getRoot = false;
if(root){
data->id = root->data.id;
data->information = root->data.information;
getRoot = true;
} else {
data->id = -1;
data->information = "";
}
return getRoot;
}
void BinTree::displayTree(){
cout << "DISPLAY TREE" << endl << "==============================================" << endl;
!(isEmpty()) ? cout << "Tree is NOT empty" : cout << "Tree is empty";
cout << endl;
cout << "Height " << getHeight() << endl;
cout << "Node Count: " << count << endl << endl;
cout << "Pre-Order Traversal" << endl;
displayPreOrder();
cout << endl;
cout << "In-Order Traversal" << endl;
displayInOrder();
cout << endl;
cout << "Post-Order Traversal" << endl;
displayPostOrder();
cout << "==============================================" << endl << endl;
}
void BinTree::clear(){
DataNode *temp = root;
clear(temp);
}
bool BinTree::addNode(int id, string data){
bool added = false;
if(id > 0 && data != ""){
DataNode * t = new DataNode;
t->data.id = id;
t->data.information = data;
t->left = nullptr;
t->right = nullptr;
DataNode **temp = &root;
added = addNode(t, temp);
}
return added;
}
bool BinTree::removeNode(int id){
bool removed = false;
int tempCount = count;
DataNode *temp = root;
root = removeNode(id, temp);
if(count < tempCount){
removed = true;
}
return removed;
}
bool BinTree::getNode(Data * data, int id){
bool found = false;
if(id > 0){
DataNode *temp = root;
found = getNode(data, id, temp);
}
return found;
}
bool BinTree::contains(int id){
bool containing = false;
if(id > 0){
DataNode *temp = root;
containing = contains(id, temp);
}
return containing;
}
int BinTree::getHeight(){
DataNode *temp = root;
return getHeight(temp);
}
void BinTree::displayPreOrder(){
DataNode *temp = root;
displayPreOrder(temp);
}
void BinTree::displayPostOrder(){
DataNode *temp = root;
displayPostOrder(temp);
}
void BinTree::displayInOrder(){
DataNode *temp = root;
displayInOrder(temp);
}
//private methods
void BinTree::clear(DataNode * temp){
if(temp){
clear(temp->left);
clear(temp->right);
temp->left = nullptr;
temp->right = nullptr;
delete temp;
count--;
root = nullptr;
}
}
bool BinTree::addNode(DataNode * t, DataNode ** temp){
bool inserted = false;
if(!(*temp)){
*temp = t;
count++;
inserted = true;
} else {
if (t->data.id < (*temp)->data.id){
inserted = addNode(t, &(*temp)->left);
} else {
inserted = addNode(t, &(*temp)->right);
}
}
return inserted;
}
DataNode* BinTree::removeNode(int id, DataNode * temp){
if(temp){
if(id < temp->data.id){
temp->left = removeNode(id, temp->left);
} else if (id > temp->data.id){
temp->right = removeNode(id, temp->right);
} else {
DataNode * nTemp = new DataNode;
if(temp->left == nullptr){
nTemp = temp->right;
delete temp;
temp = nTemp;
count--;
} else if (temp->right == nullptr){
nTemp = temp->left;
delete temp;
temp = nTemp;
count--;
} else {
nTemp = minValueNode(temp->right);
temp->data.id = nTemp->data.id;
temp->data.information = nTemp->data.information;
temp->right = removeNode(nTemp->data.id,temp->right);
}
}
}
return temp;
}
bool BinTree::getNode(Data * data, int id, DataNode * temp){
bool found = false;
if(temp){
if(id == temp->data.id){
data->id = temp->data.id;
data->information = temp->data.information;
found = true;
} else if (id < temp->data.id){
found = getNode(data, id, temp->left);
} else {
found = getNode(data, id, temp->right);
}
}
return found;
}
bool BinTree::contains(int id, DataNode * temp){
bool found = false;
if(temp){
if(id == temp->data.id){
found = true;
} else if (id < temp->data.id){
found = contains(id, temp->left);
} else {
found = contains(id, temp->right);
}
}
return found;
}
int BinTree::getHeight(DataNode * temp){
int height;
int x, y;
if (!temp){
height = 0;
} else {
x = getHeight(temp->left);
y = getHeight(temp->right);
height = x > y ? x + 1 : y + 1;
}
return height;
}
void BinTree::displayPreOrder(DataNode * temp){
if(temp){
cout << temp->data.id << " " << temp->data.information << endl;
if(temp->left){
displayPreOrder(temp->left);
}
if(temp->right){
displayPreOrder(temp->right);
}
}
}
void BinTree::displayPostOrder(DataNode * temp){
if(temp){
if(temp->left){
displayPostOrder(temp->left);
}
if(temp->right){
displayPostOrder(temp->right);
}
cout << temp->data.id << " " << temp->data.information << endl;
}
}
void BinTree::displayInOrder(DataNode * temproot){
if (temproot) {
if (temproot->left) {
displayInOrder(temproot->left);
}
cout << temproot->data.id << " " << temproot->data.information << endl;
if (temproot->right) {
displayInOrder(temproot->right);
}
}
return;
}
DataNode* BinTree::minValueNode(DataNode* node) {
DataNode* current = node;
while (current && current->left != NULL) {
current = current->left;
}
return current;
}