In Fix to Post Fix
-
Upload
ajay-prakash -
Category
Documents
-
view
89 -
download
0
Transcript of In Fix to Post Fix
![Page 1: In Fix to Post Fix](https://reader035.fdocuments.net/reader035/viewer/2022081718/54fb76224a7959434c8b4d35/html5/thumbnails/1.jpg)
Infix to Postfix Conversion
Course : Data StructuresCourse Code : CSE-203
![Page 2: In Fix to Post Fix](https://reader035.fdocuments.net/reader035/viewer/2022081718/54fb76224a7959434c8b4d35/html5/thumbnails/2.jpg)
Infix to Postfix Conversion
Need to handle operator precedence Need to handle parentheses Need to handle left-to-right association Input: Infix with variables, integers,
operators, parentheses (no exponentiation for now)
Output: Postfix form of same expression
![Page 3: In Fix to Post Fix](https://reader035.fdocuments.net/reader035/viewer/2022081718/54fb76224a7959434c8b4d35/html5/thumbnails/3.jpg)
Infix Precedence
Parenthesis () Exponentiation ^ Multiplication *, Division / Addition +, Subtraction -
![Page 4: In Fix to Post Fix](https://reader035.fdocuments.net/reader035/viewer/2022081718/54fb76224a7959434c8b4d35/html5/thumbnails/4.jpg)
We will use a stack to convert infix expression to its equivalent postfix expression
In computer science, a stack is an abstract data type and data structure based on the principle of
Last In First Out (LIFO).
![Page 5: In Fix to Post Fix](https://reader035.fdocuments.net/reader035/viewer/2022081718/54fb76224a7959434c8b4d35/html5/thumbnails/5.jpg)
Psuedocode
![Page 6: In Fix to Post Fix](https://reader035.fdocuments.net/reader035/viewer/2022081718/54fb76224a7959434c8b4d35/html5/thumbnails/6.jpg)
Get the input of an infix expression into an array
tos=0 (tos=top of the stack)
for (i=0;i<=arraySize;i++)
{ if array[i]is an operand: Append array[i] to the postfix expression break; if array[i]is an operator: if (tos==0) push array[i] into the stack tos++; if (tos>0) { for(j=tos;j>=0;j--){ compare the precedence of array[i] and stack[j]; if array[i] is an operator of greater or equal precedence pop stack[j] and append it to the postfix expression; tos--; else break; } then push array[i] into the stack tos++; break; }
![Page 7: In Fix to Post Fix](https://reader035.fdocuments.net/reader035/viewer/2022081718/54fb76224a7959434c8b4d35/html5/thumbnails/7.jpg)
if array[i] is '(' :
push array[i] into the stack
tos++;
if array[i] is ')' :
for(k=tos;k>=0;k--)
{
if ( stack[k]!=’(’ ) pop stack[k] and append it to the postfix expression;
tos --;
if ( stack==’(’ ) tos --;
break;
}
if array[i]is NULL:
for(l=tos;l>=0;l--)
{
pop stack[l] and append it to the postfix expression;
}
}
![Page 8: In Fix to Post Fix](https://reader035.fdocuments.net/reader035/viewer/2022081718/54fb76224a7959434c8b4d35/html5/thumbnails/8.jpg)
Infix input
a*b + (b-c)/g * ( (a+d) * (b-c/f) )
Lets start converting it to its equivalent postfix
form using a stack.
![Page 9: In Fix to Post Fix](https://reader035.fdocuments.net/reader035/viewer/2022081718/54fb76224a7959434c8b4d35/html5/thumbnails/9.jpg)
infix expression
postfix expression
a*b + (b-c)/g * ( (a+d) * (b-c/f) )
Infix to postfix conversion
![Page 10: In Fix to Post Fix](https://reader035.fdocuments.net/reader035/viewer/2022081718/54fb76224a7959434c8b4d35/html5/thumbnails/10.jpg)
infix expression
postfix expression
*b + (b-c)/g * ( (a+d) * (b-c/f) )
a
Infix to postfix conversion
![Page 11: In Fix to Post Fix](https://reader035.fdocuments.net/reader035/viewer/2022081718/54fb76224a7959434c8b4d35/html5/thumbnails/11.jpg)
infix expression
postfix expression
b + (b-c)/g * ( (a+d) * (b-c/f) )
*
a
Infix to postfix conversion
![Page 12: In Fix to Post Fix](https://reader035.fdocuments.net/reader035/viewer/2022081718/54fb76224a7959434c8b4d35/html5/thumbnails/12.jpg)
infix expression
postfix expression
+ (b-c)/g * ( (a+d) * (b-c/f) )
*
a b
Infix to postfix conversion
![Page 13: In Fix to Post Fix](https://reader035.fdocuments.net/reader035/viewer/2022081718/54fb76224a7959434c8b4d35/html5/thumbnails/13.jpg)
infix expression
postfix expression
(b-c)/g * ( (a+d) * (b-c/f) )
+
a b *
Infix to postfix conversion
![Page 14: In Fix to Post Fix](https://reader035.fdocuments.net/reader035/viewer/2022081718/54fb76224a7959434c8b4d35/html5/thumbnails/14.jpg)
b-c)/g * ( (a+d) * (b-c/f) )
infix expression
postfix expression
+
a b *
(
Infix to postfix conversion
![Page 15: In Fix to Post Fix](https://reader035.fdocuments.net/reader035/viewer/2022081718/54fb76224a7959434c8b4d35/html5/thumbnails/15.jpg)
infix expression
postfix expression
-c)/g * ( (a+d) * (b-c/f) )
+
a b * b
(
Infix to postfix conversion
![Page 16: In Fix to Post Fix](https://reader035.fdocuments.net/reader035/viewer/2022081718/54fb76224a7959434c8b4d35/html5/thumbnails/16.jpg)
infix expression
postfix expression
+
a b * b
Infix to postfix conversion
c)/g * ( (a+d) * (b-c/f) )
(
-
![Page 17: In Fix to Post Fix](https://reader035.fdocuments.net/reader035/viewer/2022081718/54fb76224a7959434c8b4d35/html5/thumbnails/17.jpg)
infix expression
postfix expression
)/g * ( (a+d) * (b-c/f) )
a b * b c
(
Infix to postfix conversion
-
+
![Page 18: In Fix to Post Fix](https://reader035.fdocuments.net/reader035/viewer/2022081718/54fb76224a7959434c8b4d35/html5/thumbnails/18.jpg)
infix expression
postfix expression
/g * ( (a+d) * (b-c/f) )
a b * b c -
Infix to postfix conversion
+
![Page 19: In Fix to Post Fix](https://reader035.fdocuments.net/reader035/viewer/2022081718/54fb76224a7959434c8b4d35/html5/thumbnails/19.jpg)
infix expression
postfix expression
g * ( (a+d) * (b-c/f) )
a b * b c -
/
Infix to postfix conversion
+
![Page 20: In Fix to Post Fix](https://reader035.fdocuments.net/reader035/viewer/2022081718/54fb76224a7959434c8b4d35/html5/thumbnails/20.jpg)
infix expression
postfix expression
* ( (a+d) * (b-c/f) )
a b * b c - g
/
Infix to postfix conversion
+
![Page 21: In Fix to Post Fix](https://reader035.fdocuments.net/reader035/viewer/2022081718/54fb76224a7959434c8b4d35/html5/thumbnails/21.jpg)
infix expression
postfix expression
( (a+d) * (b-c/f) )
a b * b c – g /
*
Infix to postfix conversion
+
![Page 22: In Fix to Post Fix](https://reader035.fdocuments.net/reader035/viewer/2022081718/54fb76224a7959434c8b4d35/html5/thumbnails/22.jpg)
infix expression
postfix expression
(a+d) * (b-c/f) )
a b * b c – g /(
Infix to postfix conversion
+
*
![Page 23: In Fix to Post Fix](https://reader035.fdocuments.net/reader035/viewer/2022081718/54fb76224a7959434c8b4d35/html5/thumbnails/23.jpg)
infix expression
postfix expression
a+d) * (b-c/f) )
a b * b c – g /
(
Infix to postfix conversion
+
*
(
![Page 24: In Fix to Post Fix](https://reader035.fdocuments.net/reader035/viewer/2022081718/54fb76224a7959434c8b4d35/html5/thumbnails/24.jpg)
infix expression
postfix expression
+d) * (b-c/f) )
a b * b c – g / a
(
Infix to postfix conversion
+
*
(
![Page 25: In Fix to Post Fix](https://reader035.fdocuments.net/reader035/viewer/2022081718/54fb76224a7959434c8b4d35/html5/thumbnails/25.jpg)
infix expression
postfix expression
d) * (b-c/f) )
a b * b c – g / a
(
Infix to postfix conversion
+
*
(
+
![Page 26: In Fix to Post Fix](https://reader035.fdocuments.net/reader035/viewer/2022081718/54fb76224a7959434c8b4d35/html5/thumbnails/26.jpg)
infix expression
postfix expression
) * (b-c/f) )
a b * b c – g / a d
(
Infix to postfix conversion
+
*
(
+
![Page 27: In Fix to Post Fix](https://reader035.fdocuments.net/reader035/viewer/2022081718/54fb76224a7959434c8b4d35/html5/thumbnails/27.jpg)
infix expression
postfix expression
* (b-c/f) )
a b * b c – g / a d +
Infix to postfix conversion
*
(
+
![Page 28: In Fix to Post Fix](https://reader035.fdocuments.net/reader035/viewer/2022081718/54fb76224a7959434c8b4d35/html5/thumbnails/28.jpg)
infix expression
postfix expression
(b-c/f) )
a b * b c – g / a d +
Infix to postfix conversion
*
*
+
(
![Page 29: In Fix to Post Fix](https://reader035.fdocuments.net/reader035/viewer/2022081718/54fb76224a7959434c8b4d35/html5/thumbnails/29.jpg)
infix expression
postfix expression
b-c/f) )
a b * b c – g / a d +
Infix to postfix conversion
*
*
+
(
(
![Page 30: In Fix to Post Fix](https://reader035.fdocuments.net/reader035/viewer/2022081718/54fb76224a7959434c8b4d35/html5/thumbnails/30.jpg)
infix expression
postfix expression
-c/f) )
a b * b c – g / a d + b
Infix to postfix conversion
*
*
+
(
(
![Page 31: In Fix to Post Fix](https://reader035.fdocuments.net/reader035/viewer/2022081718/54fb76224a7959434c8b4d35/html5/thumbnails/31.jpg)
infix expression
postfix expression
c/f) )
a b * b c – g / a d + b
Infix to postfix conversion
*
*
+
-
(
(
![Page 32: In Fix to Post Fix](https://reader035.fdocuments.net/reader035/viewer/2022081718/54fb76224a7959434c8b4d35/html5/thumbnails/32.jpg)
infix expression
postfix expression
/f) )
a b * b c – g / a d + b c
Infix to postfix conversion
*
*
+
-
(
(
![Page 33: In Fix to Post Fix](https://reader035.fdocuments.net/reader035/viewer/2022081718/54fb76224a7959434c8b4d35/html5/thumbnails/33.jpg)
infix expression
postfix expression
f) )
a b * b c – g / a d + b c
Infix to postfix conversion
*
*
+
/
(
(
-
![Page 34: In Fix to Post Fix](https://reader035.fdocuments.net/reader035/viewer/2022081718/54fb76224a7959434c8b4d35/html5/thumbnails/34.jpg)
infix expression
postfix expression
) )
a b * b c – g / a d + b c f
Infix to postfix conversion
*
*
+
/
(
(
-
![Page 35: In Fix to Post Fix](https://reader035.fdocuments.net/reader035/viewer/2022081718/54fb76224a7959434c8b4d35/html5/thumbnails/35.jpg)
infix expression
postfix expression
)
a b * b c – g / a d + b c f / -
Infix to postfix conversion
*
*
+
(
![Page 36: In Fix to Post Fix](https://reader035.fdocuments.net/reader035/viewer/2022081718/54fb76224a7959434c8b4d35/html5/thumbnails/36.jpg)
infix expression
postfix expression
a b * b c – g / a d + b c f / - *
Infix to postfix conversion
*
+
![Page 37: In Fix to Post Fix](https://reader035.fdocuments.net/reader035/viewer/2022081718/54fb76224a7959434c8b4d35/html5/thumbnails/37.jpg)
infix expression
postfix expression
a b * b c – g / a d + b c f / - * *
Infix to postfix conversion
+
![Page 38: In Fix to Post Fix](https://reader035.fdocuments.net/reader035/viewer/2022081718/54fb76224a7959434c8b4d35/html5/thumbnails/38.jpg)
infix expression
postfix expression
a b * b c – g / a d + b c f / - * * +
Infix to postfix conversion
![Page 39: In Fix to Post Fix](https://reader035.fdocuments.net/reader035/viewer/2022081718/54fb76224a7959434c8b4d35/html5/thumbnails/39.jpg)
Equivalent postfix expression
a b * b c – g / a d + b c f / - * * +
![Page 40: In Fix to Post Fix](https://reader035.fdocuments.net/reader035/viewer/2022081718/54fb76224a7959434c8b4d35/html5/thumbnails/40.jpg)
We have observed When we get any integer or character it goes to the output. The order of the operands in the postfix expression is the same as
the order in the infix expression, and the operands that appear to the left of an operator in the infix expression also appear to its left in the postfix expression.
When we get an operator, it is pushed onto the stack maintaining the following rule
if the stack is empty, the operator is pushed onto the stack. However, if the stack is not empty, at first we pop the operators of greater or equal precedence from the stack and append them to postfix expression. Then the new operator is pushed onto the stack. Thus, this step orders the operators by precedence and in accordance with left-to-right association.
![Page 41: In Fix to Post Fix](https://reader035.fdocuments.net/reader035/viewer/2022081718/54fb76224a7959434c8b4d35/html5/thumbnails/41.jpg)
We have observed
After getting a left parenthesis ‘(‘ , 1 & 2 is maintained until we get right parenthesis ‘)’. When ")" is encountered, we pop operators off the stack and append them to the end of postfix expression until we encounter the matching "(".
Within a pair of parentheses, precedence and left-to-right association determine the order of the operators.
When we reach the end of the string, we append the remaining contents of the stack to postfix expression.
And finally we get the postfix expression.
![Page 42: In Fix to Post Fix](https://reader035.fdocuments.net/reader035/viewer/2022081718/54fb76224a7959434c8b4d35/html5/thumbnails/42.jpg)
Thanks to……..
Course teacher:Abul Hasan Samee
![Page 43: In Fix to Post Fix](https://reader035.fdocuments.net/reader035/viewer/2022081718/54fb76224a7959434c8b4d35/html5/thumbnails/43.jpg)
Made by……
Sabrina Hossain Tonny
Roll : 200614033
CSE-7
MIST