Skip to main content

Posts

Showing posts with the label Infix expression

Conversion of postfix expression to infix

How do we convert a postfix expression to infix expression? A postfix expression or a "Reverse polish notation" is useful for programming. Because our programs can easily evaluate a postfix expression. But for humans, postfix expressions are difficult to understand. So is it possible to convert a given postfix expression to infix expression? Why not? It is possible with the help of stack data structure.  Remember that when converting an expression from infix to postfix , we used operator stack. But in this case we need an operand stack. What we need to do is - we extract values from expression. If there is an operand, we push it to stack. If there is an operator encountered, we pop two most recent values from stack, apply operator to them, enclose them in paranthesis and push the expression back to stack. This procedure is continued until the entire expression is scanned. In the next step, the content of stack is popped out - which will be our infix expression. So let me bul...

Infix, prefix and postfix notations

Mathematical expressions can be written in 3 different methods - infix, postfix and prefix. Infix notation All these days we have been using infix notation. In this operators are written between the operands. Some operators are having higher priorities than others. If some expressions are enclosed in brackets, then they are evaluated first. e.g.     (A+B)*C Here + operator will operate on A and B as it is between them. Though * has higher priority, as A+B is enclosed in brackets, that is evaluated before multiplication. Postfix notation In a postfix notation - also called Reverse Polish Notation, operator is written after the operands. The operator operates on two operands to its immediate left. And expressions are always evaluated from left to right. Brackets do not have any effect on this. e.g. (A+B)*C in postfix is  AB+C* A+B*C+D in postfix is ABC*+D+ Prefix notation In a prefix notation, operator is written before the operands. And just like postfix, expressions ...