Prefix and Postfix Expression Overview
Infix Expression: Operators are placed between operands (e.g., A + B).
Prefix Expression (Polish Notation): Operators are placed before operands (e.g., + A B).
Postfix Expression (Reverse Polish Notation): Operators are placed after operands (e.g., A B
+).
Conversion Between Expressions
1. Infix to Postfix Conversion:
Algorithm:
1. Initialize an empty stack for operators and an empty list for the output.
2. Read the infix expression from left to right.
3. For each token:
If it's an operand, append it to the output list.
If it's an operator, pop operators from the stack to the output list until
you find an operator with lower precedence or the stack is empty, then
push the current operator onto the stack.
If it's a parenthesis, handle it according to its type (push on opening
parenthesis, pop till opening parenthesis on closing parenthesis).
4. Pop any remaining operators in the stack to the output list.
2. Infix to Prefix Conversion:
Algorithm:
1. Reverse the infix expression.
2. Replace every '(' with ')' and vice versa.
3. Convert the modified expression to postfix notation using the infix-to-postfix
algorithm.
4. Reverse the postfix expression obtained to get the prefix expression.
Sample Questions
Objective Questions
1. Which of the following expressions is in postfix notation?
o A) A + B
o B) + A B
o C) A B +
o D) (A + B)
Answer: C) A B +
2. In the infix expression (A + B) * C, what will be the postfix expression?
o A) A B + C *
o B) A B C * +
o C) A + B C *
o D) A B + C *
Answer: A) A B + C *
3. Which data structure is used to convert infix expressions to postfix expressions?
o A) Queue
o B) Stack
o C) Linked List
o D) Tree
Answer: B) Stack
4. What is the prefix notation of the infix expression A - B / C * D?
o A) - A / B * C D
o B) / - A B * C D
o C) - A / B * D C
o D) - / A B * C D
Answer: A) - A / B * C D
5. For the postfix expression A B + C *, what is the equivalent infix expression?
o A) (A + B) * C
o B) A + (B * C)
o C) (A * B) + C
o D) A + B * C
Answer: A) (A + B) * C
6. In postfix notation, what is the result of the expression 5 6 2 + * 12 4 / -?
o A) 8
o B) 10
o C) 14
o D) 16
Answer: C) 14
7. What is the result of converting the infix expression A + (B - C) * D to prefix
notation?
o A) + A * - B C D
o B) * + A - B C D
o C) + * A - B C D
o D) * + A - B C D
Answer: A) + A * - B C D
8. Which operation has the highest precedence in infix expressions?
o A) Addition
o B) Subtraction
o C) Multiplication
o D) Parentheses
Answer: D) Parentheses
9. In the postfix expression A B C + *, what operation is performed first?
o A) Multiplication
o B) Addition
o C) Both are performed simultaneously
o D) None
Answer: B) Addition
10. What is the prefix notation for the postfix expression A B C / *?
o A) * A / B C
o B) * / A B C
o C) * A C / B
o D) / * A B C
Answer: A) * A / B C
11. What will be the postfix expression for the prefix notation + * A B C?
o A) A B * C +
o B) A B * C +
o C) A B C * +
o D) A B C * +
Answer: D) A B C * +
12. In the expression * + A B - C D, what operation is performed last in prefix notation?
o A) Multiplication
o B) Addition
o C) Subtraction
o D) None
Answer: A) Multiplication
13. What will be the prefix notation of the infix expression A / (B - C) * D?
o A) * / A - B C D
o B) * / A - C B D
o C) / A - B C * D
o D) * / A - C B D
Answer: A) * / A - B C D
14. If the infix expression is A * B + C, which of the following is its postfix expression?
o A) A B * C +
o B) A B C + *
o C) * A B + C
o D) A * B + C
Answer: A) A B * C +
15. How is the infix expression A - B / C * D converted to postfix notation?
o A) A B C D / * -
o B) A B C / D * -
o C) A B C D * / -
o D) A B C / D * -
Answer: A) A B C D / * -
Short Answer Questions
1. Explain how to convert an infix expression to postfix notation.
o Answer: Use a stack to hold operators and parentheses. Read the infix
expression from left to right. Append operands directly to the output. Push
operators to the stack, ensuring higher precedence operators are processed first.
Use parentheses to handle precedence explicitly. At the end, pop all operators
from the stack to the output.
2. Describe the steps to convert an infix expression to prefix notation.
o Answer: Reverse the infix expression. Replace '(' with ')' and vice versa.
Convert this modified expression to postfix notation. Reverse the postfix
expression to get the prefix notation.
3. What is the main advantage of using postfix notation over infix notation?
o Answer: Postfix notation eliminates the need for parentheses to denote operator
precedence, making it easier to evaluate expressions using a stack.
4. How does the stack help in evaluating a postfix expression?
o Answer: The stack holds operands until an operator is encountered. When an
operator is found, the required number of operands are popped from the stack,
the operation is performed, and the result is pushed back onto the stack.
5. What is the difference between prefix and postfix notation?
o Answer: In prefix notation, operators precede their operands, while in postfix
notation, operators follow their operands.
6. How do you handle parentheses in infix expressions when converting to postfix?
o Answer: Push opening parentheses onto the stack. Pop from the stack to the
output until the matching opening parenthesis is found when a closing
parenthesis is encountered.
7. What is the significance of operator precedence in infix expressions?
o Answer: Operator precedence determines the order in which operations are
performed in an expression. Higher precedence operators are evaluated before
lower precedence operators.
8. How does a stack manage operator precedence during infix to postfix conversion?
o Answer: The stack helps manage operator precedence by holding operators
until operators of lower or equal precedence are encountered, ensuring that
operations are performed in the correct order.
Long Answer Questions
1. Describe the algorithm to convert an infix expression to a postfix expression with
an example.
o Answer:
Initialize an empty stack and an empty output list.
Process each character of the infix expression:
If it's an operand, add it to the output list.
If it's an operator, pop operators from the stack to the output list
until an operator of lower precedence or a left parenthesis is at
the top of the stack. Then push the current operator onto the
stack.
If it's a left parenthesis, push it onto the stack.
If it's a right parenthesis, pop operators from the stack to the
output list until a left parenthesis is encountered. Remove the left
parenthesis.
After processing the input expression, pop any remaining operators from
the stack to the output list.
Example: For infix expression A + B * C, the postfix expression is A B C
* +.
2. Explain the process of converting an infix expression to prefix notation. Include
an example and describe the use of stacks.
o Answer:
Reverse the infix expression.
Replace every '(' with ')' and vice versa.
Convert the modified expression to postfix notation using the infix to
postfix algorithm.
Reverse the postfix expression to get the prefix expression.
Example: For infix expression A - B / C * D, reversing gives D * C / B - A.
Converting this to postfix yields D C B / * A -. Reversing this gives - A * /
B C D.
3. Discuss the evaluation of a postfix expression using a stack. Provide an example
with detailed steps.
o Answer:
Initialize an empty stack.
Read the postfix expression from left to right.
For each operand, push it onto the stack.
For each operator, pop the required number of operands from the stack,
perform the operation, and push the result back onto the stack.
Example: For postfix expression 5 6 2 + * 12 4 / -:
Push 5, 6, 2 onto the stack.
Encounter +, pop 6 and 2, compute 6 + 2 = 8, push 8.
Encounter *, pop 5 and 8, compute 5 * 8 = 40, push 40.
Push 12, 4.
Encounter /, pop 12 and 4, compute 12 / 4 = 3, push 3.
Encounter -, pop 40 and 3, compute 40 - 3 = 37.
The final result is 37.
4. Provide a detailed explanation of the use of stacks in the conversion of infix to
postfix and prefix notations.
o Answer:
Stacks help manage operators and parentheses during the conversion
process. They ensure that operators are processed in the correct order
based on precedence and associativity.
Infix to Postfix: Operators are pushed onto the stack, and operands are
added directly to the output list. Operators are popped from the stack to
the output list when necessary.
Infix to Prefix: The infix expression is reversed, and parentheses are
swapped. The reversed expression is converted to postfix, which is then
reversed to get the prefix notation.
5. Discuss the practical applications of prefix and postfix notations in computer
science and programming.
o Answer:
Prefix Notation: Used in certain functional programming languages and
expression evaluation contexts where prefix notation is used for its
straightforward parsing and evaluation.
Postfix Notation: Widely used in calculators and stack-based languages
because it simplifies expression evaluation without requiring
parentheses. It’s also used in compilers for intermediate code generation
and syntax tree processing.
Here’s the table summarizing the operator precedence rules for common
mathematical operators:
Operator Precedence
Parentheses () Highest
Exponents ^ High
Multiplication * Medium
Division / Medium
Operator Precedence
Addition + Low
Subtraction – Low
Illustration:
Follow the below illustration for a better understanding
Consider the infix expression exp = “a+b*c+d”
and the infix expression is scanned using the iterator i, which is initialized as i = 0.
1st Step: Here i = 0 and exp[i] = ‘a’ i.e., an operand. So add this in the postfix expression.
Therefore, postfix = “a”.
Add ‘a’ in the postfix
2nd Step: Here i = 1 and exp[i] = ‘+’ i.e., an operator. Push this into the stack. postfix =
“a” and stack = {+}.
Push ‘+’ in the stack
3rd Step: Now i = 2 and exp[i] = ‘b’ i.e., an operand. So add this in the postfix
expression. postfix = “ab” and stack = {+}.
Add ‘b’ in the postfix
4th Step: Now i = 3 and exp[i] = ‘*’ i.e., an operator. Push this into the stack. postfix =
“ab” and stack = {+, *}.
Push ‘*’ in the stack
5th Step: Now i = 4 and exp[i] = ‘c’ i.e., an operand. Add this in the postfix
expression. postfix = “abc” and stack = {+, *}.
Add ‘c’ in the postfix
6th Step: Now i = 5 and exp[i] = ‘+’ i.e., an operator. The topmost element of the stack has
higher precedence. So pop until the stack becomes empty or the top element has less
precedence. ‘*’ is popped and added in postfix. So postfix = “abc*” and stack = {+}.
Pop ‘*’ and add in postfix
Now top element is ‘+‘ that also doesn’t have less precedence. Pop it. postfix = “abc*+”.
Pop ‘+’ and add it in postfix
Now stack is empty. So push ‘+’ in the stack. stack = {+}.
Push ‘+’ in the stack
7th Step: Now i = 6 and exp[i] = ‘d’ i.e., an operand. Add this in the postfix
expression. postfix = “abc*+d”.
Add ‘d’ in the postfix
Final Step: Now no element is left. So empty the stack and add it in the postfix
expression. postfix = “abc*+d+”.
Pop ‘+’ and add it in postfix
Conversion of infix to postfix
Let's understand through an example.
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
* +* 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+.
Conversion of Infix to Prefix using Stack
K + L - M * N + (O^P) * W/U/V * T + Q
If we are converting the expression from infix to prefix, we need first to reverse the expression.
The Reverse expression would be:
Q + T * V/U/W * ) P^O(+ N*M - L + K
To obtain the prefix expression, we have created a table that consists of three columns, i.e., input
expression, stack, and prefix expression. When we encounter any symbol, we simply add it into the
prefix expression. If we encounter the operator, we will push it into the stack.
Input expression Stack Prefix expression
Q Q
+ + Q
T + QT
* +* QT
V +* QTV
/ +*/ QTV
U +*/ QTVU
/ +*// QTVU
W +*// QTVUW
* +*//* QTVUW
) +*//*) QTVUW
P +*//*) QTVUWP
^ +*//*)^ QTVUWP
O +*//*)^ QTVUWPO
( +*//* QTVUWPO^
+ ++ QTVUWPO^*//*
N ++ QTVUWPO^*//*N
* ++* QTVUWPO^*//*N
M ++* QTVUWPO^*//*NM
- ++- QTVUWPO^*//*NM*
L ++- QTVUWPO^*//*NM*L
+ ++-+ QTVUWPO^*//*NM*L
K ++-+ QTVUWPO^*//*NM*LK
QTVUWPO^*//*NM*LK+-++
The above expression, i.e., QTVUWPO^*//*NM*LK+-++, is not a final expression. We need to reverse
this expression to obtain the prefix expression.