0% found this document useful (0 votes)
38 views60 pages

Infix To Postfix Conversion

Uploaded by

Sneha Malik
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
38 views60 pages

Infix To Postfix Conversion

Uploaded by

Sneha Malik
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 60

Arithmetic Expressions

Infix form
operand operator operand
 2+3 or
 a+b
Need precedence rules
May use parentheses
 4*(3+5) or
 a*(b+c)
Alternatives to infix -- prefix
Prefix notation, a.k.a. Polish notation
Operator precedes operands
infix expression: A + B
prefix expression: +AB
Parentheses become unnecessary
infix expression: (A + B) * C
prefix expression: * + A B C

2 stacks2
Alternative II: Postfix
Postfix is also known as reverse Polish notation --
widely used in HP calculators
In postfix notation, operators appear after the operands
they operate on
As with prefix, operand order is maintained, and
parentheses are not needed
Postfix expression is not merely a reverse of the
equivalent prefix expression

3 stacks2
Arithmetic Expressions
Postfix form
Operator appears after the operands
 (4+3)*5 : 4 3 + 5 *
 4+(3*5) : 4 3 5 * +
No precedence rules or parentheses!
Input expression given in postfix form
How to evaluate it?
Postfix expression examples
Simple expression:
Original Expression: A + B
Postfix Equivalent: A B +
Compound expression with parentheses:
original: (A + B) * (C - D)
postfix: A B + C D - *
Compound expression without parentheses:
original: A + B * C - D
postfix: A B C * + D -

5 stacks2
Translating infix to postfix
General method:
move each operator to the right of its corresponding right
parenthesis
eliminate parentheses
Example:
(((A + B) * C) - (E * (F + G)))
(((A B) + C) * (E (F G) +) * ) -
A B+ C * E F G + * -

6 stacks2
Infix to Postfix
(((A+B)*C)-((D+E)/F))

A B+C* D E+F/-
 Operand order does not change!
 Operators are in order of evaluation!
Infix to Postfix

(A + B) / D AB+D/

(A + B) / (D + E) AB+DE+/

(A - B / C + E)/(A + B) ABC/ - E+AB+ /

A* (B+C) /D ABC+*D/
Why do we need the stack?
a+b ab +

a+b a b +

+
Example

Infix String : a+b*c-d

Postfix String : abc*+d-

b*c -> bc* a+bc* -> abc*+ abc*+-d -> abc*+d-


Infix String : a+b*c-d

+
Infix String : a+b*c-d

ab

*
+
Infix String : a+b*c-d

abc *+

-
Infix String : a+b*c-d

abc *+d-
Infix To Postfix Conversion
Scan the Infix expression left to right
If the character x is an Output the character into the Postfix Expression
operand
If the character is “(“ Push it into the stack
If the character is “)” Repeatedly pop and output all the operators until “(“
is popped from the stack.
If the character x is a is Step 1: Check the character y currently at the top of
a regular operator the stack.
Step 2: If Stack is empty or y=‘(‘ or y is an operator
of lower precedence than x, then push x into
stack.
Step 3: If y is an operator of higher or equal
precedence than x, then pop and output y and
push x into the stack.

When all characters in infix expression are processed repeatedly pop the character(s)
from the stack and output them until the stack is empty
Infix to postfix conversion
Stack

Infix Expression
(a+b-c)*d–(e+f)

Postfix Expression
Infix to postfix conversion
Stack

Infix Expression
a+b-c)*d–(e+f)

Postfix Expression

(
Infix to postfix conversion
Stack

Infix Expression
+b-c)*d–(e+f)

Postfix Expression
a

(
Infix to postfix conversion
Stack

Infix Expression
b-c)*d–(e+f)

Postfix Expression
a

+
(
Infix to postfix conversion
Stack

Infix Expression
-c)*d–(e+f)

Postfix Expression
ab

+
(
Infix to postfix conversion
Stack

Infix Expression
c)*d–(e+f)

Postfix Expression
ab+

-
(
Infix to postfix conversion
Stack

Infix Expression
)*d–(e+f)

Postfix Expression
ab+c

-
(
Infix to postfix conversion
Stack

Infix Expression
*d–(e+f)

Postfix Expression
ab+c-
Infix to postfix conversion
Stack

Infix Expression
d–(e+f)

Postfix Expression
ab+c-

*
Infix to postfix conversion
Stack

Infix Expression
–(e+f)

Postfix Expression
ab+c-d

*
Infix to postfix conversion
Stack

Infix Expression
(e+f)

Postfix Expression
ab+c–d*

-
Infix to postfix conversion
Stack

Infix Expression
e+f)

Postfix Expression
ab+c–d*

(
-
Infix to postfix conversion
Stack

Infix Expression
+f)

Postfix Expression
ab+c–d*e

(
-
Infix to postfix conversion
Stack

Infix Expression
f)

Postfix Expression

+ ab+c–d*e

(
-
Infix to postfix conversion
Stack

Infix Expression
)

Postfix Expression

+ ab+c–d*ef

(
-
Infix to postfix conversion
Stack

Infix Expression

Postfix Expression
ab+c–d*ef+

-
Infix to postfix conversion
Stack

Infix Expression

Postfix Expression
ab+c–d*ef+-
FPE Infix to Postfix
(((A+B)*(C-E))/(F+G))

 stack: <empty>
 output: []
FPE Infix to Postfix
((A+B)*(C-E))/(F+G))

 stack: (
 output: []
FPE Infix to Postfix
(A+B)*(C-E))/(F+G))

 stack: ( (
 output: []
FPE Infix to Postfix
A+B)*(C-E))/(F+G))

 stack: ( ( (
 output: []
FPE Infix to Postfix
+B)*(C-E))/(F+G))

 stack: ( ( (
 output: [A]
FPE Infix to Postfix
B)*(C-E))/(F+G))

 stack: ( ( ( +
 output: [A]
FPE Infix to Postfix
)*(C-E))/(F+G))

 stack: ( ( ( +
 output: [A B]
FPE Infix to Postfix
*(C-E))/(F+G))

 stack: ( (
 output: [A B + ]
FPE Infix to Postfix
(C-E))/(F+G))

 stack: ( ( *
 output: [A B + ]
FPE Infix to Postfix
C-E))/(F+G))

 stack: ( ( * (
 output: [A B + ]
FPE Infix to Postfix
-E))/(F+G))

 stack: ( ( * (
 output: [A B + C ]
FPE Infix to Postfix
E))/(F+G))

 stack: ( ( * ( -
 output: [A B + C ]
FPE Infix to Postfix
))/(F+G))

 stack: ( ( * ( -
 output: [A B + C E ]
FPE Infix to Postfix
)/(F+G))

 stack: ( ( *
 output: [A B + C E - ]
FPE Infix to Postfix
/(F+G))

 stack: (
 output: [A B + C E - * ]
FPE Infix to Postfix
(F+G))

 stack: ( /
 output: [A B + C E - * ]
FPE Infix to Postfix
F+G))

 stack: ( / (
 output: [A B + C E - * ]
FPE Infix to Postfix
+G))

 stack: ( / (
 output: [A B + C E - * F ]
FPE Infix to Postfix
G))

 stack: ( / ( +
 output: [A B + C E - * F ]
FPE Infix to Postfix
))

 stack: ( / ( +
 output: [A B + C E - * F G ]
FPE Infix to Postfix
)

 stack: ( /
 output: [A B + C E - * F G + ]
FPE Infix to Postfix

 stack: <empty>
 output: [A B + C E - * F G + / ]
Infix to Postfix Example
A + B * C - D / E

Infix Stack(bot->top) Postfix


a) A + B * C - D / E
b) + B * C - D / E A
c) B * C - D / E + A
d) * C - D / E + A B
e) C - D / E + * A B
f) - D / E + * A B C
g) D / E + - A B C *
h) / E + - A B C * D
i) E + - / A B C * D
j) + - / A B C * D E
k) A B C * D E / -
+
Infix to Postfix Example #2
A * B - ( C + D ) + E

Infix Stack(bot->top) Postfix

a) A * B - ( C - D ) + E empty empty
b) * B - ( C + D ) + E empty A
c) B - ( C + D ) + E * A
d) - ( C + D ) + E * A B
e) - ( C + D ) + E empty A B *
f) ( C + D ) + E - A B *
g) C + D ) + E - ( A B *
h) + D ) + E - ( A B * C
i) D ) + E - ( + A B * C
j) ) + E - ( + A B * C D
k) + E - A B * C D +
l) + E empty A B * C D + -
m) E + A B * C D + -
n) + A B * C D + - E
o) empty A B * C D + - E
+
Evaluating Postfix Expressions
Use a stack, assume binary operators +,*
Input: postfix expression
Scan the input
If operand,
 push to stack
If operator
 pop the stack twice
 apply operator
 push result back to stack
Postfix Evaulation
Operand: push
Operator: pop 2 operands, do the math, pop result
back onto stack

1 2 3 + *

Postfix Stack( bot -> top )


a) 1 2 3 + *
b) 2 3 + * 1
c) 3 + * 1 2
d) + * 1 2 3
e) * 1 5 // 5 from 2 + 3
f) 5 // 5 from 1 * 5

You might also like