-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathbinaryTree.cpp
executable file
·307 lines (303 loc) · 7.59 KB
/
binaryTree.cpp
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
//
// binaryTree.cpp
// AnishC++
//
// Created by Anish Mookherjee on 01/11/19.
// Copyright © 2019 Anish Mookherjee. All rights reserved.
//
#include <iostream>
using namespace std;
struct node
{
int data;
struct node *left;
struct node *right;
}*tree;
void createTree(struct node* tree)
{
tree=NULL;
}
struct node* searchElement(struct node *tree,int val)
{
if(tree->data==val||tree==NULL)
return tree;
else if(val<tree->data)
return searchElement(tree->left, val);
else
return searchElement(tree->right, val);
}
struct node *insertElement(struct node *tree, int val)
{
struct node *ptr, *nodeptr, *parentptr;
ptr=(struct node*)malloc(sizeof(struct node));
ptr->data = val;
ptr->left = NULL;
ptr->right = NULL;
if(tree==NULL)
{
tree=ptr;
tree->left=NULL;
tree->right=NULL;
}
else
{
parentptr=NULL;
nodeptr=tree;
while(nodeptr!=NULL)
{
parentptr=nodeptr;
if(val<nodeptr->data)
nodeptr=nodeptr->left;
else
nodeptr = nodeptr->right;
}
if(val<parentptr->data)
parentptr->left = ptr;
else
parentptr->right = ptr;
}
return tree;
}
void preorder(struct node* tree)
{
if(tree!=NULL)
{
cout<<tree->data<<" ";
preorder(tree->left);
preorder(tree->right);
}
}
void inorder(struct node* tree)
{
if(tree!=NULL)
{
inorder(tree->left);
cout<<tree->data<<" ";
inorder(tree->right);
}
}
void postorder(struct node* tree)
{
if(tree!=NULL)
{
postorder(tree->left);
postorder(tree->right);
cout<<tree->data<<" ";
}
}
struct node* largest(struct node* tree)
{
if(tree==NULL||tree->right==NULL)
return tree;
else
return largest(tree->right);
}
struct node* smallest(struct node* tree)
{
if(tree==NULL||tree->left==NULL)
return tree;
else
return smallest(tree->left);
}
struct node *deleteElement(struct node *tree, int val)
{
struct node *cur, *parent, *suc, *psuc, *ptr;
if(tree->left==NULL)
{
printf("\nThe tree is empty ");
return(tree);
}
parent = tree;
cur = tree->left;
while(cur!=NULL && val!= cur->data)
{
parent = cur;
cur = (val<cur->data)? cur->left:cur->right;
}
if(cur == NULL)
{
printf("\nThe value to be deleted is not present in the tree");
return(tree);
}
if(cur->left == NULL)
ptr = cur->right;
else if(cur->right == NULL)
ptr = cur->left;
else
{
// Find the in–order successor and its parent
psuc = cur;
cur = cur->left;
while(suc->left!=NULL)
{
psuc = suc;
suc = suc->left;
}
if(cur==psuc)
{
// Situation 1
suc->left = cur->right;
}
else
{
// Situation 2
suc->left = cur->left;
psuc->left = suc->right;
suc->right = cur->right;
}
ptr = suc;
}
// Attach ptr to the parent node
if(parent->left == cur)
parent->left=ptr;
else
parent->right=ptr;
free(cur);
return tree;
}
int height(struct node* tree)
{
int lh,rh;
if(tree==NULL)
{
return 0;
}
else
{
lh=height(tree->left);
rh=height(tree->right);
if(lh>rh)
return lh+1;
else
return rh+1;
}
}
int totalNodes(struct node* tree)
{
if(tree==NULL)
return 0;
else
return totalNodes(tree->left)+totalNodes(tree->right)+1;
}
int internalNodes(struct node* tree)
{
if(tree==NULL)
return 0;
else if(tree->left==NULL&&tree->right==NULL)
return 0;
else
return internalNodes(tree->left)+internalNodes(tree->right)+1;
}
int externalNodes(struct node* tree)
{
if(tree==NULL)
return 0;
else if(tree->left==NULL&&tree->right==NULL)
return 1;
else
return externalNodes(tree->left)+externalNodes(tree->right);
}
void deleteTree(struct node* tree)
{
if(tree!=NULL)
{
deleteTree(tree->left);
deleteTree(tree->right);
free(tree);
}
}
int main()
{
int option,val;
struct node* ptr;
ptr=(struct node*)malloc(sizeof(struct node));
createTree(tree);
do
{
cout<<"1.INSERT ELEMENT"<<endl;
cout<<"2.SEARCH ELEMENT"<<endl;
cout<<"3.SMALLEST ELEMENT"<<endl;
cout<<"4.LARGEST ELEMENT"<<endl;
cout<<"5.DELETE ELEMENT"<<endl;
cout<<"6.HEIGHT"<<endl;
cout<<"7.TOTAL NODES"<<endl;
cout<<"8.TOTAL INTERNAL NODES"<<endl;
cout<<"9.TOTAL EXTERNAL NODES"<<endl;
cout<<"10.DELETE TREE"<<endl;
cout<<"11.PRE-ORDER TRAVERSAL"<<endl;
cout<<"12.IN-ORDER TRAVERSAL"<<endl;
cout<<"13.POST-ORDER TRAVERSAL"<<endl;
cout<<"14.EXIT"<<endl;
cin>>option;
switch(option)
{
case 1:
cout<<"Insert the element you want to enter:"<<endl;
cin>>val;
tree=insertElement(tree, val);
break;
case 2:
cout<<"Enter element you want to search:"<<endl;
cin>>val;
if(searchElement(tree, val)==NULL)
cout<<"Element not found"<<endl;
else
cout<<"Element found"<<endl;
break;
case 3:
ptr=smallest(tree);
cout<<"The smallest element is: "<<ptr->data<<endl;
break;
case 4:
ptr=largest(tree);
cout<<"The largest element is: "<<ptr->data<<endl;
break;
case 5:
cout<<"Enter the element to be deleted"<<endl;
cin>>val;
tree=deleteElement(tree, val);
if(tree==NULL)
cout<<"Element not found"<<endl;
else
cout<<"Element deleted"<<endl;
break;
case 6:
cout<<"Height of the tree is: "<<height(tree)<<endl;
break;
case 7:
cout<<"Total no. of nodes present are: "<<totalNodes(tree)<<endl;
break;
case 8:
cout<<"Total no. of internal nodes are: "<<internalNodes(tree)<<endl;
break;
case 9:
cout<<"Total no. of external nodes are: "<<externalNodes(tree)<<endl;
break;
case 10:
cout<<"Deleting the entire tree."<<endl;
deleteTree(tree);
break;
case 11:
cout<<"Pre-order traversal:"<<endl;
preorder(tree);
cout<<endl;
break;
case 12:
cout<<"In-order traversal:"<<endl;
inorder(tree);
cout<<endl;
break;
case 13:
cout<<"Post-order traversal:"<<endl;
postorder(tree);
cout<<endl;
break;
case 14:
cout<<"EXIT"<<endl;
break;
default:
cout<<"Wrong choice, enter again."<<endl;
}
}while(option!=14);
return 0;
}