forked from dharmanshu9930/Website
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Tree.cpp
180 lines (155 loc) · 3.15 KB
/
Tree.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
#include <bits/stdc++.h>
using namespace std;
class Node
{
public:
int data;
Node *left;
Node *right;
Node(int d)
{
this->data = d;
this->left = NULL;
this->right = NULL;
}
};
Node *buildTree(Node *root)
{
int data;
cout << "Enter the data : " << endl;
cin >> data;
root = new Node(data);
if (data == -1)
{
return NULL;
}
cout << "Enter the data to the left of the root: " << endl;
root->left = buildTree(root->left);
cout << "Enter the data to the right of the tree: " << endl;
root->right = buildTree(root->right);
return root;
}
void InorderTraversal(Node *root)
{
if (root == NULL)
{
return;
}
InorderTraversal(root->left);
cout << root->data << endl;
InorderTraversal(root->right);
}
void PostorderTraversal(Node *root)
{
if (root == NULL)
{
return;
}
PostorderTraversal(root->left);
PostorderTraversal(root->right);
cout << root->data << endl;
}
void PreorderTraversal(Node *root)
{
if (root == NULL)
{
return;
}
cout << root->data << endl;
PreorderTraversal(root->left);
PreorderTraversal(root->right);
}
void levelOrderTraversal(Node *root)
{
queue<Node *> q;
q.push(root);
while (!q.empty())
{
Node *temp = q.front();
cout << temp->data << " ";
q.pop();
if (temp->left != NULL)
{
q.push(temp->left);
}
if (temp->right != NULL)
{
q.push(temp->right);
}
}
}
int height(Node *root)
{
if (root == NULL)
{
return 0;
}
int left = height(root->left);
int right = height(root->right);
int ans = max(left, right) + 1;
return ans;
}
int diameter(Node *root)
{
if (root == NULL)
{
return 0;
}
int op1 = diameter(root->left);
int op2 = diameter(root->right);
int op3 = height(root->left) + 1 + height(root->right);
int ans = max(op1, max(op2, op3));
return ans;
}
bool isBalanced(Node *root)
{
if (root == NULL)
{
return true;
}
bool left = isBalanced(root->left);
bool right = isBalanced(root->right);
bool diff = abs(height(root->left) - height(root->right)) <= 1;
if (left && right && diff)
{
return true;
}
else
{
return false;
}
}
bool isIdentical(Node *r1, Node *r2)
{
if (r1 == NULL && r2 == NULL)
{
return true;
}
if (r1 == NULL && r2 != NULL)
{
return false;
}
if (r1 != NULL && r2 == NULL)
{
return false;
}
bool left = isIdentical(r1->left, r2->left);
bool right = isIdentical(r2->left, r2->right);
bool ans = r1->data == r2->data;
if (left && right && ans)
{
return true;
}
else
{
return false;
}
}
int main()
{
Node *root = NULL;
root = buildTree(root);
PreorderTraversal(root);
levelOrderTraversal(root);
return 0;
}