-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcheckfull,complete,perfect.cpp
454 lines (388 loc) · 10.9 KB
/
checkfull,complete,perfect.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
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
/*
#include <iostream>
#include <queue>
using namespace std;
class TreeNode
{
public:
int data;
TreeNode *left;
TreeNode *right;
TreeNode(int data) : data(data), left(nullptr), right(nullptr) {}
};
class BinaryTree
{
public:
TreeNode *root;
BinaryTree() : root(nullptr) {}
void bfsTraversal()
{
if (root == nullptr)
return;
queue<TreeNode *> q;
TreeNode *node = root;
q.push(node);
cout << endl;
while (!q.empty())
{
node = q.front();
q.pop();
cout << node->data << " ";
if (node->left != nullptr)
q.push(node->left);
if (node->right != nullptr)
q.push(node->right);
}
}
void dfsTraversal()
{
dfsTraversal(root);
}
void dfsTraversal(TreeNode *root)
{
if (root == nullptr)
return;
cout << root->data << " ";
dfsTraversal(root->left);
dfsTraversal(root->right);
}
// check complete with queue
bool checkComplete()
{
if (root == nullptr)
return true;
queue<TreeNode *> q;
q.push(root);
bool nonCompleteNode = false;
while (!q.empty())
{
TreeNode *cur = q.front();
q.pop();
if (cur->left != nullptr)
{
if (nonCompleteNode == true)
{
return false;
}
q.push(cur->left);
}
else
{
nonCompleteNode = true;
}
if (cur->right != nullptr)
{
if (nonCompleteNode == true)
{
return false;
}
q.push(cur->right);
}
else
{
nonCompleteNode = true;
}
}
return true;
}
bool checkFull()
{
return checkFull(root);
}
bool checkFull(TreeNode *root)
{
if (root == nullptr)
return true;
if (root->left == nullptr && root->right == nullptr)
return true;
if ((root->left) && (root->right))
return (checkFull(root->left) && checkFull(root->right));
return false;
}
// Function to check if the given tree is perfect
bool checkPerfect()
{
queue<TreeNode *> q;
// Push the root node
q.push(root);
// Flag to check if leaf nodes have been found
bool flag = false;
while (!q.empty())
{
TreeNode *temp = q.front();
q.pop();
// If current node has both left and right child
if (temp->left && temp->right)
{
// If a leaf node has already been found
// then return false
if (flag == true)
{
return false;
}
// If a leaf node has not been discovered yet
// push the left and right child in the queue
else
{
q.push(temp->left);
q.push(temp->right);
}
}
// If a leaf node is found mark flag as one
else if (!temp->left && !temp->right)
{
flag = true;
}
// If the current node has only one child
// then return false
else if (!temp->left || !temp->right)
return false;
}
// the given tree is perfect return true
return true;
}
};
int main()
{
// example 1
// not a complete tree
BinaryTree T1;
T1.root = new TreeNode(7);
T1.root->left = new TreeNode(3);
T1.root->right = new TreeNode(9);
T1.root->left->left = new TreeNode(1);
T1.root->right->right = new TreeNode(4);
cout << "BFS Traversal: ";
T1.bfsTraversal();
cout << endl;
cout << "DFS Traversal: " << endl;
T1.dfsTraversal();
cout << endl;
cout << endl
<< "Example 1: " << endl;
cout << (T1.checkComplete() ? "is complete" : "is not complete") << endl;
// example 2
// is a complete tree
cout << endl
<< "Example 2: " << endl;
BinaryTree T2;
T2.root = new TreeNode(7);
T2.root->left = new TreeNode(3);
T2.root->right = new TreeNode(9);
T2.root->left->left = new TreeNode(1);
T2.root->left->right = new TreeNode(4);
cout << (T2.checkComplete() ? "is complete" : "is not complete") << endl;
// example 3
// is a full tree
cout << endl
<< "Example 3: " << endl;
BinaryTree T3;
T3.root = new TreeNode(7);
T3.root->left = new TreeNode(3);
T3.root->right = new TreeNode(9);
T3.root->left->left = new TreeNode(1);
T3.root->left->right = new TreeNode(4);
cout << (T3.checkFull() ? "is full" : "is not full") << endl;
// example 4
// is not a full tree
cout << endl
<< "Example 4: " << endl;
BinaryTree T4;
T4.root = new TreeNode(7);
T4.root->left = new TreeNode(3);
T4.root->right = new TreeNode(9);
T4.root->left->left = new TreeNode(1);
cout << (T4.checkFull() ? "is full" : "is not full") << endl;
// example 5
// is a perfect tree
cout << endl
<< "Example 5: " << endl;
BinaryTree T5;
T5.root = new TreeNode(7);
T5.root->left = new TreeNode(3);
T5.root->right = new TreeNode(9);
T5.root->left->left = new TreeNode(1);
T5.root->left->right = new TreeNode(2);
T5.root->right->left = new TreeNode(11);
T5.root->right->right = new TreeNode(14);
cout << (T5.checkPerfect() ? "is perfect" : "is not perfect") << endl;
// example 6
// is not a perfect tree
cout << endl
<< "Example 6: " << endl;
BinaryTree T6;
T6.root = new TreeNode(7);
T6.root->left = new TreeNode(3);
T6.root->right = new TreeNode(9);
T6.root->left->left = new TreeNode(1);
cout << (T6.checkPerfect() ? "is perfect" : "is not perfect") << endl;
return 0;}
*/
#include <iostream>
#include <bits/stdc++.h>
#include <queue>
#include <stack>
using namespace std;
class binary{
public:
int data;
binary *left , *right;
// Valued Constructor
binary(int value){
data = value;
right = NULL; left = NULL;
}};
// insertion of Values in Tree.
binary *insert_To_Tree(binary *root , int value){ // root null
if(root == NULL){
return new binary(value); // Assigning Value to root if NULL. root value
} // when root is assign before runing than below statment will work.
if(value > root->data){
root->right = insert_To_Tree(root->right , value); //
}else // add to root left.
{root->left = insert_To_Tree(root->left , value);}
return root;}
// Finding the minimum and maximum ib Binary Tree.
void min(binary *root){
binary * curr = root ;
while(curr ->left != NULL){
curr = curr->left;
}
cout << "The Minimum Number in Tree is : " << curr->data << endl;
}
void max(binary *root){
binary * curr = root ;
while(curr ->right != NULL){
curr = curr->right;
}
cout << "The Maximum Number in Tree is : " << curr->data << endl;
}
void BFS(binary *root){
if(root == NULL){
return;}
queue<binary * > q;
q.push(root);
binary *node; // temp node
while(!q.empty()){
node = q.front();
cout << node->data << " ";
q.pop();
if (node->left !=NULL){
q.push(node->left);
}
if (node->right !=NULL){
q.push(node->right);
}
}};
bool isFULL(binary* &root){
if(root == NULL)
return true;
if(root->left == NULL && root->right == NULL){
return true;
}
if(root->left != NULL && root->right != NULL){
return (isFULL(root->left) && isFULL(root->right));
}
return false;
}
int Depth(binary* &node)
{
int dep = 0;
while (node != NULL)
{
dep++;
node = node->left;
}
return dep;
}
bool Check(binary* &root, int dep, int level = 0)
{
if (root == NULL) //node does not present
return true;
if (root->left == NULL && root->right == NULL)//finding node is present and level is one
return (dep == level+1);
if (root->left == NULL || root->right == NULL)
return false;
return Check(root->left, dep, level+1) && Check(root->right, dep, level+1);
}
bool isPerfect(binary* &root)
{
int dep = Depth(root);
return Check(root, dep);
}
int TotalNodes(binary* &root)
{
if (root == NULL)
return (0);
return (1 + TotalNodes(root->left) +
TotalNodes(root->right));
}
bool isComplete( binary* &root,int index, int number_nodes)
{
if (root == NULL)
return (true);
if (index >= number_nodes)
return (false);
return (isComplete(root->left, 2*index + 1, number_nodes) &&
isComplete(root->right, 2*index + 2, number_nodes));
}
void DFS_Pre(binary *root){
if(root == NULL){
return;}
stack<binary *> s;
s.push(root);
binary *node;
while(!s.empty()){
node = s.top();
cout << node->data << " ";
s.pop();
if (node->right !=NULL){
s.push(node->right);
}
if (node->left !=NULL){
s.push(node->left);
}
}};
// Checking either a tree is full or perfect or Complete.
int main(){
// binary b;
binary *root = new binary(4); // root
/* binary b;
binary *root = NULL;
root = b.insert(root,5);*/ // need to understand
//binary *root = insert_To_Tree(root , 4); same work
insert_To_Tree(root, 10);
insert_To_Tree(root, 2);
insert_To_Tree(root, 4);
insert_To_Tree(root, 6);
insert_To_Tree(root, 8);
insert_To_Tree(root, 10);
// Tree Traversal BFS AND DFS.
BFS(root);
cout << endl;
DFS_Pre(root);
cout << endl;
// Minimum and maximum in Tree
min(root);
max(root);
// Complete and full and Perfect
if(isFULL(root)){
cout<<"It is Full tree";
}else{
cout<<"Not a full tree";
}
cout<<endl;
if(isPerfect(root)){
cout<<"It is Perfect tree";
}else{
cout<<"Not a Perfect tree";
}
cout<<endl;
int node_count = TotalNodes(root);
int index = 0;
if(isComplete(root,index,node_count)){
cout<<"It is Complete tree";
}else{
cout<<"Not a Complete tree";
}
return 0;}