/
Assignment3WithArrays.cpp
282 lines (268 loc) · 6.95 KB
/
Assignment3WithArrays.cpp
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
/*
This Program Creates Expression Tree For valid PostFix Equation
Written By: Muhammad Nadeem Azam
Reg#: 2009176
Assignemnt: 3
Date:03-12-2010
*/
#include<stdio.h>
#include<stdlib.h>
//typedef declarations for Tree Structure
typedef struct TreeNode *TPosition;
typedef struct TreeNode *Tree;
/*
typedef declarations for Stack Structure which is being used for building Expression Tree, for futher
references, just "Expression Stack" term is used in short
*/
typedef struct Exp_StackNode *ExpStack;
typedef struct Exp_StackNode *PtrToExpSNode;
//typedef declarations for Stack Structure which is being used to check validity of the entered PostFix Equation
typedef struct StackNode *Stack;
typedef struct StackNode *PtrToStack;
struct TreeNode //Tree Node
{
char data;
Tree Left;
Tree Right;
};
struct Exp_StackNode //Stack Node(being used for buliding Expression Tree
{
Tree PtrToTreeNode;
PtrToExpSNode next;
};
struct StackNode //Stack Node(being used for Checking Validity of PostFix Equation
{
char data;
PtrToStack next;
};
//Methods being used for building Tree
Tree createTreeNode(char x);
void InOrder(Tree Root);
void PostOrder(Tree Root);
void PreOrder(Tree Root);
//Methods being used for Expression Stack
ExpStack createExpStack(void);
void PushIntoExpStack(TPosition TreePos, ExpStack S);
int IsEmpty_ExpStack(ExpStack S);
TPosition Pop(ExpStack S);
//Methods being used for Stack
Stack createStack(void);
void PushIntoExpStack(char data, Stack S);
int IsEmpty_Stack(Stack S);
void Pop(Stack S);
int PostfixCheck(char equation[]); //This methods Checks whether Postfix Equation is valid or Not
Tree BulidTree(char equation[]); // This method creates Expression Tree for Valid PostFix Equation
int main()
{
Tree T;
printf("\t\t\t\tWelcome!!\n\tThis Program creates Expression tree For PostFix Equation\n\n");
char equation[20];
printf("Enter Expression: ");
scanf("%s",equation);
printf("Your Entered Equation is\n");
printf("\t\t\t\t%s\n",equation);
if(PostfixCheck(equation)==1) //Incase Equation is Valid
{
printf("\t\t\tIt is Valid PostFix Equation\n\tNow We can proceed further to build Exppress Tree for given equation\n\n\n");
T=BulidTree(equation); //Expression Tree Building Method is being called at this stage
printf("\nInfix Equation by InOrder Traversal of Tree:\t\t");
InOrder(T);
printf("\nPostfix Equation by PostOrder Traversal of Tree:\t");
PostOrder(T);
printf("\nPrefix Equation by PreOrder Traversal of Tree:\t\t");
PreOrder(T);
printf("\n\t\t**********************************************\n\t\t\tThanks For Using Program\n\n");
}
else //If Equation is Invalid, Program excuses to build expression Tree and stops
printf("Equation is Invalid\nSorry!!!\n\t\tExpression Tree can't be built for Invalid Postfix Equation\n");
return 0;
}
Tree createTreeNode(char x) //This method creates Single Node for Tree and returns it's pointer
{
Tree T;
T = (struct TreeNode*)malloc(sizeof(struct TreeNode));
if( T == NULL )
printf( "Out of space!!!");
else
{
T->data = x;
T->Left = T->Right = NULL;
}
return T;
}
void InOrder(Tree Root) //InOrder Traversal
{
if (Root !=NULL)
{
InOrder(Root->Left);
printf("%c",Root->data);
InOrder(Root->Right);
}
}
void PostOrder(Tree Root) //POstOrder Traversal
{
if (Root !=NULL)
{
PostOrder(Root->Left);
PostOrder(Root->Right);
printf("%c",Root->data);
}
}
void PreOrder(Tree Root) //PreOrder Traversal
{
if (Root !=NULL)
{
printf("%c",Root->data);
PreOrder(Root->Left);
PreOrder(Root->Right);
}
}
ExpStack createExpStack(void) //This method creates Expression Stack
{
PtrToExpSNode NewNode;
NewNode=(struct Exp_StackNode*)malloc(sizeof(struct Exp_StackNode));
NewNode->PtrToTreeNode=NULL;
NewNode->next=NULL;
return NewNode;
}
void PushIntoExpStack(TPosition TreePos, ExpStack S) // This Method pushes TreeNode pointer into Expression Stack
{
PtrToExpSNode TmpCell;
TmpCell=(struct Exp_StackNode*)malloc(sizeof(struct Exp_StackNode));
if (TmpCell== NULL)
{
printf("Out of Space!");
}
else
{
TmpCell->PtrToTreeNode=TreePos;
TmpCell->next= S->next;
S->next= TmpCell;
}
}
int IsEmpty_ExpStack(ExpStack S) //This Method returns 0 if Expression Stack is empty otherwise 1
{
if(S->next== NULL)
{
return 0;
}
else
return 1;
}
TPosition Pop_ExpStack(ExpStack S) //This Method Pops out TreeNode pointer at Top of Expression Stack
{
PtrToExpSNode TmpCell;
TmpCell= S->next;
if (IsEmpty_ExpStack(S)!=1)
{
printf("Empty Stack");
return NULL;
}
else
{
S->next= S->next->next;
return TmpCell->PtrToTreeNode;
}
free(TmpCell); //freeing the space
}
Stack createStack(void) //creates stack
{
PtrToStack NewNode;
NewNode=(struct StackNode*)malloc(sizeof(struct StackNode));
NewNode->data=NULL;
NewNode->next=NULL;
return NewNode;
}
void PushIntoStack(char data, Stack S) //pushes character data into stack
{
PtrToStack TmpCell;
TmpCell=(struct StackNode*)malloc(sizeof(struct StackNode));
if (TmpCell== NULL)
{
printf("Out of Space!");
}
else
{
TmpCell->data=data;
TmpCell->next= S->next;
S->next= TmpCell;
}
}
int IsEmpty_Stack(Stack S) //returns 0 incase stack is empty otherwise 1
{
if(S->next== NULL)
{
return 0;
}
else
return 1;
}
void Pop_Stack(Stack S) //Just deletes node at top, without returning anything
{
PtrToStack TmpCell;
TmpCell= S->next;
if (IsEmpty_Stack(S)!=1)
{
printf("Empty Stack");
}
else
{
S->next= S->next->next;
}
free(TmpCell);
}
int PostfixCheck(char equation[]) //This method checks either Postfix Equation is valid or not, incase of valid, it returns 1
{
int i;
Stack S;
S=createStack();
for(i=0;equation[i]!='\0';i++)
{
if(equation[i]!='+' && equation[i]!='-' && equation[i]=='/' && equation[i]=='*')
{
PushIntoStack(equation[i],S);
}
else if(equation[i]=='+' || equation[i]=='-' || equation[i]=='/' || equation[i]=='*')
{
Pop_Stack(S);
Pop_Stack(S);
PushIntoStack(equation[i],S);
}
else
PushIntoStack(equation[i],S);
}
Pop_Stack(S);
if(IsEmpty_Stack(S)==0)
return 1;
else
return 0;
}
Tree BulidTree(char equation[]) //Builds Expression Tree
{
Tree T[20];
int i;
ExpStack S;
S=createExpStack();
for(i=0;equation[i]!='\0';i++)
{
if(equation[i]!='+' && equation[i]!='-' && equation[i]!='/' && equation[i]!='*')
{
T[i]=createTreeNode(equation[i]);
PushIntoExpStack(T[i],S);
}
else if(equation[i]=='+' || equation[i]=='-' || equation[i]=='/' || equation[i]=='*')
{
T[i]=createTreeNode(equation[i]);
T[i]->Right=Pop_ExpStack(S);
T[i]->Left=Pop_ExpStack(S);
PushIntoExpStack(T[i],S);
}
else
{
T[i]=createTreeNode(equation[i]);
PushIntoExpStack(T[i],S);
}
}
printf("Expression Tree has been successfully built!!!\n");
return Pop_ExpStack(S); //returns Root of newly built Expression Tree
}