-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcompleteBinaryTrees.cpp
More file actions
307 lines (286 loc) · 6.02 KB
/
completeBinaryTrees.cpp
File metadata and controls
307 lines (286 loc) · 6.02 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
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
#include<iostream>
#include<queue>
using namespace std;
class binaryTreeNode{
public:
int data;
binaryTreeNode* left;
binaryTreeNode* right;
binaryTreeNode(int data)
{
this->data=data;
left=NULL;
right=NULL;
}
~binaryTreeNode()
{
delete left;
delete right;
}
};
// Take Input recursive
binaryTreeNode* takeInput()
{
int data;
cout<<"enter data "<<endl;
cin>>data;
if(data==-1)
{
return NULL;
}
binaryTreeNode*root=new binaryTreeNode(data);
cout<<"enter left child of "<<root->data<<endl;
binaryTreeNode*lchild=takeInput();
cout<<"enter right child of "<<root->data<<endl;
binaryTreeNode* rchild=takeInput();
root->left=lchild;
root->right=rchild;
return root;
}
// Print Recursive
void print(binaryTreeNode* root)
{
if(root==NULL)
{
return ;
}
cout<<root->data<<": ";
if(root->left!=NULL)
{
cout<<"L "<<root->left->data<<",";
}
if(root->right!=NULL)
{
cout<<"R "<<root->right->data<<",";
}
cout<<endl;
print(root->left);
print(root->right);
}
// Function to takeInput Level Wise
binaryTreeNode* takeInputLevelWise()
{
int rootdata;
cout<<"enter root data "<<endl;
cin>>rootdata;
if(rootdata==-1)
{
return NULL;
}
binaryTreeNode*root=new binaryTreeNode(rootdata);
queue<binaryTreeNode*>q;
q.push(root);
while(q.size()!=0)
{
binaryTreeNode*front=q.front();
q.pop();
cout<<"enter leftchild of "<<front->data<<endl;
int lchildval;
cin>>lchildval;
if(lchildval!=-1)
{
binaryTreeNode* lchild=new binaryTreeNode(lchildval);
q.push(lchild);
front->left=lchild;
}
cout<<"enter rightchild of "<<front->data<<endl;
int rchildval;
cin>>rchildval;
if(rchildval!=-1)
{
binaryTreeNode* rchild=new binaryTreeNode(rchildval);
q.push(rchild);
front->right=rchild;
}
}
return root;
}
//Function to print level wise
void printLevelWise(binaryTreeNode* root)
{
queue<binaryTreeNode*> q;
q.push(root);
while(q.size()!=0)
{
binaryTreeNode* front=q.front();
q.pop();
cout<<front->data<<": ";
if(front->left!=NULL)
{
cout<<"L "<<front->left->data<<",";
q.push(front->left);
}
if(front->right!=NULL)
{
cout<<"R "<<front->right->data<<",";
q.push(front->right);
}
cout<<endl;
}
}
// Find height of a tree
int height(binaryTreeNode *root)
{
if(root==NULL)
{
return -1;
}
int lheight=height(root->left);
int rheight=height(root->right);
int ans=max(lheight,rheight);
return ans+1;
}
//total nodes in tree
int nodes(binaryTreeNode *root)
{
int ans=0;
if(root==NULL)
{
return 0;
}
ans+=nodes(root->left);
ans+=nodes(root->right);
return ans+1;
}
//preorder traversal
void preorder(binaryTreeNode *root)
{
if(root==NULL)
{
return ;
}
cout<<root->data<<" ";
preorder(root->left);
preorder(root->right);
}
//inorder traversal
void inorder(binaryTreeNode *root)
{
if(root==NULL)
{
return ;
}
inorder(root->left);
cout<<root->data<<" ";
inorder(root->right);
}
//postorder traversal
void postorder(binaryTreeNode *root)
{
if(root==NULL)
{
return ;
}
postorder(root->left);
postorder(root->right);
cout<<root->data<<" ";
}
//Helper function to Build tree from inorder and preorder
binaryTreeNode* buildTreeHelper(int*pr, int*in,int pres,int pree,int ins,int ine)
{
if(ins>ine)
{
return NULL;
}
int rootData=pr[pres];
int rootIndex=-1;
for(int i=ins;i<=ine;i++)
{
if(rootData==in[i])
{
rootIndex=i;
break;
}
}
int lins=ins;
int line=rootIndex-1;
int rins=rootIndex+1;
int rine=ine;
int lprs=pres+1;
int lpre=line-lins+lprs;
int rprs=lpre+1;
int rpre=pree;
binaryTreeNode* root=new binaryTreeNode(rootData);
binaryTreeNode* lchild=buildTreeHelper(pr,in,lprs,lpre,lins,line);
binaryTreeNode* rchild=buildTreeHelper(pr,in,rprs,rpre,rins,rine);
root->left=lchild;
root->right=rchild;
return root;
}
//Build tree from inorder and preorder
binaryTreeNode* buildTree(int pr[],int in[],int size)
{
return buildTreeHelper(pr,in,0,size-1,0,size-1);
}
//Function to find dimaeter of tree in O(N*H) where H is height of tree
int diameter(binaryTreeNode* root)
{
if(root==NULL)
{
return 0;
}
int lheight=height(root->left);
int rheight=height(root->right);
int option1=1+lheight+rheight;
int option2=diameter(root->left);
int option3=diameter(root->right);
int ans=max(option1,max(option2,option3));
return ans+1;
}
class pair1{
public:
int diamater;
int height;
};
// function to find dimater of tree in O(h) where h is height of tree
pair1 heightDiam(binaryTreeNode* root)
{
if(root==NULL)
{
pair1 ans;
ans.diamater=0;
ans.height=0;
return ans;
}
pair1 lpair=heightDiam(root->left);
pair1 rpair=heightDiam(root->right);
int lh=lpair.height;
int rh=rpair.height;
int ld=lpair.diamater;
int rd=rpair.diamater;
int height=1+max(lh,rh);
int diam=max(lh+rh,max(ld,rd));
pair1 ans;
ans.diamater=diam;
ans.height=height;
return ans;
}
//TreeInput: 10 20 30 40 50 60 70 -1 -1 80 90 -1 100 -1 -1 110 -1 -1 -1 -1 -1 -1 -1
// inorder: 40 20 110 80 50 90 10 60 100 30 70
//preorder: 10 20 40 50 80 110 90 30 60 100 70
//postorder: 40 110 80 90 50 20 100 60 70 30 10
int main()
{
binaryTreeNode*root=takeInputLevelWise();
printLevelWise(root);
cout<<"Height of binary tree edges : "<<height(root)<<endl;
cout<<"Total Nodes in binary tree : "<<nodes(root)<<endl;
cout<<"Preorder : ";
preorder(root);
cout<<endl<<"Inorder : ";
inorder(root);
cout<<endl<<"Postorder : ";
postorder(root);
cout<<endl;
int in[]={40, 20, 110, 80, 50, 90, 10, 60, 100, 30, 70};
int pre[]={10, 20, 40, 50, 80, 110, 90, 30, 60, 100, 70};
int n=sizeof(pre)/sizeof(pre[0]);
cout<<n<<endl;
binaryTreeNode*root1=buildTree(pre,in,n);
printLevelWise(root1);
cout<<endl;
cout<<"diameter: "<<diameter(root)<<endl;
pair1 ans=heightDiam(root);
cout<<"diameter: "<<ans.diamater<<endl;
cout<<"height: "<<ans.height<<endl;
}