Search:     Advanced search
Browse by category:
C/C++   |   Java   |   Oracle/Database   |   SAP   |   ASP .NET   |   Mainframe-DB2   |   Freshers

What is infix, prefix, postfix? How can you convert from one representation to another? How do you evaluate these expressions?

Add comment
Views: 1413
Votes: 0
Comments: 0

There are three different ways in which an expression like a+b can be represented.

Prefix (Polish)

+ab


Postfix (Suffix or reverse polish)

ab+


Infix

a+b


Note than an infix expression can have parathesis, but postfix and prefix expressions are paranthesis free expressions.

 

Conversion from infix to postfix

Suppose this is the infix expresssion


((A + (B - C) * D) ^ E + F)


To convert it to postfix, we add an extra special value ] at the end of the infix string and push [ onto the stack.


((A + (B - C) * D) ^ E + F)]

--------->


We move from left to right in the infix expression. We keep on pushing elements onto the stack till we reach a operand. Once we reach an operand, we add it to the output. If we reach a ) symbol, we pop elements off the stack till we reach a corresponding { symbol. If we reach an operator, we pop off any operators on the stack which are of higher precedence than this one and push this operator onto the stack.

As an example


Expresssion                          Stack                    Output
----------------------------------------------------------------------------
((A + (B - C) * D) ^ E + F)]      [
^

((A + (B - C) * D) ^ E + F)]      [((                         A
  ^

((A + (B - C) * D) ^ E + F)]      [((+                        A
    ^

((A + (B - C) * D) ^ E + F)]      [((+(-                      ABC       
            ^

((A + (B - C) * D) ^ E + F)]      [(                          ABC-D*+
                 ^

((A + (B - C) * D) ^ E + F)]      [(                          ABC-D*+E^F+
                          ^

((A + (B - C) * D) ^ E + F)]      [                           ABC-D*+E^F+
                           ^


Is there a way to find out if the converted postfix expression is valid or not

Yes. We need to associate a rank for each symbol of the expression. The rank of an operator is -1 and the rank of an operand is +1. The total rank of an expression can be determined as follows:


- If an operand is placed in the post fix expression, increment the rank by 1.
- If an operator is placed in the post fix expression, decrement the rank by 1.


At any point of time, while converting an infix expression to a postfix expression, the rank of the expression can be greater than or equal to one. If the rank is anytime less than one, the expression is invalid. Once the entire expression is converted, the rank must be equal to 1. Else the expression is invalid.

 

Conversion from infix to prefix

This is very similar to the method mentioned above, except the fact that we add the special value [ at the start of the expression and ] to the stack and we move through the infix expression from right to left. Also at the end, reverse the output expression got to get the prefix expression.

 

Evaluation of a postfix expression

Here is the pseudocode. As we scan from left to right

 


If we encounter an operand, push it onto the stack.

If we encounter an operator, pop 2 operand from the stack. The first one popped is called operand2 and the second one is called operand1.

Perform Result=operand1 operator operand2.

Push the result onto the stack.

Repeat the above steps till the end of the input.

 


Convert from postfix expression to infix

This is the same as postfix evaluation, the only difference being that we wont evaluate the expression, but just present the answer. The scanning is done from left to right.


ABC-D*+

A(B-C)D*+

A((B-C)*D)+

A+((B-C)*D)

 

Convert from postfix expression to infix

The scanning is done from right to left and is similar to what we did above.


+A*-BCD

Reverse it

DCB-*A+

D(B-C)*A+

((B-C)*D)A+

A+((B-C)*D)

 

 
Others in this Category
document Write a program to have the output go two places at once (to the screen and to a file also)
document How to scan a string till we hit a new line using scanf()?
document Name some pure object oriented languages.?
document A program that demonstrates the use of variables and constants.
document Write C code to check if a given binary tree is a binary search tree or not?
document Which template must you provide, in order to display data in a Repeater control?
document What is the difference between an Interface and an Abstract class?
document What will be printed as the result of the operation below: #define swap(a,b) a=a+b;b=a-b;a=a-b; void main() { int x=5, y=10; swap (x,y); printf(“%d %d\n”,x,y); swap2(x,y); printf(“%d %d\n”,x,y); } int swap2(int a, int b) { int temp; temp=a; b=a; a=temp; return 0; }
document Write your own sqrt() function in C
document How would you count the number of bits set in a floating point number?
» More articles



RSS