Prefix And Postfix Conversion Questions

These questions are generally asked in university exam

infix, prefix based stack question




Also try these question



Jump To Question

Prefix and Postfix Notation in DSA

Prefix and postfix are two ways to write expressions in which the operator is positioned either before (prefix) or after (postfix) the operands. These notations are particularly useful in the evaluation of expressions without the need for parentheses to dictate order of operations.

Method to Convert Prefix to Postfix and Vice-Versa

  • Prefix to Postfix:
    • Read the prefix expression from right to left.
    • If an operand is encountered, push it onto a stack.
    • If an operator is encountered, pop the top two operands from the stack.
    • Combine the operands with the operator in postfix form and push the result back onto the stack.
    • The final element left in the stack is the postfix expression.
  • Postfix to Prefix:
    • Read the postfix expression from left to right.
    • If an operand is encountered, push it onto a stack.
    • If an operator is encountered, pop the top two operands from the stack.
    • Combine the operands with the operator in prefix form and push the result back onto the stack.
    • The final element left in the stack is the prefix expression.

Advantages and Disadvantages

Notation Advantages Disadvantages
Prefix
  • No need for parentheses to dictate order.
  • Evaluation is straightforward using a stack.
  • Unambiguous, as each operator follows its operands.
  • Less intuitive for human reading and writing.
  • More complex parsing compared to infix notation.
Postfix
  • Similar advantages to prefix, with easy evaluation.
  • Allows for easier implementation in calculators.
  • Also unambiguous, eliminating confusion with operator precedence.
  • Like prefix, can be less intuitive for humans.
  • Requires extra processing to convert to infix for readability.

Need for Conversion

Converting between prefix and postfix notation is essential for various applications, including compilers and expression evaluators. The conversion enables different representations of expressions, optimizing them for specific evaluation strategies and improving efficiency in computations where one form might be more advantageous than the other.

69. Convert the following prefix expression to postfix: * + A B - C D

Answer:

The given prefix expression is * + A B - C D. In order to convert it to a postfix expression, we follow the reverse process of the prefix to postfix conversion.

Step 1: Reverse the prefix expression: D C - B A + *

Step 2: Use a stack to evaluate the expression from left to right.

Result: A B + C D - *.

70. Convert the following postfix expression to prefix: AB+CD-*

Answer:

The given postfix expression is AB+CD-*.

Step 1: Read the postfix expression from left to right and use a stack to evaluate it.

Step 2: Push operands onto the stack. When encountering an operator, pop the required number of operands, and create a new prefix expression.

Final prefix expression: * + A B - C D.

71. Convert the prefix expression / * A B + C D to a postfix expression.

Answer:

The prefix expression is / * A B + C D.

Step 1: Reverse the prefix expression: D C + B A * /

Step 2: Evaluate from left to right using a stack.

Postfix expression: A B * C D + /.

72. Convert the postfix expression ABC+*D/ to prefix.

Answer:

The given postfix expression is ABC+*D/.

Step 1: Read the postfix expression from left to right, pushing operands onto the stack.

Step 2: When encountering operators, pop operands and form a prefix expression.

Prefix expression: / * A + B C D.

73. Convert the prefix expression - * A B + C D to postfix.

Answer:

The prefix expression is - * A B + C D.

Step 1: Reverse the prefix expression: D C + B A * -

Step 2: Using a stack, evaluate from left to right.

Postfix expression: A B * C D + -.

74. Convert the postfix expression AB*CD+/ to prefix.

Answer:

The given postfix expression is AB*CD+/.

Step 1: Read the expression from left to right, push operands onto a stack.

Step 2: Use the stack to evaluate when encountering operators.

Prefix expression: / * A B + C D.

75. Convert the prefix expression + A / B C to postfix.

Answer:

The prefix expression is + A / B C.

Step 1: Reverse the prefix expression: C B / A +

Step 2: Use a stack to evaluate.

Postfix expression: A B C / +.

76. Convert the postfix expression ABC+/D* to prefix.

Answer:

The given postfix expression is ABC+/D*.

Step 1: Push operands onto the stack.

Step 2: Pop operands and create prefix expressions as operators appear.

Prefix expression: * + A B / C D.

77. Convert the prefix expression - / A B + C D to postfix.

Answer:

The prefix expression is - / A B + C D.

Step 1: Reverse the prefix expression: D C + B A / -

Step 2: Evaluate using a stack.

Postfix expression: A B / C D + -.

78. Convert the postfix expression ABC+D/- to prefix.

Answer:

The given postfix expression is ABC+D/-.

Step 1: Push operands onto the stack.

Step 2: Use the stack to create prefix expressions as operators appear.

Prefix expression: - / A B + C D.

79. Convert the prefix expression * A + B C to postfix.

Answer:

The prefix expression is * A + B C.

Step 1: Reverse the prefix expression: C B + A *

Step 2: Evaluate using a stack.

Postfix expression: A B C + *.

80. Convert the postfix expression AB+CD-* to prefix.

Answer:

The given postfix expression is AB+CD-*.

Step 1: Push operands onto the stack.

Step 2: Pop operands and form prefix expressions.

Prefix expression: * + A B - C D.

81. Convert the prefix expression / - A B + C D to postfix.

Answer:

The prefix expression is / - A B + C D.

Step 1: Reverse the prefix expression: D C + B A - /

Step 2: Evaluate using a stack.

Postfix expression: A B - C D + /.

82. Convert the postfix expression AB-CD+* to prefix.

Answer:

The given postfix expression is AB-CD+*.

Step 1: Push operands onto the stack.

Step 2: Use the stack to form prefix expressions as operators appear.

Prefix expression: * - A B + C D.

83. Convert the prefix expression + A * B C to postfix.

Answer:

The prefix expression is + A * B C.

Step 1: Reverse the prefix expression: C B * A +

Step 2: Evaluate using a stack.

Postfix expression: A B C * +.

84. Convert the postfix expression ABC*- to prefix.

Answer:

The given postfix expression is ABC*-.

Step 1: Push operands onto the stack.

Step 2: Use the stack to form prefix expressions as operators appear.

Prefix expression: - A * B C.

85. Convert the prefix expression * - A B + C D to postfix.

Answer:

The prefix expression is * - A B + C D.

Step 1: Reverse the prefix expression: D C + B A - *

Step 2: Evaluate using a stack.

Postfix expression: A B - C D + *.

86. Convert the postfix expression AB*CD+/ to prefix.

Answer:

The given postfix expression is AB*CD+/.

Step 1: Push operands onto the stack.

Step 2: Use the stack to form prefix expressions as operators appear.

Prefix expression: / * A B + C D.

87. Convert the prefix expression / + A B - C D to postfix.

Answer:

The prefix expression is / + A B - C D.

Step 1: Reverse the prefix expression: D C - B A + /

Step 2: Use a stack to evaluate.

Postfix expression: A B + C D - /.

88. Convert the postfix expression AB+CD-* to prefix.

Answer:

The given postfix expression is AB+CD-*.

Step 1: Push operands onto the stack.

Step 2: Use the stack to form prefix expressions as operators appear.

Prefix expression: * + A B - C D.

Also try these question