-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbinaryTree.cpp
More file actions
281 lines (240 loc) · 5.97 KB
/
binaryTree.cpp
File metadata and controls
281 lines (240 loc) · 5.97 KB
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
#include<iostream>
using namespace std;
#include<queue>
class binaryTreeNode{
public:
int data;
binaryTreeNode *left;
binaryTreeNode*right;
binaryTreeNode(int data){
this->data=data;
left=NULL;
right=NULL;
}
~binaryTreeNode(){
delete left;
delete right;
}
};
void printTree(binaryTreeNode*root){
if(root==NULL){
return;
}
else{
cout<<root->data<<": ";
if(root->left!=NULL){
cout<<"L"<<root->left->data<<" ";
}
if(root->right!=NULL){
cout<<"R"<<root->right->data<<" ";
}
cout<<endl;
printTree(root->left);
printTree(root->right);
}
}
binaryTreeNode* takeInput(){
int data;
cout<<"Enter Data"<<endl;
cin>>data;
if(data==0){
return NULL;
}
binaryTreeNode*root=new binaryTreeNode(data);
cout<<"Enter left child of "<<root->data<<" :"<<endl;
root->left=takeInput();
cout<<"Enter right child of "<<root->data<<" :"<<endl;
root->right=takeInput();
}
binaryTreeNode* takeInputLvL(){
queue<binaryTreeNode*>helper;
int data;
cout<<"Enter Data"<<endl;
cin>>data;
binaryTreeNode*node=new binaryTreeNode(data);
helper.push(node);
while(!helper.empty()){
binaryTreeNode*temp=helper.front();
cout<<"Enter left child of "<<temp->data<<" "<<endl;
int x;
cin>>x;
if(x!=-1){
binaryTreeNode*newleftnode=new binaryTreeNode(x);
helper.push(newleftnode);
temp->left=newleftnode;
}
cout<<"Enter right child of "<<temp->data<<" "<<endl;
int y;
cin>>y;
if(y!=-1){
binaryTreeNode*newrightnode=new binaryTreeNode(y);
helper.push(newrightnode);
temp->right=newrightnode;
}
helper.pop();
}
return node;
}
int countNodes(binaryTreeNode*root){
if(root==NULL){
return 0;
}
int smallpart=countNodes(root->left)+ countNodes(root->right);
return smallpart+1;
}
//Something went wrong
bool findNode(binaryTreeNode*root,int nodeData){
if(root==NULL){
return false;
}
if(root->data==nodeData){
return true;
}
bool small1=root->left;
bool small2=root->right;
// if(small1==true || small2==true){
// return true;
// }
// else{
// return false;
// }
if(small1==false && small2==false){
return false;
}
else{
return true;
}
}
int heightTree(binaryTreeNode*root){
if (root==NULL){
return 0;
}
int small1=heightTree(root->left);
int small2=heightTree(root->right);
if(small1>=small2){
return small1+1;
}
else{
return small2+1;
}
}
void mirror(binaryTreeNode*root){
if(root==NULL){
return;
}
binaryTreeNode*temp=root->left;
root->left=root->right;
root->right=temp;
mirror(root->left);
mirror(root->right);
}
void inorderTraversal(binaryTreeNode *root)
{
if (root == NULL)
{
return;
}
inorderTraversal(root->left);
cout << root->data << " ";
inorderTraversal(root->right);
}
void preorderTraversal(binaryTreeNode *root)
{
if (root == NULL)
{
return;
}
cout << root->data << " ";
inorderTraversal(root->left);
inorderTraversal(root->right);
}
void postorderTraversal(binaryTreeNode *root)
{
if (root == NULL)
{
return;
}
inorderTraversal(root->left);
inorderTraversal(root->right);
cout << root->data << " ";
}
binaryTreeNode* constructFromInandPreOrder(int in[],int pre[],int inst,int inend,int prest,int preend){
if(inst>inend){
return NULL;
}
int rootdata=pre[prest];
// binaryTreeNode*root;
// root->data=pre[prest];
int linst=inst;
int lprest=prest+1;
int rootindex=-1;
for(int i=inst;i<=inend;i++){
if(in[i]==rootdata){
rootindex=i;
break;
}
}
int linend=rootindex-1;
int lpreend=(linend-linst)+lprest;
int rinst=rootindex+1;
int rinend=inend;
int rprest=lpreend+1;
int rpreend=preend;
binaryTreeNode*root=new binaryTreeNode(rootdata);
root->left=constructFromInandPreOrder(in,pre,linst,linend,lprest,lpreend);
root->right=constructFromInandPreOrder(in,pre,rinst,rinend,rprest,rpreend);
return root;
}
int diametre(binaryTreeNode*root){
if(root==NULL){
return 0;
}
int LplusR=heightTree(root->left)+heightTree(root->right);
int smallpart1=diametre(root->left);
int smallpart2=diametre(root->right);
return max(LplusR,max(smallpart1,smallpart2));
}
pair<int,int> diameterHeight(binaryTreeNode*root){
if(root==NULL){
pair<int,int> p;
p.first=0;
p.second=0;
return p;
}
pair<int,int>leftAns=diameterHeight(root->left);
pair<int,int>rightAns=diameterHeight(root->right);
int lh=leftAns.first;
int ld=leftAns.second;
int rh=rightAns.first;
int rd=rightAns.second;
int height=1+max(lh,rh);
int diameter=max(lh+rh,max(ld,rd));
pair<int,int>p;
p.first=height;
p.second=diameter;
return p;
}
int main(){
// binaryTreeNode*root= takeInput();
// binaryTreeNode*root= takeInputLvL();
// printTree(root);
// // cout<<"NUMBER OF NODES : "<<countNodes(root)<<endl;
// // cout<<boolalpha<<findNode(root,8);
// cout<<"Height of tree "<<heightTree(root)<<endl;
// mirror(root);
// printTree(root);
// inorderTraversal(root);
// int in[]={4,2,5,1,8,6,9,3,7};
// int pre[]={1,2,4,5,3,6,8,9,7};
// binaryTreeNode*root=constructFromInandPreOrder(in,pre,0,8,0,8);
// printTree(root);
binaryTreeNode*root=takeInputLvL();
printTree(root);
// pair<int,int>p=diameterHeight(root);
// cout<<"Success"<<endl;
// cout<<"Height of tree is :"<<p.first<<endl;
// cout<<"Diameter of tree is :"<<p.second<<endl;
cout<<boolalpha<<findNode(root,1)<<endl;
cout<<boolalpha<<findNode(root,11)<<endl;
}
// 1 2 4 -1 3 5 6 -1 -1 7 -1 -1 -1 -1 -1