Infix And Postfix Conversion Questions

There are 21 question related to Infix And Postfix conversion for student, so that the students can practice the questions well





Also try these question


Jump To Question

Infix and Postfix Conversion in Data Structures and Algorithms (DSA)

What is Infix and Postfix Notation?

Infix Notation: This is the conventional way of writing expressions, where operators are placed between operands. For example, A + B.

Postfix Notation: Also known as Reverse Polish Notation, in postfix notation, operators are placed after the operands. For example, the infix expression A + B would be written as AB+ in postfix.

Advantages and Disadvantages of Infix and Postfix Notation

Infix:

  • Advantages: Familiar and easy for humans to read and write.
  • Disadvantages: Requires precedence rules and parentheses to determine the order of operations, which can be complex to parse in programming.

Postfix:

  • Advantages: Eliminates the need for parentheses, making it easier for computers to evaluate expressions.
  • Disadvantages: Less intuitive for humans to interpret compared to infix notation.

Why is There a Need to Convert Infix to Postfix and Postfix to Infix?

Converting infix expressions to postfix is necessary because postfix notation simplifies the computation process by eliminating ambiguity related to operator precedence. Additionally, postfix expressions are easily processed by stack-based algorithms. Converting back to infix can make expressions more readable for humans.

Method to Convert Infix to Postfix

  • Initialize an empty stack for operators and an empty list for the output.
  • Scan the infix expression from left to right.
  • If the character is an operand, add it to the output list.
  • If the character is an operator:
    • Pop operators from the stack with higher or equal precedence and add them to the output list.
    • Push the current operator onto the stack.
  • If the character is an opening parenthesis, push it onto the stack.
  • If the character is a closing parenthesis, pop operators from the stack to the output list until an opening parenthesis is encountered.
  • After scanning the expression, pop any remaining operators on the stack to the output list.

Method to Convert Postfix to Infix

  • Initialize an empty stack.
  • Scan the postfix expression from left to right.
  • If the character is an operand, push it onto the stack.
  • If the character is an operator:
    • Pop the two topmost elements from the stack.
    • Form a new expression by placing the operator between the two operands.
    • Push the new expression back onto the stack.
  • After scanning the expression, the stack should contain the complete infix expression.

49. Convert the infix expression (A + B) * (C + D) to postfix expression.

Answer:

To convert the infix expression (A + B) * (C + D) to postfix, we follow the precedence of operators and associativity.

Step-by-step conversion:

1. (A + B) becomes AB+
2. (C + D) becomes CD+
3. Now the entire expression becomes AB+CD+*.

The postfix expression is: AB+CD+*

50. Convert the infix expression A * (B + C) / D to postfix expression.

Answer:

Step-by-step conversion:

1. A * (B + C) becomes ABC+*
2. Now, divide the result by D.

The postfix expression is: ABC+*D/

51. Convert the postfix expression AB+C* to infix expression.

Answer:

In postfix, operands appear before operators, so the order of operations is important.

Step-by-step conversion:

1. A and B are operands, and + is an operator, so combine them as (A + B).
2. C is the next operand, and * is the operator, so multiply (A + B) with C.

The infix expression is: (A + B) * C

52. Convert the infix expression (A / B) + (C * D) - E to postfix expression.

Answer:

Step-by-step conversion:

1. (A / B) becomes AB/
2. (C * D) becomes CD*
3. Now add AB/ and CD*
4. Finally, subtract E.

The postfix expression is: AB/CD*+E-

53. Convert the postfix expression AB+CD+*E- to infix expression.

Answer:

Step-by-step conversion:

1. AB+ becomes (A + B)
2. CD+ becomes (C + D)
3. Now multiply (A + B) with (C + D) to get (A + B) * (C + D)
4. Subtract E from the result.

The infix expression is: ((A + B) * (C + D)) - E

54. Convert the infix expression A * B + C / D to postfix expression.

Answer:

Step-by-step conversion:

1. A * B becomes AB*
2. C / D becomes CD/
3. Now add AB* and CD/.

The postfix expression is: AB*CD/+

55. Convert the postfix expression AB*C+D/ to infix expression.

Answer:

Step-by-step conversion:

1. AB* becomes (A * B)
2. C and D are operands, and / is the operator, so combine them as (C / D)
3. Now add (A * B) and (C / D).

The infix expression is: (A * B) + (C / D)

56. Convert the infix expression (A - B) * (C + D) / E to postfix expression.

Answer:

Step-by-step conversion:

1. (A - B) becomes AB-
2. (C + D) becomes CD+
3. Multiply AB- with CD+, then divide by E.

The postfix expression is: AB-CD+*E/

57. Convert the postfix expression AB-CD+*E/ to infix expression.

Answer:

Step-by-step conversion:

1. AB- becomes (A - B)
2. CD+ becomes (C + D)
3. Multiply (A - B) with (C + D), then divide by E.

The infix expression is: ((A - B) * (C + D)) / E

58. Convert the infix expression A + B * (C - D) to postfix expression.

Answer:

Step-by-step conversion:

1. B * (C - D) becomes BCD-*
2. Now add A to the result.

The postfix expression is: ABCD-*+

59. Convert the postfix expression ABCD-*+ to infix expression.

Answer:

Step-by-step conversion:

1. BCD-* becomes B * (C - D)
2. Now add A to the result.

The infix expression is: A + (B * (C - D))

60. Convert the infix expression A * B + C - D to postfix expression.

Answer:

Step-by-step conversion:

1. A * B becomes AB*
2. Now add C, and then subtract D.

The postfix expression is: AB*C+D-

61. Convert the postfix expression AB*C+D- to infix expression.

Answer:

Step-by-step conversion:

1. AB* becomes (A * B)
2. Add C to the result, then subtract D.

The infix expression is: (A * B) + C - D

62. Convert the infix expression A + (B * C) - D to postfix expression.

Answer:

Step-by-step conversion:

1. B * C becomes BC*
2. Now add A to BC* and then subtract D.

The postfix expression is: ABC*+D-

63. Convert the postfix expression ABC*+D- to infix expression.

Answer:

Step-by-step conversion:

1. BC* becomes (B * C)
2. Add A to (B * C), then subtract D.

The infix expression is: A + (B * C) - D

64. Convert the infix expression (A - B) * (C / D) + E to postfix expression.

Answer:

Step-by-step conversion:

1. (A - B) becomes AB-
2. (C / D) becomes CD/
3. Multiply AB- with CD/ and then add E.

The postfix expression is: AB-CD/*E+

65. Convert the postfix expression AB-CD/*E+ to infix expression.

Answer:

Step-by-step conversion:

1. AB- becomes (A - B)
2. CD/ becomes (C / D)
3. Multiply (A - B) with (C / D), then add E.

The infix expression is: ((A - B) * (C / D)) + E

66. Convert the infix expression A * (B + C) - D / E to postfix expression.

Answer:

Step-by-step conversion:

1. (B + C) becomes BC+
2. Multiply A with BC+, then divide D by E and subtract.

The postfix expression is: ABC+*DE/-

67. Convert the postfix expression ABC+*DE/- to infix expression.

Answer:

Step-by-step conversion:

1. BC+ becomes (B + C)
2. Multiply A with (B + C), then subtract (D / E).

The infix expression is: (A * (B + C)) - (D / E)

68. Convert the infix expression A / B + (C * D) - E to postfix expression.

Answer:

Step-by-step conversion:

1. A / B becomes AB/
2. C * D becomes CD*
3. Add AB/ and CD*, then subtract E.

The postfix expression is: AB/CD*+E-

Also try these question