Infix, Postfix, Prefix
Infix, Postfix, Prefix
Expressions
• An expression consists of constants, variables, and symbols. Symbols
can be operators or parenthesis. All these components must be
arranged according to a set of rules so that all these expressions can
be evaluated using the set of rules.
• Examples of expressions are:
• 5+6
• A-B
• (P * 5)
What is infix notation?
• The postfix expression is an expression in which the operator is written after the
operands. For example, the postfix expression of infix notation ( 2+3) can be
written as 23+.
• For example:
• If the infix expression is A + B * C
• As we know that the multiplication operator has a higher precedence than the
addition operator, so multiplication operator will move after the operands B and
C shown as below:
• A+BC*
• Once the multiplication operator is moved after the operand C, then the addition
operator will come after the multiplication operator shown as below:
• ABC*+
Postfix Expression
• Some key points regarding the postfix expression are:
• In postfix expression, operations are performed in the order in which
they have written from left to right.
• It does not any require any parenthesis.
• We do not need to apply operator precedence rules and associativity
rules.
Examples post to infix
• Input : abc++
• Output : (a + (b + c))
• Input : ab*c+
• Output : ((a*b)+c)
• bc*cd+-(postfix)
• ((b*c)-(c+d))(infix)
Example prefix to infix
• Input : Prefix : *+AB-CD
• Output : Infix : ((A+B)*(C-D))
• *+a-bc/-de+-fgh(prefix)
• (a+b-c)*(d-e)/(f-g+h)(infix)
Rules for the conversion from infix to postfix expression using
stack
• Scan all the symbols one by one from left to right in the given Infix Expression.
• If the reading symbol is operand, then immediately append it to the Postfix
Expression .
• If the reading symbol is left parenthesis ‘( ‘, then Push it onto the Stack.
• If the reading symbol is right parenthesis ‘)’, then Pop all the contents of the stack
until the respective left parenthesis is popped and append each popped symbol to
Postfix Expression.
• If the reading symbol is operator (+ , – , * , /), then Push it onto the Stack.
However, first pop the operators which are already on the stack that have higher
or equal precedence than the current operator and append them to the postfix .
• If open parenthesis is there on top of the stack then push the operator into the
stack.
• If the input is over, pop all the remaining symbols from the stack and append them
to the postfix .
Infix to prefix using stack
• initially reverse the expression given for the infix.
• One by one scan of characters.
• Is if character is an operand, then copy it to the output of the prefix notation.
• When the character is a parenthesis closing then push it to the stack.
• If the character is an opening parenthesis, pop the elements in the stack till we find the
closing parenthesis that corresponds.
• If a character scanned is operator.
• If the operator has greater or equal precedence than the top of the stack, push the
operator to the stack.
• If the operator has less precedence than the top of the stack, pop the operator and
output it to the output of the prefix notation, then check the above condition with the
new top of the stack again.
• After scanning all the characters, reverse the notation output for the prefix.