Conversion of Infix Expression To Prefix and Postfix Expression Without Using Stack
Conversion of Infix Expression To Prefix and Postfix Expression Without Using Stack
Bengaluru, Karnataka
Computer Science & Engineering
School of Computer Science & Engineering
Conversion of infix expression to prefix and postfix expression without using stack
Precedence & Associativity
Parenthesis ( ), [ ], { } L to R
Exponents ^ R to L
Multiplication & Division *, / L to R
operator
Addition & Subtraction +, - L to R
operator
Infix expression :
Op1 operator Op2 (Operator in between operands is called as infix expression)
Example: a + b, a – b, a * b, a / b, a + b * c, a + (b – c), 1 + 2 – (b + c),……..
Prefix expression : (operator ahead of operands)
+ab, -ab, *ab, /ab,…..
Postfix expression : (Operator after operands)
ab+, ab-, ab*, ab/,…….
Conversion of infix to prefix and postfix:
Infix expression Prefix expression Postfix expression
a+b +ab ab+
a+b*c =a+b*c =a+b*c
= a + (b * c) = a + (b * c)
= a + (*bc) = a + (bc*)
= +a*bc = abc*+
a+b–c =a+b–c =a+b–c
= (a + b) – c = (a + b) – c
= (+ab) – c = (ab+) – c
= -+abc = ab+c-
a + (b – c) = a + (b – c) = a + (b – c)
= a + (-bc) = a + (bc-)
= +a-bc = abc-+
a+b–c*d–e^f = a+b-c*d-e^f = a+b-c*d-(e^f)
= a+b-c*d-(e^f) = a+b-{c*d}-(ef^)
= a+b-c*d-(^ef) = {a+b}-(cd*)-(ef^)
= a+b-{c*d}-(^ef) = {(ab+)-(cd*)}-(ef^)
Prepared by,
Ms. Sridevi S, AP/SoCSE, Ms. Meena Kumari, AP/SoCSE, Ms. Rohini A, AP/SoCSE 1
= a+b-(*cd)-(^ef) = (ab+cd*-)-(ef^)
= {a+b}-(*cd)-(^ef) = ab+cd*-ef^-
={(+ab)-(*cd)}-(^ef)
=(-+ab*cd)-(^ef)
= --+ab*cd^ef
Try this
Infix expression:
a + b – c * d – (e ^ f) * g / h * i + j
Prepared by,
Ms. Sridevi S, AP/SoCSE, Ms. Meena Kumari, AP/SoCSE, Ms. Rohini A, AP/SoCSE 2