Data Structures
Unit 3
Stacks
Infix , Prefix and Postfix Notation
Infix Notation
Infix, Postfix and Prefix notations are three different but equivalent notations of writing
algebraic expressions.
While writing an arithmetic expression using infix notation, the operator is placed
between the operands. For example, A+B; here, plus operator is placed between the two
operands A and B.
Although it is easy to write expressions using infix notation, computers find it difficult to
parse as they need a lot of information to evaluate the expression.
Information is needed about operator precedence, associativity rules, and brackets
which overrides these rules.
So, computers work more efficiently with expressions written using prefix and postfix
notations.
Postfix Notation
Postfix notation was given by Jan Łukasiewicz who was a Polish logician, mathematician, and philosopher.
His aim was to develop a parenthesis-free prefix notation (also known as Polish notation) and a postfix
notation which is better known as Reverse Polish Notation or RPN.
In postfix notation, the operator is placed after the operands.
• For example, if an expression is written as A+B in infix notation, the same expression can be written
as AB+ in postfix notation.
The order of evaluation of a postfix expression is always from left to right.
Postfix Notation
The expression (A + B) * C is written as:
• AB+C* in the postfix notation.
A postfix operation does not even follow the rules of operator
precedence. The operator which occurs first in the expression is operated
first on the operands.
• For example, given a postfix notation AB+C*. While evaluation,
addition will be performed prior to multiplication.
Prefix Notation
In a prefix notation, the operator is placed before the operands.
• For example, if A+B is an expression in infix notation, then the corresponding
expression in prefix notation is given by +AB.
While evaluating a prefix expression, the operators are applied to the operands that
are present immediately on the right of the operator.
Prefix expressions also do not follow the rules of operator precedence, associativity,
and even brackets cannot alter the order of evaluation.
• The expression (A + B) * C is written as: *+ABC in the prefix notation
Evaluation of an Infix Expression
STEP 1: Convert the infix expression into its equivalent postfix expression
Algorithm to convert an Infix notation into postfix notation
Step 1: Add ‘)” to the end of the infix expression
Step 2: Push “(“ on to the stack
Step 3: Repeat until each character in the infix notation is scanned
IF a “(“ is encountered, push it on the stack
IF an operand (whether a digit or an alphabet) is encountered,
add it to the postfix expression.
IF a “)” is encountered, then;
a. Repeatedly pop from stack and add it to the postfix expression
until a “(” is encountered.
b. Discard the “(“. That is, remove the “(“ from stack and do not
add it to the postfix expression
IF an operator X is encountered, then;
a Repeatedly pop from stack and add each operator (popped from the
stack) to the postfix expression which has the same precedence or a
higher precedence than X
b. Push the operator X to the stack
Step 4: Repeatedly pop from the stack and add it to the postfix expression
until the stack is empty
Step 5: EXIT
Evaluation of an Infix Expression
STEP 2: Evaluate the postfix expression
Algorithm to evaluate a postfix expression
Step 1: Add a “)” at the end of the postfix expression
Step 2: Scan every character of the postfix expression and repeat
steps 3 and 4 until “)”is encountered
Step 3: IF an operand is encountered, push it on the stack
IF an operator X is encountered, then
a. pop the top two elements from the stack as A and B
b. Evaluate B X A, where A was the topmost element and B was
the element below A.
c. Push the result of evaluation on the stack
[END OF IF]
Step 4: SET RESULT equal to the topmost element of the stack
Step 5: EXIT
Evaluation of an Infix Expression
• Let us now take an example that makes use of this algorithm.
• Consider the infix expression given as “9 - (( 3 * 4) + 8) / 4”.
• The infix expression "9 - (( 3 * 4) + 8) / 4" can be written as “9 3 4 * 8
+ 4 / -“ using postfix notation.
• Look at table which shows the procedure.
Character scanned Stack
9 9
3 9, 3
4 9, 3, 4
* 9, 12
8 9, 12, 8
+ 9, 20
4 9, 20, 4
/ 9, 5
- 4
Convert Infix Expression into Prefix
Expression
Consider an infix expression: (A – B / C) * (A / K – L)
Step 1: Reverse the infix string. Note that while reversing the string you
must interchange left and right parenthesis.
• (L – K / A) * (C / B – A)
Step 2: Obtain the corresponding postfix expression of the infix expression
obtained as a result of Step 1.
• The expression is: (L – K / A) * (C / B – A)
• Therefore, [L – (K A /)] * [ (C B /) - A ]
• = [LKA/-] * [ CB/A-]
• =LKA/-CB/A-*
Step 3: Reverse the postfix expression to get the prefix expression
• Therefore, the prefix expression is * - A / B C - / A K L