CS DataStructure-Lecture 6 - Polish Notation
CS DataStructure-Lecture 6 - Polish Notation
CS DataStructure-Lecture 6 - Polish Notation
if(op=='+'||op=='-')return 1;
if(op=='/'||op=='*')return 2;
if(op=='^')return 3;
return 0;
}
void infix_to_post(char infix[],char post[]){
Stack s;
createStack(&s);
char e,t=';';
int i=0,j=0;
for(;infix[i]!='\0';i++){
e=infix[i];
if(isDigit(e)){
post[j]=e;
j++;
}
else if(e=='('){push(e,&s);}
else if(e==')'){
while(t!='('){
pop(&t,&s);
if(t!='('){
post[j]=t;
j++;
}
}
}
else {
while(!EmptyStack(&s)&&precednce(stackTop(&s))>=precednce(e))
{
pop(&t,&s);
post[j]=t;
j++;
}
push(e,&s);
}
}//End for the for loop “end of string”
while(!EmptyStack(&s)){
pop(&t,&s);
post[j]=t;
j++;
}
post[j]='\0';
return post;
}
Evaluation of Postfix Expression
• Create a stack to store operands (or values).
• Scan the given expression from left to right and do the
following for every scanned element.
• If the element is a number, push it into the stack.
• If the element is an operator, pop operands for the operator from the
stack. Evaluate the operator and push the result back to the stack.
• When the expression is ended, the number in the stack is the
final answer.
Example
• Consider the expression: exp = “2 3 1 * + 9 -“
• Scan 2, it’s a number, So push it into stack. Stack contains ‘2’.
• Scan 3, again a number, push it to stack, stack now contains ‘2
3’ (from bottom to top)
• Scan 1, again a number, push it to stack, stack now contains ‘2
3 1’
• Scan *, it’s an operator. Pop two operands from stack, apply
the * operator on operands. We get 3*1 which results in 3. We
push the result 3 to stack. The stack now becomes ‘2 3’.
Example
• Scan +, it’s an operator. Pop two operands from stack, apply the +
operator on operands. We get 3 + 2 which results in 5. We push the
result 5 to stack. The stack now becomes ‘5’.
• Scan 9, it’s a number. So we push it to the stack. The stack now
becomes ‘5 9’.
• Scan -, it’s an operator, pop two operands from stack, apply the –
operator on operands, we get 5 – 9 which results in -4. We push
the result -4 to the stack. The stack now becomes ‘-4’.
• There are no more elements to scan, we return the top element
from the stack (which is the only element left in a stack).
• So the result becomes -4.
Implementation
float opval(char op,double val1,double val2){
if(op=='+')return val1+val2;
if(op=='-')return val1-val2;
if(op=='/')return val1/val2;
if(op=='*')return val1*val2;
if(op=='^')return pow(val1,val2);
}
float infixEval(char infix[]){
int i=0;
Stack s;
char e,val1,val2;
float res;
createStack(&s);
for(;infix[i];i++){
e=infix[i];
if(isDigit(e)){
push(e,&s);
}
else{
pop(&val2,&s) ;
pop(&val1,&s) ;
push(opval(e,(float)val1-'0',(float)val2-'0')+'0',&s);
}
}
pop(&val1,&s);
res=(float)(val1-'0');
return res;
}
int main()
{
char in[50]="",post[50]="";
printf("Enter infix String:\n ");
gets(in);
infix_to_post(in,post);
printf("The postfix for %s is =%s",in,post);
printf("\nthe eval of %s is =%f \n ",post,infixEval(post));
return 0;
}
Infix To Prefix Algorithm
• First, reverse the order of the infix expression. For example, if the infix expression
is 4*3+(5/2), the reverse would be )2/5(+3*4.
• Next, working from left to right on the reversed infix expression, one token at a
time, determine whether or not the current token is an operand, an operator, or
an opening or closing parenthesis.
• If the token is an operand, append it to the output. Operands always appear in
the same order in the output as they do in the infix expression.
• If the token is a closing parenthesis, push it to the stack. This marks the
beginning of an expression that should be evaluated separately.
• If the token is an opening parenthesis, until a closing parenthesis is
encountered at the top of the stack, pop each operator from the stack and
append to the output. This marks the end of the operands and operators located
within the current set of parentheses.
• If the token is an operator and it has greater precedence than the operator
on the top of the stack, push the token to the stack.
Infix To Prefix Algorithm
• If the token is an operator and it has lower precedence than the operator on the top
of the stack, until the operator on top of the stack has lower or equal precedence than
the token, or until the stack is empty, pop each operator from the stack and append them
to the output. Then push the current token to the stack.
• If the token is an operator and it has the same precedence as the operator on the
top of the stack, and the operator on top of the stack is left-to-right
associative, push the token to the stack.
• If the token is an operator and it has the same precedence as the operator on the
top of the stack, but the operator on top of the stack is right-to-left associative, until
the operator on top of the stack has lower or equal precedence than the token and is left-
to-right associative, or until the stack is empty, pop each operator from the stack and
append them to the output. Then push the current token to the stack.
• Once all of the infix tokens have been evaluated, pop any remaining operators from the
stack, one at a time, and append each of them to the output expression.
• Finally, reverse the order of the output expression to get the prefix expression.
• Example #1: A+B*C+D
• Reverse Infix Expression:
• D+C*B+A
• Scan tokens one at a time from left to right.
Character Stack Ouput
D D
Since D is an operand, print to output.
+ + D
Since + is an operator, and the stack is empty, push it to the stack.
C + DC
Since C is an operand, print to output.
* +* DC
Since * is an operator, and it has higher precedence than the + at the top of the stack, push * to the stack.
B +* DCB
Since B is an operand, print to output.
+ ++ DCB*
Since + is an operator, and it has lower precedence than the * at the top of the stack, pop * from the stack and append it to the
output, and then push + to the stack.
A ++ DCB*A
Since A is an operand, print to output.
Scanning complete.
Pop remaining 2 operators from stack, one at a time, and append to output.
+ DCB*A+
DCB*A++
Reverse final output.
Prefix: ++A*BCD
Example
• Infix =2*(1+(4*(2+1)+3))
• First Reverse the string :
•))3+)1+2(*4(+1(*2
Character Stack Ouput
) )
) ))
3 )) 3
+ ))+ 3
) ))+) 3
1 ))+) 31
+ ))+)+ 31
2 ))+)+ 312
( ))+ 312+
* ))+* 312+
4 ))+* 312+4
( ) 312+4*+
+ )+ 312+4*+
1 )+ 312+4*+1
( 312+4*+1+
* * 312+4*+1+
2 * 312+4*+1+2
312+4*+1+2*
Prefix: *2+1+*4+213
References
• Evaluation of Postfix Expression – GeeksforGeeks
• Convert Infix expression to Postfix expression – GeeksforGeeks
• Postfix to Infix – GeeksforGeeks
• Data Structure - Expression Parsing (tutorialspoint.com)
• https://www.free-online-calculator-use.com/infix-to-prefix-
converter.html