# Infix, Postfix and Prefix

Infix, Postfix and Prefix notations are three different but equivalent ways of writing expressions. It is easiest to demonstrate the differences by looking at examples of operators that take two operands.

**Infix notation: X + Y**

Operators are written in-between their operands. This is the usual way we write expressions. An expression such as A * ( B + C ) / D is usually taken to mean something like: “First add B and C together, then multiply the result by A, then divide by D to give the final answer.”Infix notation needs extra information to make the order of evaluation of the operators clear: rules built into the language about operator precedence and associativity, and brackets ( ) to allow users to override these rules. For example, the usual rules for associativity say that we perform operations from left to right, so the multiplication by A is assumed to come before the division by D. Similarly, the usual rules for precedence say that we perform multiplication and division before we perform addition and subtraction.

**Postfix notation (also known as “Reverse Polish notation”): X Y +**

Operators are written after their operands. The infix expression given above is equivalent to A B C + * D /

The order of evaluation of operators is always left-to-right, and brackets cannot be used to change this order. Because the “+” is to the left of the “*” in the example above, the addition must be performed before the multiplication.

Operators act on values immediately to the left of them. For example, the “+” above uses the “B” and “C”. We can add (totally unnecessary) brackets to make this explicit:

( (A (B C +) *) D /)

Thus, the “*” uses the two values immediately preceding: “A”, and the result of the addition. Similarly, the “/” uses the result of the multiplication and the “D”.

**Prefix notation (also known as “Polish notation”): + X Y**

Operators are written before their operands. The expressions given above are equivalent to / * A + B C D

As for Postfix, operators are evaluated left-to-right and brackets are superfluous. Operators act on the two nearest values on the right. I have again added (totally unnecessary) brackets to make this clear:

(/ (* A (+ B C) ) D)

Although Prefix “operators are evaluated left-to-right”, they use values to their right, and if these values themselves involve computations then this changes the order that the operators have to be evaluated in. In the example above, although the division is the first operator on the left, it acts on the result of the multiplication, and so the multiplication has to happen before the division (and similarly the addition has to happen before the multiplication).

Because Postfix operators use values to their left, any values involving computations will already have been calculated as we go left-to-right, and so the order of evaluation of the operators is not disrupted in the same way as in Prefix expressions.

In all three versions, the operands occur in the same order, and just the operators have to be moved to keep the meaning correct. (This is particularly important for asymmetric operators like subtraction and division: A – B does not mean the same as B – A; the former is equivalent to A B – or – A B, the latter toB A – or – B A).

In all three versions, the operands occur in the same order, and just the operators have to be moved to keep the meaning correct. (This is particularly important for asymmetric operators like subtraction and division: A – B does not mean the same as B – A; the former is equivalent to A B – or – A B, the latter to`B A - or - B A).`

Examples:

Infix | Postfix | Prefix | Notes |
---|---|---|---|

A * B + C / D | A B * C D / + | + * A B / C D | multiply A and B, divide C by D, add the results |

A * (B + C) / D | A B C + * D / | / * A + B C D> | add B and C, multiply by A, divide by D |

A * (B + C / D) | A B C D / + * | * A + B / C D | divide C by D, add B, multiply by A |

/*Q9-WAP In C++ To Convert The Infix Expression To Postfix Equivalent*/ #include<iostream.h> #include<conio.h> #include<stdio.h> #include<string.h> #include<stdlib.h> const int size=50; char infix[size],postfix[size],stack[size]; int top=-1; int precedence(char ch); char pop(); char topelement(); void push(char ch); int braces(char *); void main() { char ele,elem,st[2]; int prep,pre,popped,j=0,chk=0; strcpy(postfix," "); clrscr(); cout<<"\nEnter Infix Expression:"; gets(infix); if(chk!=0) { cout<<"\nUnbalanced no. of braces.\nExtra"; cout<<(chk==1?"right braces":"left braces")<<endl; getch(); exit(1); } for(int i=0;infix[i]!='\0';i++) { if(infix[i]!='('&& infix[i]!=')' && infix[i]!='^' && infix[i]!='*' && infix[i]!='/' && infix[i]!='+' && infix[i]!='-') { postfix[j++]=infix[i]; } else if(infix[i]=='(') { elem=infix[i]; push(elem); } else if(infix[i]==')') { while(popped=pop())!='(') {postfix[j++]=popped; } } else {elen=infix[i]; pre=precedence(elem); ele=topelement(); prep=precedence(ele); if(pre>prep) push(elem); else {while(prep>=pre) { if(ele=='#') break; popped=pop(); ele=topelement(); postfix[j++]=popped; prep=precedence(ele); } push(elem); } } } while(popped=pop())!='#') postfix[j++]=popped; postfix[j]='\0'; cout<<"\nPostfix:"<<postfix<<endl; } int precedence(char ch) { switch(ch) {case '^':return 5; case '/':return 4; case '*':return 4; case '+':return 3; case '-':return 3; default:return 0; } } char pop() { char ret; if(top!=-1) { ret=stack[top]; top--; return ret; } else return '#'; } char topelement() { char ch; if(top!=-1) ch=stack[top]; else ch='#'; return ch; } void push(char ch) {if(top!=size-1) {top++; stack[top]=ch; } } int braces(char *s) { int leftbr,rightbr; leftbr=rightbr=0; for(int i=0;s[i];i++) { if(s[i]=='(') leftbr++; else if(s[i]==')') rightbr++; } if(leftbr==rightbr) return 0; else if(leftbr<rightbr) return 1; else return-1; }