In: Computer Science
1) Consider the following infix expressions. What is the equivalent postfix (reverse Polish notation) expression? 16/(5+3)b) A*B+C*Dc) X × Y + W × Z + V × U
2) Consider the postfix (reverse Polish notation) 10 5 + 6 3 - /. What is the equivalent infix expression?
1) What we mean by a postfix expression is that the expression is always of the form AB op where A and B are two operands whereas op is an operator such as +, -, *, /, ^, etc. In simple words, when an operator is written and follows after every pair of operands, it is known as a postfix expression. Keep in mind that even during the conversion from infix to postfix expressions, the order of operator precedence remains the same as is in the normal case.
1a) The equivalent postfix (reverse Polish notation) expression of 16/(5+3) is 16 5 3 + /
Explanation: Let’s convert the expression 16/(5+3) using operator precedence. 5 and 3 are added first because they are inside the parenthesis ( ) and we know that ( ) has the highest precedence out of all the operator. Hence, 5+3 will be written as 5 3+ which is a postfix expression as explained in the starting. Here we can see that 5 and 3 are the operands and + is the operator. After 5 and 3 are added, then 16 is divided by the addition result of 5 and 3. Now, here again, we will have 16 / (5 3 +), and finally, we will write it in the postfix expression form as 16 5 3 + /
Infix Expression |
Postfix Expression |
---|---|
5+3 |
5 3+ |
16/5+3 |
16 5 3+ / |
1b) The equivalent postfix (reverse Polish notation) expression of A*B+C*D is A B * C D * +
Explanation: Similar explanation goes for this problem also. Here we know that A will be multiplied with B and C will be multiplied with D and finally, the result of both the products will be added to each other. This is because we know that multiplication * has higher precedence than addition +
So,
1c) The equivalent postfix (reverse Polish notation) expression of X × Y + W × Z + V × U is X Y * W Z * + V U * +
Explanation: Similar explanation goes for this problem also. Here we know that X will be multiplied with Y and W will be multiplied with Z and then V will be multiplied with U. Finally, the result of all the three products will be added to each other. This is because we know that multiplication * has higher precedence than addition +
So,
-------------------------------------------------------------------------------------------------
2) The equivalent infix expression of the postfix (reverse Polish notation) 10 5 + 6 3 - / is (10+5) / (6-3)
Explanation: The exact opposite happens when we convert a postfix expression to an infix expression. Suppose the postfix expression is written as A B + it means that its equivalent infix expression will be A+B. Hence in an infix expression, the operator (here +) is always written between the two operands (here A and B). Always keep this trick in your mind that when converting from postfix to infix expression, start scanning from left to right in the postfix expression and keep on writing the equivalent infix expressions.
So,
Thanks!