-
Notifications
You must be signed in to change notification settings - Fork 31
/
Copy pathcode_2.cpp
113 lines (99 loc) · 2.21 KB
/
code_2.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
//
// code_2.cpp
// Algorithm
//
// Created by Mohd Shoaib Rayeen on 15/04/19.
// Copyright © 2019 Shoaib Rayeen. All rights reserved.
//
#include <iostream>
#include <queue>
using namespace std;
struct bnode {
int data;
bnode *left;
bnode *right;
};
class btree {
public:
bnode *root , *root1;
btree();
void insert();
void display();
bnode* newNode(int);
bool isIdentical(bnode* ,bnode*);
};
btree::btree() {
root = NULL;
}
bnode* btree:: newNode(int value) {
bnode* temp=new bnode;
temp->data=value;
temp->left=NULL;
temp->right=NULL;
return temp;
}
void btree:: insert() {
root = newNode(1);
root->left = newNode(-2);
root->right = newNode(3);
root->left->left = newNode(4);
root->left->right = newNode(5);
root->right->left = newNode(-6);
root->right->right = newNode(2);
root1 = newNode(1);
root1->left = newNode(-2);
root1->right = newNode(3);
root1->left->left = newNode(4);
root1->left->right = newNode(5);
root1->right->left = newNode(-6);
root1->right->right = newNode(2);
}
void btree::display() {
insert();
if(isIdentical(root , root1)) {
cout << "\nBoth are identical.\n";
return;
}
cout << "\nBoth are different Trees.\n";
}
bool btree::isIdentical(bnode *root , bnode* root1) {
if (!root1 && !root) {
return true;
}
if (!root1 || !root) {
return false;
}
queue<bnode *> q1, q2;
q1.push(root);
q2.push(root1);
while (!q1.empty() && !q2.empty()) {
bnode *n1 = q1.front();
bnode *n2 = q2.front();
if (n1->data != n2->data) {
return false;
}
q1.pop();
q2.pop();
if (n1->left && n2->left) {
q1.push(n1->left);
q2.push(n2->left);
}
else if (n1->left || n2->left) {
return false;
}
if (n1->right && n2->right) {
q1.push(n1->right);
q2.push(n2->right);
}
else if (n1->right || n2->right) {
return false;
}
}
return true;
}
int main() {
btree obj;
obj.display();
cout << "\n";
return 0;
}