0% found this document useful (0 votes)
13 views6 pages

Infix, Prefix, Postfix Expression Examples

Uploaded by

rcit7600
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)
13 views6 pages

Infix, Prefix, Postfix Expression Examples

Uploaded by

rcit7600
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

Examples of Infix, Prefix, and Postfix

Infix Expression Prefix Expression Postfix Expression

A+B +AB AB+

A+B*C +A*BC ABC*+

Infix Prefix Postfix


A+B-C -+ABC AB+C-
(A+B)*C-D -*+ABCD AB+C*D-

An Expression with Parentheses

Infix Expression Prefix Expression Postfix Expression

(A + B) * C *+ABC AB+C*

Additional Examples of Infix, Prefix, and Postfix

Infix Expression Prefix Expression Postfix Expression

A+B*C+D ++A*BCD ABC*+D+

(A + B) * (C + D) *+AB+CD AB+CD+*

A*B+C*D +*AB*CD AB*CD*+

A+B+C+D +++ABCD AB+C+D+

Input: A*B+C
Output: AB*C+

Input: (A+B)*(C/D)
Output: AB+CD/*

Input: A*(B*C+D*E)+F
Output: ABC*DE*+*F+

Stack_InPost_Example 1 / 6
Input: (A+B)*C+(D-E)/F+G
Output: AB+C*DE-F/+G+

Infix Expression: A * B- (C + D) + E

Input Operations on Stack Postfix Expression


Character Stack
A Empty A
* Push * A
B * AB
- Check and Push - AB*
( Push -( AB*
C -( AB*C
+ Check and Push -(+ AB*C
D AB*CD
) Pop and Append - AB*CD+
to Postfix till ‘(’
+ Check and + AB*CD+-
Push
E + AB*CD+-E
End Pop till Empty AB*CD+-E+

Infix Expression: A*(B*C+D*E)+F :

Stack_InPost_Example 2 / 6
Infix Expression : m*n+(p-q)+r.

Steps Current Operation on Stack Stack Postfix


Token Representation Expression
1 m m
2 * Push * m
3 n * mn
4 + Push + mn*
5 ( Push +( mn*
6 p +( mn*p
7 - Push +(- mn*p
8 q +(- mn*pq
9 ) Pop and append + mn*pq-
until the open
bracket
10 + Push + mn*pq-+
11 r + mn*pq-+r
12 end Pop until the stack mn*pq-+r
is empty

Infix Expression : A+(B*C+D)/E

Input Postfix
Stack Action
Token Expression

A A Add A into expression string

+ + A Push ‘+’ into stack

( +( A Push ( into stack

B +( AB Add B into expression string

* +(* AB Push ‘*’ into stack

C +(* ABC Add C into expression string

‘+’ operator has less precedence than


+ +(+ ABC* ‘*’, so pop * and add to
expression string

D +(+ ABC*D Add D into expression string

) has come so pop + and add it to


) + ABC*D+
expression string

/ +/ ABC*D+ / has higher precedence than + so push

Stack_InPost_Example 3 / 6
/ into stack

Add E into expression string and pop


E +/ ABC*D+E/+ all operators one by one from
stack and add it to expression string

Infix expression: K + L - M*N + (O^P) * W/U/V * T + Q


Input Expression Stack Postfix Expression

K K

+ +

L + KL

- - K L+

M - K L+ M

* -* K L+ M

N -* KL+MN

+ + K L + M N*
K L + M N* -

( +( K L + M N *-

O +( KL+MN*-O

^ +(^ K L + M N* - O

P +(^ K L + M N* - O P

) + K L + M N* - O P ^

* +* K L + M N* - O P ^

W +* K L + M N* - O P ^ W

/ +/ K L + M N* - O P ^ W *

U +/ K L + M N* - O P ^W*U

/ +/ K L + M N* - O P ^W*U/

V +/ KL + MN*-OP^W*U/V

Stack_InPost_Example 4 / 6
* +* KL+MN*-OP^W*U/V/

T +* KL+MN*-OP^W*U/V/T

+ + KL+MN*-OP^W*U/V/T*
KL+MN*-OP^W*U/V/T*+

Q + KL+MN*-OP^W*U/V/T*Q

KL+MN*-OP^W*U/V/T*+Q+

The final postfix expression of infix expression(K + L - M*N + (O^P) * W/U/V * T +


Q) is KL+MN*-OP^W*U/V/T*+Q+.

Convert the following infix expression into postfix expression using the algorithm
A – ( B / C + (D % E * F) / G )* H
A – ( B / C + (D % E * F) / G )* H )

Infix Character STACK Postfix Expression


Scanned
(

A ( A

- (- A

( (-( A

B (-( AB

/ (-(/ AB

C (-(/ ABC

+ (-(+ ABC/

( (-(+( ABC/

D (-(+( ABC/D

% (-(+(% ABC/D

E (-(+(% ABC/DE

* (-(+(%* ABC/DE

F (-(+(%* ABC/DEF
Stack_InPost_Example 5 / 6
) (-(+ ABC/DEF*%

/ (-(+/ ABC/DEF*%

G (-(+/ ABC/DEF*%G

) (- ABC/DEF*%G/+

* (-* ABC/DEF*%G/+

H (-* ABC/DEF*%G/+H

) ABC/DEF*%G/+H* -

Stack_InPost_Example 6 / 6

You might also like