-
Notifications
You must be signed in to change notification settings - Fork 17
/
Copy pathds_assign8_17.c
224 lines (197 loc) · 4.69 KB
/
ds_assign8_17.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
218
219
220
221
222
223
224
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
// Anirudh Sharma 2K20/SE/17
struct TreeNode
{
char info;
struct TreeNode *left, *right;
};
int isDigit(char ch)
{
if (ch > 47 && ch < 57)
return 1;
return 0;
}
int isOperator(char ch)
{
if (ch == '+' || ch == '-' || ch == '*' || ch == '/' || ch == '%' || ch == '^')
return 1;
return 0;
}
struct TreeNode *Create_Node(char info)
{
struct TreeNode *temp = (struct TreeNode *)malloc(sizeof(struct TreeNode));
if (temp == NULL)
{
printf("Out of space!\n");
return (temp);
}
temp->left = NULL;
temp->right = NULL;
temp->info = info;
return temp;
};
struct TreeNode *constructTree(struct TreeNode *tree, char postfix[])
{
int i = 0;
struct TreeNode *st[100];
int top = -1;
struct TreeNode *temp_tree1;
struct TreeNode *temp_tree2;
while (postfix[i] != '\0')
{
if (!(postfix[i] == '+' || postfix[i] == '-' || postfix[i] == '*' || postfix[i] == '/' || postfix[i] == '%' || postfix[i] == '^'))
{
tree = Create_Node(postfix[i]);
top = top + 1;
st[top] = tree;
}
else
{
tree = Create_Node(postfix[i]);
temp_tree1 = st[top];
top = top - 1;
temp_tree2 = st[top];
top = top - 1;
tree->right = temp_tree1;
tree->left = temp_tree2;
top = top + 1;
st[top] = tree;
}
i++;
}
return tree;
}
// Function to Show Node
void show_node(struct TreeNode *T)
{
printf("%c ", T->info);
}
// Function for PreOrder Traversal
void preorder(struct TreeNode *T)
{
if (T != NULL)
{
show_node(T);
preorder(T->left);
preorder(T->right);
}
}
// Function for PostOrder Traversal
void postorder(struct TreeNode *T)
{
if (T != NULL)
{
postorder(T->left);
postorder(T->right);
show_node(T);
}
}
// Display Function
void display(struct TreeNode *tree, char postfix[])
{
printf("\n\t*************TRAVERSALS*************\n");
printf("\nPreOrder Traverse (Parent, Left, Right) Postfix Expression : ");
preorder(tree);
printf("\nPostOrder Traverse (Left, Right, Parent) Prefix Expression : ");
postorder(tree);
}
// Function to sort Priority
int getPriority(char ch)
{
switch (ch)
{
case '^':
return 4;
case '%':
return 3;
case '/':
case '*':
return 2;
case '+':
case '-':
return 1;
default:
return 0;
}
}
// Function to convert Infix Expression to Postfix Expression
void Convert_In_To_Post(char infix[], char postfix[])
{
unsigned int counter1 = 0;
char st[100];
int top = -1;
int postCount = 0;
char element;
while (counter1 < strlen(infix))
{
element = infix[counter1];
if (element == '(')
{
top = top + 1;
st[top] = element;
counter1++;
continue;
}
if (element == ')')
{
while (top != -1 && st[top] != '(')
{
postfix[postCount++] = st[top];
top = top - 1;
;
}
if (top != -1)
{
top = top - 1;
}
counter1++;
continue;
}
if (getPriority(element) == 0)
{
postfix[postCount++] = element;
}
else
{
if (top == -1)
{
top = top + 1;
st[top] = element;
}
else
{
while (top != -1 && st[top] != '(' &&
getPriority(element) <= getPriority(st[top]))
{
postfix[postCount++] = st[top];
top = top - 1;
}
top = top + 1;
st[top] = element;
}
}
counter1++;
}
while (top != -1)
{
postfix[postCount++] = st[top];
top = top - 1;
}
postfix[postCount] = '\0';
}
// Driver Code
int main()
{
printf("\n Enter Expression: ");
char infix[100];
gets(infix);
char *postfix = (char *)malloc(sizeof(char) * strlen(infix));
Convert_In_To_Post(infix, postfix);
struct TreeNode *tree1 = NULL;
tree1 = constructTree(tree1, postfix);
display(tree1, postfix);
printf("\n");
return 0;
}