You are on page 1of 26

DATA

STRUCTURES &
ALGORITHMS
Applications of Stack
Infix, Postfix and Prefix
Week 9
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
 A+B*C
 Remember: operators +, - etc.
 Operands a, b etc
POSTFIX NOTATION
 In postfix notation, the operator comes after the operand.
 For example, the infix expression A+B will be written in postfix as AB+
 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 form.
 Prefix is also called “Polish Notation.”
INFIX, POSTFIX AND PREFIX
EXPRESSIONS
 Infix:
 A+B

 Prefix:
 + AB

 Postfix
 AB +

 ‘+’ is the operator


 A and B are operands
INFIX, POSTFIX AND PREFIX
EXPRESSIONS
 Note:
 Before conversion from Infix to Postfix or Prefix, we need to know the precedence of operators

Precedence (from top to bottom)


1. Brackets [] {} ()
2. Exponential ^
3. Multiplication and Division*/
4. Addition and Subtraction +-
INFIX TO POSTFIX USING
STACK: RULES
 If operator comes, push it in the stack
 If operand comes, write it in expression
 For pushing into and popping from stack:
1. If incoming operator has a higher precedence than the one in Stack, simply push it
2. If incoming operator has lower or same precedence, then pop the operators comparing them one by one (No
two operators of same priority can stay together in the stack). After popping out all operators from stack
which have higher or same precedence, push the incoming operator in stack.
3. If opening bracket comes, simply push it without checking any precedence
4. If any operator comes right after opening bracket, then its precedence is not checked, it will be simply
pushed in the stack. However, if any operator already exists after the opening bracket, then the incoming will
be compared with it
5. If closing bracket comes pop everything until incoming bracket it reached
6. When expression ends, pop everything till the stack is empty
7. Parenthesis are not appended to expression
EXAMPLE
INFIX TO POSTFIX BY
SUBSTITUTION METHOD
A*(B+C)-D/E
=>A*x-D/E B+C=>BC+=>x
=>y-D/E A*x=>Ax*=>y
=>y-z D/E=>DE/=>z
=>yz- This is final, now start putting back values
=>Ax*z- Substituting value of y
=>Ax*DE/- Substituting value of z
=>ABC+*DE/- This is your final expression
EVALUATING EXPRESSIONS
 Consider A=10, B=30, C=9, D=10 and E=5
 Refer to video tutorial number 1 “Evaluating Postfix Expressions”
INFIX TO PREFIX
CONVERSION
 Reverse the given expression
 Find the postfix of the reversed expression
 Reverse the postfix expression
 The only difference is, if an incoming operator has the same precedence as the operator in the
stack, then here you will push the incoming operator in the stack
CONVERSION FROM INFIX TO
PREFIX USING STACK
 Please watch video link 2 with the above mentioned title.
CONVERSION FROM INFIX TO
PREFIX USING SUBSTITUTION
METHOD
 Conversion using substitution method is the same as postfix. The only difference is that you
will be putting the operator before the operands
 Please refer to slide number 21
APPLICATION OF PREFIX AND
POSTFIX
 Infix notation is easy to read for humans, whereas pre-/postfix notation is easier to parse for a
machine. The big advantage in pre-/postfix notation is that there never arise any questions like
operator precedence.
 For example, consider the infix expression 1 # 2 $ 3. Now, we don't know what those
operators mean, so there are two possible corresponding postfix expressions: 1 2 # 3 $ and 1 2
3 $ #. Without knowing the rules governing the use of these operators, the infix expression is
essentially worthless.
 Or, to put it in more general terms: it is possible to restore the original (parse) tree from a
pre-/postfix expression without any additional knowledge, but the same isn't true for infix
expressions.

You might also like