Data Structures

Convert infix expression into postfix Expression

Infix notation:
In most common arithmetic opeartions,the opeartor is placed b/w the two operands.
1) A+B 2)C-D 3) E*F 4) G/H 5)(a+b)*c
It is called infix notation,it can be parantheised or parantheses free expressions.

Polish /Prefix notation:
It is named after the polish mathematician Jan Lukasiewicz,refers to the notation in which the
operator symbol is placed before its two operations.
Example: +AB, -CD, *+ABC

Postfix/Suffix/Reverse polish notation:
In which operator is placed after the operands.
AB+ CD* AB+C* ABC*+

  • Computer usually evaluates an arithmetic expression written in infix notation in two steps
    i)it converts the expression into postfix notation
    ii)then it evaluate that expression.
  • stack is the main tool used to accomplish this task.

Procedure to convert infix expression into postfix Expression using stack

  • read one input symbol at a time from the array of characters(infix expression)
  • if a input symbol is an operand .write it into output(postfix)
  •  if a input symbol is an operator,push it in to stack,if the stack is empty.if stack is not empty compare the precedence of stack symbol and input symbol. pop all the stack symbol having
    higher or equal priority(write poped entries into stack) and then only push operator in to stack.
  •  If the input symbol is left parentheses ‘(‘ push it into stack.
  • If input symbol is right parentheses ‘)”,pop entries of stack and write those entries into output till
    you find “(‘ in stack. (don’t write ‘(‘ into output).
  • When you finish reading the string,pop all the left out symbols from the stack and store in output.

Ex: convert (A*B)+C into postfix using stack.

Now the infix expression is ended: check out the status of stack if it is not empty pop the entries of stack till it become empty and write all the poped entries into output. After making stack empty,
The postfix expression of (A+B)*C= AB*C+

Leave a comment

Your email address will not be published. Required fields are marked *