0% found this document useful (0 votes)
15 views45 pages

INFIXTOPOSTFIX

The document discusses converting expressions from infix to postfix notation. It provides: 1) An algorithm that scans an infix expression from left to right, appending operands and pushing/popping operators onto a stack based on precedence to output postfix notation. 2) An example of converting the expression A * (B + C) - D / E step-by-step using the algorithm. 3) A summary of the algorithm which examines each element, outputs operands, pushes parentheses, and pushes/pops operators based on precedence.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
15 views45 pages

INFIXTOPOSTFIX

The document discusses converting expressions from infix to postfix notation. It provides: 1) An algorithm that scans an infix expression from left to right, appending operands and pushing/popping operators onto a stack based on precedence to output postfix notation. 2) An example of converting the expression A * (B + C) - D / E step-by-step using the algorithm. 3) A summary of the algorithm which examines each element, outputs operands, pushes parentheses, and pushes/pops operators based on precedence.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 45

DATA STRUCTURES

INFIX TO POSTFIX CONVERSION


01 INFIX, POSTFIX AND PREFIX NOTATION

02 INFIX TO POSTFIX CONVERSION-ALGORITHM

03
03 EXAMPLES FOR INFIX TO POSTFIX CONVERSION

04
Infix notation

Infix notation is the common arithmetic and logical formula notation, in which

operators are written infix-style between the operands they act on

E.g. A+B
Postfix notation

In Postfix notation, the operator comes after the Operand.

For example, the Infix expression A+B will be written as AB+ in its Postfix Notation.

Postfix is also called ‘Reverse Polish Notation’


Prefix notation

In Prefix notation, the operator comes before the operand.

The Infix expression A+B will be written as +AB in its Prefix Notation.

Prefix is also called ‘Polish Notation’


Conversion from Infix to Postfix Algorithm

Step1

Scan the Infix expression from left to right for tokens (Operators, Operands

& Parentheses) and perform the steps 2 to 5 for each token in the Expression
Algorithm

Step2

If token is operand, Append it in postfix expression

Step3

If token is a left parentheses “(“, push it in stack.


Algorithm

Step4

If token is an operator,

Check the stack top and Pop the operator if it is of higher or equal

precedence than the incoming token and append it (in the same order) to the

output Expression.

After popping , push the new token on stack.


Algorithm
Step5

If “)” right parentheses is found,

Pop all the operators from the Stack and append them to Output String, till you encounter

the Opening Parenthesis “(“.

Pop the left parenthesis but don’t append it to the output string (Postfix notation does not

have brackets).
Algorithm

Step6

When all tokens of Infix expression have been scanned. Pop all the elements

from the stack and append them to the Output String.

The Output string is the Corresponding Postfix Notation.


Example 1
Let the incoming the Infix expression be:

A * (B + C) – D / E

Stage 1: Stack is empty and we only have the Infix Expression

.
Example

Stage 2

The first token is Operand A Operands are Appended to

the Output as it is.


Example

Stage 3

Next token is * Since Stack is empty (top==NULL) it is pushed

into the Stack


Example
Stage 4

Next token is ( the precedence of open-parenthesis, when it is to go in

side, is maximum.

But when another operator is to come on the top of ‘(‘ then its preced

ence is least.
Example

Stage 5

Next token, B is an operand which will go to the Output expression as

it is
Example

Stage 6

Next token, + is operator, We consider the precedence of top elemen

t in the Stack, ‘(‘. The outgoing precedence of open parenthesis is the

least (refer point 4. Above). So + gets pushed into the Stack


Example

Stage 7

 Next token, C, is appended to the output


Example

Stage 8

Next token ), means that pop all the elements from Stack and appe

nd them to the output expression till we read an opening parenthesis.


Example
Stage 9

Next token, -, is an operator. The precedence of operator on the top of

Stack ‘*‘ is more than that of Minus. So we pop multiply and append it

to output expression. Then push minus in the Stack.


Example

Stage 10

Next, Operand ‘D‘ gets appended to the output.


Example

Stage 11

Next, we will insert the division operator into the Stack because its pr

ecedence is more than that of minus.


Example

Stage 12

The last token, E, is an operand, so we insert it to the output Expres

sion as it is.
Example

Stage 13

The input Expression is complete now. So we pop the Stack and Ap

pend it to the Output Expression as we pop it.


Algorithm for Infix to Postfix
1) Examine the next element in the input.
2) If it is operand, output it.
3) If it is opening parenthesis, push it on stack.
4) If it is an operator, then
i) If stack is empty, push operator on stack.
ii) If the top of stack is opening parenthesis, push operator on stack
iii) If it has higher priority than the top of stack, push operator on stack.
iv) Else pop the operator from the stack and output it, repeat step 4
5) If it is a closing parenthesis, pop operators from stack and output them
until an opening parenthesis is encountered. pop and discard the opening
parenthesis.
6) If there is more input go to step 1
7) If there is no more input, pop the remaining operators to output.
Example-2:Suppose we want to convert 2*3/(2-1)+5*3 into Postfix form
Example-2:Suppose we want to convert 2*3/(2-1)+5*3 into Pos
tfix form
Expression Stack Output
2 Empty 2
* * 2
3 * 23
/ / 23*
( /( 23*
2 /( 23*2
- /(- 23*2
1 /(- 23*21
) / 23*21-
+ + 23*21-/
5 + 23*21-/5
* +* 23*21-/53
3 +* 23*21-/53
Empty 23*21-/53*+
Scan the Infix expression left to right
◼ If the character x is an operand
❑ Output the character into the Postfix Expression
◼ If the character x is a left or right parenthesis
❑ If the character is “(“
◼ Push it into the stack
❑ if the character is “)”
◼ Repeatedly pop and output all the operators/characters u
ntil “(“ is popped from the stack.
◼ If the character x is a is a regular operator
◼ Step 1: Check the character y currently at the top of the st
ack.
◼ Step 2: If Stack is empty or y=‘(‘ or y is an operator of low
er 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.
Stack

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

Postfix Expression
Stack

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

Postfix Expression

(
Stack

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

Postfix Expression
a

(
Stack

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

Postfix Expression
a

+
(
Stack

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

Postfix Expression
ab

+
(
Stack

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

Postfix Expression
ab+

-
(
Stack

Infix Expression
)*d–(e+f)

Postfix Expression
ab+c

-
(
Stack

Infix Expression
*d–(e+f)

Postfix Expression
ab+c-
Stack

Infix Expression
d–(e+f)

Postfix Expression
ab+c-

*
Stack

Infix Expression
–(e+f)

Postfix Expression
ab+c-d

*
Stack

Infix Expression
(e+f)

Postfix Expression
ab+c–d*

-
Stack

Infix Expression
e+f)

Postfix Expression
ab+c–d*

(
-
Stack

Infix Expression
+f)

Postfix Expression
ab+c–d*e

(
-
Stack

Infix Expression
f)

Postfix Expression

+ ab+c–d*e

(
-
Stack

Infix Expression
)

Postfix Expression

+ ab+c–d*ef

(
-
Stack

Infix Expression

Postfix Expression
ab+c–d*ef+

-
Stack

Infix Expression

Postfix Expression
ab+c–d*ef+-
Thank you

You might also like