-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMirrorLinux.c
More file actions
320 lines (303 loc) · 5.21 KB
/
Copy pathMirrorLinux.c
File metadata and controls
320 lines (303 loc) · 5.21 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
308
309
310
311
312
313
314
315
316
317
318
319
320
#include<stdio.h>
#include<malloc.h>
#define MAX 100
typedef struct BinaryTree
{
struct BinaryTree *left;
int data;
struct BinaryTree *right;
}Btree;
typedef struct queue
{
int front,rear;
Btree *item[MAX];
}Queue;
Queue Q;
int countNode;
Btree* getNode();
void insert(Btree **,int,int); //creates Binary Search tree
//---------------------------------------------------------------------------------
//displays tree in inorder manner
void inOrder(Btree *root)
{
if(root!=NULL)
{
inOrder(root->left);
printf("%d ",root->data);
inOrder(root->right);
}
}
//displays tree in preorder manner
void preOrder(Btree *root)
{
if(root!=NULL)
{
printf("%d ",root->data);
preOrder(root->left);
preOrder(root->right);
}
}
//displays tree in postorder manner
void postOrder(Btree *root)
{
if(root!=NULL)
{
postOrder(root->left);
postOrder(root->right);
printf("%d ",root->data);
}
}//------------------------/
void makeMirror(Btree *,Btree **); //creates mirror of * into **
void levelWise(Btree *,int);
void countLeafNodes(Btree *);
/*-------------------------Queue operating functions*/
void initQ()
{
int i;
Q.front=Q.rear=-1;
for(i=0; i<MAX; i++)
Q.item[i]=NULL;
}
void addQ(Btree *temp) //adds element to the queue
{
if((Q.rear==MAX-1 && Q.front==0) || (Q.rear+1==Q.front))
{
printf("\nQueue is full!!!!");
return;
}
if(Q.rear==MAX-1)
Q.rear=0;
else
Q.rear++;
Q.item[Q.rear]=temp;
if(Q.front==-1)
Q.front=0;
}
Btree *delQ() //deletes element from queue
{
Btree *temp;
if(Q.front==-1)
{
printf("\nQueue is empty !!!");
return NULL;
}
temp=Q.item[Q.front];
Q.item[Q.front]=NULL;
if(Q.front==Q.rear)
{
Q.front=-1;
Q.rear=-1;
}
else
{
if(Q.front==MAX-1)
Q.front=0;
else
Q.front++;
}
return temp;
}
int isQEmpty() //used to check whether queue is empty or not
{
return(Q.front==-1);
}
void
main()
{
int ch,d;
Btree *root=NULL,*mirror=NULL;
initQ();
do{
printf("\n\t\t\t0.Exit");
printf("\n\t\t\t1.Insert into tree");
printf("\n\t\t\t2.Inorder display");
printf("\n\t\t\t3.Preoder display");
printf("\n\t\t\t4.Postorder display");
printf("\n\t\t\t5.Mirror");
printf("\n\t\t\t6.Height");
printf("\n\t\t\t7.Leaf node");
printf("\n\t\t\t8.Display Original and Mirror levelwise");
printf("\n\t\t\tYour Choice : ");
scanf("%d",&ch);
switch(ch)
{
case 1:
printf("\nEnter value in tree : ");
scanf("%d",&d);
insert(&root,d,0);
break;
case 2:
printf("\n");
inOrder(root);
break;
case 3:
printf("\n");
preOrder(root);
break;
case 4:
printf("\n");
postOrder(root);
break;
case 5:
mirror=NULL;
makeMirror(root,&mirror);
printf("\n");
inOrder(root);
printf("<--Mirror Image--> ");
inOrder(mirror);
printf("\n");
break;
case 6:
initQ();
levelWise(root,1);
break;
case 7:
countNode=0;
countLeafNodes(root);
if(countNode==1)
printf("\n%d leaf node found.",countNode);
else
printf("\n%d leaf nodes found.",countNode);
break;
case 8:
printf("\nOriginal Tree");
levelWise(root,0);
printf("\n\nMirror Tree");
levelWise(mirror,0);
break;
}
}while(ch!=0);
}
//allocates memory and returns address of it
Btree *getNode()
{
Btree *n;
n=(Btree *)malloc(sizeof(Btree));
n->left=NULL;
n->data=0;
n->right=NULL;
return n;
}
//creates binary tree in with original & reverse logic
void insert(Btree **root,int d,int dir)
{
Btree *temp,*temp1,*n;
n=getNode();
n->data=d;
if(*root==NULL)
{
(*root)=n;
}
//creates tree like SMALL VALUE<-ROOT VAL->BIG VALUE
else if(dir==0)
{
temp=*root;
while(temp!=NULL)
{
if(d==temp->data)
{
printf("\n%d already exist.",d);
free(n);
return;
}
else if(d < temp->data)
{
temp1=temp;
temp=temp->left;
}
else
{
temp1=temp;
temp=temp->right;
}
}
if(d < temp1->data)
temp1->left=n;
else
temp1->right=n;
}
else //creates tree like BIG VALUE<-ROOT VAL->SMALL VALUE
{
temp=*root;
while(temp!=NULL)
{
if(d > temp->data)
{
temp1=temp;
temp=temp->left;
}
else
{
temp1=temp;
temp=temp->right;
}
}
if(d > temp1->data)
temp1->left=n;
else
temp1->right=n;
}
}
//creates mirror of Btree * into Btree **
void makeMirror(Btree *root,Btree **m)
{
if(root!=NULL)
{
insert(m,root->data,1);
makeMirror(root->left,m);
makeMirror(root->right,m);
}
}
void levelWise(Btree *root,int option)
{
int level=0;
Btree *temp=root,*dummy=NULL;
if(temp==NULL)
{
printf("\nTree is empty.");
return;
}
addQ(temp);
addQ(dummy);
if(option==0)
printf("\nLevel %d : ",level);
do
{
temp=delQ();
if(option==0)
if(temp!=dummy)
printf("%6d ",temp->data);
if(temp==dummy)
{
if(!isQEmpty())
{
level++;
if(option==0)
printf("\nLevel %d : ",level);
addQ(dummy);
}
}
else
{
if(temp->left)
addQ(temp->left);
if(temp->right)
addQ(temp->right);
}
}while(!isQEmpty());
if(option==1)
printf("\nThe height of tree : %d",level+1);
}
void countLeafNodes(Btree *root)
{
if(root!=NULL)
{
countLeafNodes(root->left);
if(root->left==NULL && root->right==NULL)
{
printf("%d ",root->data);
countNode++;
}
countLeafNodes(root->right);
}
}