Skip to content

Commit e1203dd

Browse files
authored
Binary Tree CPP File
1 parent e3fe326 commit e1203dd

File tree

1 file changed

+273
-0
lines changed

1 file changed

+273
-0
lines changed

BinaryT

+273
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,273 @@
1+
/****************PROGRAM TO WRITE THE IMPLEMENTATION OF BINARY TREE CLASS******************/
2+
#include<iostream>
3+
#include "BinTree.h"
4+
#include "Node.h"
5+
using namespace std;
6+
//NULL CONSTRUCTOR
7+
binTree::binTree():root(NULL), count(0) {}
8+
9+
//PARAMETRIC CONSTRUCTOR
10+
binTree::binTree(Cnode *&ptr) :root(ptr), count(1)
11+
{
12+
ptr = root->left = root->right = NULL;
13+
}
14+
15+
//INSERT FUNCTION
16+
binTree& binTree::insert(Cnode *&ptr)
17+
{
18+
Cnode *rptr, *bptr;
19+
bptr = NULL;
20+
rptr = root;
21+
if (!root)
22+
{
23+
root = ptr;
24+
}
25+
else {
26+
while (rptr)
27+
{
28+
bptr = rptr;
29+
if (rptr->data == ptr->data)
30+
return *this;
31+
if (rptr->data < ptr->data)
32+
rptr = rptr->right;
33+
else
34+
rptr = rptr->left;
35+
}
36+
if (bptr->data < ptr->data)
37+
{
38+
bptr->right = ptr;
39+
}
40+
else
41+
{
42+
bptr->left = ptr;
43+
}
44+
}
45+
++count;//ptr =
46+
ptr->left = ptr->right = NULL;
47+
return *this;
48+
}
49+
50+
//PRINT INORDER
51+
void binTree::printInorder()
52+
{
53+
void printInorder(Cnode *root);
54+
55+
if (!root)
56+
cout << "Empty Tree" << endl;
57+
else
58+
printInorder(root);
59+
}
60+
void printInorder(Cnode *root)
61+
{
62+
if (root)
63+
{
64+
printInorder(root->left);
65+
root->print(); cout << " ";
66+
printInorder(root->right);
67+
}
68+
}
69+
70+
//PRINT PREORDER
71+
void binTree::printPreorder()
72+
{
73+
void printPreorder(Cnode *root);
74+
if (!root)
75+
cout << "Empty Tree" << endl;
76+
else
77+
printPreorder(root);
78+
}
79+
void printPreorder(Cnode *root)
80+
{
81+
if (root)
82+
{
83+
root->print(); cout << " ";
84+
printPreorder(root->left);
85+
printPreorder(root->right);
86+
}
87+
}
88+
89+
//PRINT POSTORDER
90+
void binTree::printPostorder()
91+
{
92+
void printPostorder(Cnode *root);
93+
if (!root)
94+
cout << "Empty Tree" << endl;
95+
else
96+
printPostorder(root);
97+
}
98+
void printPostorder(Cnode *root)
99+
{
100+
if (root)
101+
{
102+
printPostorder(root->left);
103+
printPostorder(root->right);
104+
root->print(); cout << " ";
105+
}
106+
}
107+
108+
//DESTRUCTOR
109+
binTree::~binTree()
110+
{
111+
void deleteNode(Cnode *&node);
112+
if (root)
113+
deleteNode(root);
114+
}
115+
void deleteNode(Cnode *&node)
116+
{
117+
if (node)
118+
{
119+
deleteNode(node->left);
120+
deleteNode(node->right);
121+
cout << "\n Deleting node: " << node->data;
122+
delete node;
123+
}
124+
node = NULL;
125+
}
126+
127+
//PRINT REVERSE
128+
void binTree::printReverse()
129+
{
130+
void printReverse(Cnode *root);
131+
if (!root)
132+
cout << "Empty Tree" << endl;
133+
else
134+
printReverse(root);
135+
}
136+
void printReverse(Cnode *root)
137+
{
138+
if (root)
139+
{
140+
printReverse(root->right);
141+
root->print(); cout << ",";
142+
printReverse(root->left);
143+
}
144+
}
145+
146+
//FUNCTION TO FIND NODE WITH MINIMUM VALUE
147+
int binTree::minValue(Cnode *node)
148+
{
149+
Cnode* current = node;
150+
while (current->left != NULL)
151+
{
152+
current = current->left;
153+
}
154+
return(current->getData());
155+
}
156+
157+
158+
//FUNCTION TO FIND NODE WITH MAXIMUM VALUE
159+
int binTree::maxValue(Cnode *node)
160+
{
161+
Cnode* current = node;
162+
while (current->right != NULL)
163+
{
164+
current = current->right;
165+
}
166+
return(current->getData());
167+
}
168+
169+
//FUNCTION TO GET COUNT
170+
int binTree::getCount()
171+
{
172+
return count;
173+
}
174+
175+
//FUNCTION TO PRINT COUNT
176+
void binTree::printCount()
177+
{
178+
cout << "COUNT: " << count<<endl;
179+
}
180+
181+
//IS EMPTY FUNCTION
182+
bool binTree::isEmpty()
183+
{
184+
if (root)
185+
return false;
186+
else
187+
return true;
188+
189+
}
190+
191+
//IS NOT EMPTY FUNCTION
192+
bool binTree::isNotEmpty()
193+
{
194+
if (root)
195+
return true;
196+
else
197+
return false;
198+
199+
}
200+
201+
//SEARCH NODE FUNCTION
202+
bool binTree::searchNode(Cnode* node, int key)
203+
{
204+
if (node == NULL)
205+
return false;
206+
207+
if (node->getData() == key)
208+
return true;
209+
210+
bool res1 = searchNode(node->left, key);
211+
bool res2 = searchNode(node->right, key);
212+
213+
return res1 || res2;
214+
}
215+
216+
//FUNCTION TO DELETE NODE IN BINARY TREE
217+
Cnode * binTree::deleteNode(Cnode *currentNode, int value)
218+
{
219+
if (currentNode == NULL) // empty tree
220+
return NULL;
221+
else if (value < currentNode->getData())
222+
currentNode->left = deleteNode(currentNode->left, value);
223+
else if (value > currentNode->getData())
224+
currentNode->right = deleteNode(currentNode->right, value);
225+
else
226+
{
227+
if (currentNode->left == NULL && currentNode->right == NULL)
228+
{
229+
currentNode = NULL;
230+
}
231+
else if (currentNode->left == NULL) // node has only right child
232+
{
233+
currentNode = currentNode->right;
234+
}
235+
else if (currentNode->right == NULL) // node has only left child
236+
{
237+
currentNode = currentNode->left;
238+
}
239+
else
240+
{
241+
Cnode *tempNode = new Cnode(minValue(currentNode->right));
242+
currentNode->setData(tempNode->getData());
243+
currentNode->right = deleteNode(currentNode->right, tempNode->getData());
244+
}
245+
246+
}
247+
248+
return currentNode;
249+
}
250+
251+
//FUNCTION TO COUNT LEVEL OF TREES
252+
int binTree::levelOfTree(Cnode* node)
253+
{
254+
if (node == NULL)
255+
return 0;
256+
else
257+
{
258+
int lDepth = levelOfTree(node->left);
259+
int rDepth = levelOfTree(node->right);
260+
if (lDepth > rDepth)
261+
return(lDepth + 1);
262+
else return(rDepth + 1);
263+
}
264+
}
265+
266+
//FUNCTION TO COUNT NUMBER OF TREES IN BINARY TREE
267+
int binTree::totalTreeNodes(Cnode* node)
268+
{
269+
if (node == NULL)
270+
return 0;
271+
else
272+
return(totalTreeNodes(node->left) + 1 + totalTreeNodes(node->right));
273+
}

0 commit comments

Comments
 (0)