-
Notifications
You must be signed in to change notification settings - Fork 368
/
Copy pathBinary_Tree_View.c
217 lines (175 loc) · 4.98 KB
/
Binary_Tree_View.c
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
#include <stdio.h>
#include <stdlib.h>
struct Node{
int key;
struct Node *left;
struct Node *right;
};
struct Node *Insert(struct Node *root){
printf("Enter value to be entered : ");
int val;
scanf("%d", &val);
root = (struct Node *)malloc(sizeof(struct Node));
root->key = val;
if (val == -1)
return NULL;
printf("Enter value to left of %d : ", val);
root->left = Insert(root->left);
printf("Enter value to right of %d : ", val);
root->right = Insert(root->right);
return root;
}
void Display(int *V, int size){
for (int i = 0; i < size; i++)
printf("%d ", V[i]);
}
void LeftView(struct Node *root, int level, int *res, int *index){
if (!root)
return;
if (*index == level)
{
res[*index] = root->key;
(*index)++;
}
LeftView(root->left, level + 1, res, index);
LeftView(root->right, level + 1, res, index);
}
void RightView(struct Node *root, int level, int *res, int *index){
if (!root)
return;
if (*index == level){
res[*index] = root->key;
(*index)++;
}
RightView(root->right, level + 1, res, index);
RightView(root->left, level + 1, res, index);
}
void TopView(struct Node *root, int **res, int *size){
if (!root)
return;
int minIdx = 0;
int maxIdx = 0;
struct NodeQueue
{
struct Node *node;
int hd;
};
struct NodeQueue *queue = (struct NodeQueue *)malloc(sizeof(struct NodeQueue) * 1000);
int front = 0, rear = -1;
queue[++rear] = (struct NodeQueue){root, 0};
while (front <= rear){
struct NodeQueue f = queue[front++];
if (f.hd < minIdx)
minIdx = f.hd;
else if (f.hd > maxIdx)
maxIdx = f.hd;
if (f.node->left)
queue[++rear] = (struct NodeQueue){f.node->left, f.hd - 1};
if (f.node->right)
queue[++rear] = (struct NodeQueue){f.node->right, f.hd + 1};
}
*size = maxIdx - minIdx + 1;
*res = (int *)malloc(sizeof(int) * (*size));
for (int i = 0; i < *size; i++)
(*res)[i] = 0;
queue = (struct NodeQueue *)realloc(queue, sizeof(struct NodeQueue) * (rear + 1));
front = 0;
while (front <= rear){
struct NodeQueue f = queue[front++];
if ((*res)[f.hd - minIdx] == 0)
(*res)[f.hd - minIdx] = f.node->key;
}
free(queue);
}
void BottomView(struct Node *root, int **res, int *size){
if (!root)
return;
int minIdx = 0;
int maxIdx = 0;
struct NodeQueue
{
struct Node *node;
int hd;
};
struct NodeQueue *queue = (struct NodeQueue *)malloc(sizeof(struct NodeQueue) * 1000);
int front = 0, rear = -1;
queue[++rear] = (struct NodeQueue){root, 0};
while (front <= rear)
{
struct NodeQueue f = queue[front++];
if (f.hd < minIdx)
minIdx = f.hd;
else if (f.hd > maxIdx)
maxIdx = f.hd;
if (f.node->left)
queue[++rear] = (struct NodeQueue){f.node->left, f.hd - 1};
if (f.node->right)
queue[++rear] = (struct NodeQueue){f.node->right, f.hd + 1};
}
*size = maxIdx - minIdx + 1;
*res = (int *)malloc(sizeof(int) * (*size));
for (int i = 0; i < *size; i++)
(*res)[i] = 0;
queue = (struct NodeQueue *)realloc(queue, sizeof(struct NodeQueue) * (rear + 1));
front = 0;
while (front <= rear){
struct NodeQueue f = queue[front++];
(*res)[f.hd - minIdx] = f.node->key;
}
free(queue);
}
int main(){
struct Node *root = NULL;
printf("Enter binary tree nodes and for no node enter -1\n\n");
root = Insert(root);
int *L = NULL;
int *R = NULL;
int *T = NULL;
int *B = NULL;
int usrchoice = 0;
while (1){
printf("\n\n1.Left View\n2.Right View\n3.Top View\n4.Bottom View\n5.Exit\n");
printf("Enter the choice of operation to be implemented : ");
scanf("%d", &usrchoice);
switch (usrchoice){
case 1:{
L = (int *)malloc(sizeof(int) * 1000);
int lIndex = 0;
LeftView(root, 0, L, &lIndex);
Display(L, lIndex);
free(L);
break;
}
case 2:{
R = (int *)malloc(sizeof(int) * 1000);
int rIndex = 0;
RightView(root, 0, R, &rIndex);
Display(R, rIndex);
free(R);
break;
}
case 3:{
int tSize = 0;
T = (int *)malloc(sizeof(int) * 1000);
TopView(root, &T, &tSize);
Display(T, tSize);
free(T);
break;
}
case 4:{
int bSize = 0;
B = (int *)malloc(sizeof(int) * 1000);
BottomView(root, &B, &bSize);
Display(B, bSize);
free(B);
break;
}
case 5:
exit(1);
default:
printf("Enter a valid choice : ");
break;
}
}
return 0;
}