## Convert Infix to Postfix notationBefore understanding the conversion from infix to postfix notation, we should know about the infix and postfix notations separately. An infix and postfix are the expressions. An expression consists of constants, variables, and symbols. Symbols can be operators or parenthesis. All these components must be arranged according to a set of rules so that all these expressions can be evaluated using the set of rules.
All the above expressions have a common structure, i.e., we have an operator between the two operands. An Operand is an object or a value on which the operation is to be performed. In the above expressions, 5, 6 are the operands while '+', '-', and '*' are the operators. ## What is infix notation?When the operator is written in between the operands, then it is known as
(p + q) * (r + s) In the above expression, both the expressions of the multiplication operator are the operands, i.e., In the above expression, there are three operators. The operands for the first plus operator are p and q, the operands for the second plus operator are r and s. While performing the
If there is only one operator in the expression, we do not require applying any rule. For example, 5 + 2; in this expression, addition operation can be performed between the two operands (5 and 2), and the result of the operation would be 7. If there are multiple operators in the expression, then some rule needs to be followed to evaluate the expression. If the expression is: 4 + 6 * 2 If the plus operator is evaluated first, then the expression would look like: 10 * 2 = 20 If the multiplication operator is evaluated first, then the expression would look like: 4 + 12 = 16 The above problem can be resolved by following the operator precedence rules. In the algebraic expression, the order of the operator precedence is given in the below table:
The first preference is given to the parenthesis; then next preference is given to the exponents. In the case of multiple exponent operators, then the operation will be applied from right to left.
2^2^3 = 2 ^ 8 = 256 After exponent, multiplication, and division operators are evaluated. If both the operators are present in the expression, then the operation will be applied from left to right. The next preference is given to addition and subtraction. If both the operators are available in the expression, then we go from left to right. The operators that have the same precedence termed as ## Problem with infix notationTo evaluate the infix expression, we should know about the ## Postfix ExpressionThe postfix expression is an expression in which the operator is written after the operands. For example, the postfix expression of infix notation ( 2+3) can be written as 23+. Some key points regarding the postfix expression are: - In postfix expression, operations are performed in the order in which they have written from left to right.
- It does not any require any parenthesis.
- We do not need to apply operator precedence rules and associativity rules.
- Scan the expression from left to right until we encounter any operator.
- Perform the operation
- Replace the expression with its computed value.
- Repeat the steps from 1 to 3 until no more operators exist.
Infix expression: 2 + 3 * 4 We will start scanning from the left most of the expression. The multiplication operator is an operator that appears first while scanning from left to right. Now, the expression would be: Expression = 2 + 34* = 2 + 12 Again, we will scan from left to right, and the expression would be: Expression = 2 12 + = 14 ## Evaluation of postfix expression using stack.- Scan the expression from left to right.
- If we encounter any operand in the expression, then we push the operand in the stack.
- When we encounter any operator in the expression, then we pop the corresponding operands from the stack.
- When we finish with the scanning of the expression, the final value remains in the stack.
The result of the above expression is 14.
The result of the above expression is 22.
- Read a character
- If the character is a digit, convert the character into int and push the integer into the stack.
- If the character is an operator,
- Pop the elements from the stack twice obtaining two operands.
- Perform the operation
- Push the result into the stack.
## Conversion of infix to postfixHere, we will use the stack data structure for the conversion of infix expression to prefix expression. Whenever an operator will encounter, we push operator into the stack. If we encounter an operand, then we append the operand to the expression.
- Print the operand as they arrive.
- If the stack is empty or contains a left parenthesis on top, push the incoming operator on to the stack.
- If the incoming symbol is '(', push it on to the stack.
- If the incoming symbol is ')', pop the stack and print the operators until the left parenthesis is found.
- If the incoming symbol has higher precedence than the top of the stack, push it on the stack.
- If the incoming symbol has lower precedence than the top of the stack, pop and print the top of the stack. Then test the incoming operator against the new top of the stack.
- If the incoming operator has the same precedence with the top of the stack then use the associativity rules. If the associativity is from left to right then pop and print the top of the stack then push the incoming operator. If the associativity is from right to left then push the incoming operator.
- At the end of the expression, pop and print all the operators of the stack.
The final postfix expression of infix expression(K + L - M*N + (O^P) * W/U/V * T + Q) is KL+MN*-OP^W*U/V/T*+Q+. Next TopicConvert infix to prefix notation |