forked from sarahfarrukh/cpp-data-structures
-
Notifications
You must be signed in to change notification settings - Fork 2
/
BinaryT
273 lines (247 loc) · 4.64 KB
/
BinaryT
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
/****************PROGRAM TO WRITE THE IMPLEMENTATION OF BINARY TREE CLASS******************/
#include<iostream>
#include "BinTree.h"
#include "Node.h"
using namespace std;
//NULL CONSTRUCTOR
binTree::binTree():root(NULL), count(0) {}
//PARAMETRIC CONSTRUCTOR
binTree::binTree(Cnode *&ptr) :root(ptr), count(1)
{
ptr = root->left = root->right = NULL;
}
//INSERT FUNCTION
binTree& binTree::insert(Cnode *&ptr)
{
Cnode *rptr, *bptr;
bptr = NULL;
rptr = root;
if (!root)
{
root = ptr;
}
else {
while (rptr)
{
bptr = rptr;
if (rptr->data == ptr->data)
return *this;
if (rptr->data < ptr->data)
rptr = rptr->right;
else
rptr = rptr->left;
}
if (bptr->data < ptr->data)
{
bptr->right = ptr;
}
else
{
bptr->left = ptr;
}
}
++count;//ptr =
ptr->left = ptr->right = NULL;
return *this;
}
//PRINT INORDER
void binTree::printInorder()
{
void printInorder(Cnode *root);
if (!root)
cout << "Empty Tree" << endl;
else
printInorder(root);
}
void printInorder(Cnode *root)
{
if (root)
{
printInorder(root->left);
root->print(); cout << " ";
printInorder(root->right);
}
}
//PRINT PREORDER
void binTree::printPreorder()
{
void printPreorder(Cnode *root);
if (!root)
cout << "Empty Tree" << endl;
else
printPreorder(root);
}
void printPreorder(Cnode *root)
{
if (root)
{
root->print(); cout << " ";
printPreorder(root->left);
printPreorder(root->right);
}
}
//PRINT POSTORDER
void binTree::printPostorder()
{
void printPostorder(Cnode *root);
if (!root)
cout << "Empty Tree" << endl;
else
printPostorder(root);
}
void printPostorder(Cnode *root)
{
if (root)
{
printPostorder(root->left);
printPostorder(root->right);
root->print(); cout << " ";
}
}
//DESTRUCTOR
binTree::~binTree()
{
void deleteNode(Cnode *&node);
if (root)
deleteNode(root);
}
void deleteNode(Cnode *&node)
{
if (node)
{
deleteNode(node->left);
deleteNode(node->right);
cout << "\n Deleting node: " << node->data;
delete node;
}
node = NULL;
}
//PRINT REVERSE
void binTree::printReverse()
{
void printReverse(Cnode *root);
if (!root)
cout << "Empty Tree" << endl;
else
printReverse(root);
}
void printReverse(Cnode *root)
{
if (root)
{
printReverse(root->right);
root->print(); cout << ",";
printReverse(root->left);
}
}
//FUNCTION TO FIND NODE WITH MINIMUM VALUE
int binTree::minValue(Cnode *node)
{
Cnode* current = node;
while (current->left != NULL)
{
current = current->left;
}
return(current->getData());
}
//FUNCTION TO FIND NODE WITH MAXIMUM VALUE
int binTree::maxValue(Cnode *node)
{
Cnode* current = node;
while (current->right != NULL)
{
current = current->right;
}
return(current->getData());
}
//FUNCTION TO GET COUNT
int binTree::getCount()
{
return count;
}
//FUNCTION TO PRINT COUNT
void binTree::printCount()
{
cout << "COUNT: " << count<<endl;
}
//IS EMPTY FUNCTION
bool binTree::isEmpty()
{
if (root)
return false;
else
return true;
}
//IS NOT EMPTY FUNCTION
bool binTree::isNotEmpty()
{
if (root)
return true;
else
return false;
}
//SEARCH NODE FUNCTION
bool binTree::searchNode(Cnode* node, int key)
{
if (node == NULL)
return false;
if (node->getData() == key)
return true;
bool res1 = searchNode(node->left, key);
bool res2 = searchNode(node->right, key);
return res1 || res2;
}
//FUNCTION TO DELETE NODE IN BINARY TREE
Cnode * binTree::deleteNode(Cnode *currentNode, int value)
{
if (currentNode == NULL) // empty tree
return NULL;
else if (value < currentNode->getData())
currentNode->left = deleteNode(currentNode->left, value);
else if (value > currentNode->getData())
currentNode->right = deleteNode(currentNode->right, value);
else
{
if (currentNode->left == NULL && currentNode->right == NULL)
{
currentNode = NULL;
}
else if (currentNode->left == NULL) // node has only right child
{
currentNode = currentNode->right;
}
else if (currentNode->right == NULL) // node has only left child
{
currentNode = currentNode->left;
}
else
{
Cnode *tempNode = new Cnode(minValue(currentNode->right));
currentNode->setData(tempNode->getData());
currentNode->right = deleteNode(currentNode->right, tempNode->getData());
}
}
return currentNode;
}
//FUNCTION TO COUNT LEVEL OF TREES
int binTree::levelOfTree(Cnode* node)
{
if (node == NULL)
return 0;
else
{
int lDepth = levelOfTree(node->left);
int rDepth = levelOfTree(node->right);
if (lDepth > rDepth)
return(lDepth + 1);
else return(rDepth + 1);
}
}
//FUNCTION TO COUNT NUMBER OF TREES IN BINARY TREE
int binTree::totalTreeNodes(Cnode* node)
{
if (node == NULL)
return 0;
else
return(totalTreeNodes(node->left) + 1 + totalTreeNodes(node->right));
}