Prefix & Postfix Expressions

Infix notation is more commonly used in human-readable mathematical expressions because it aligns with the way humans typically write and read math. However, computers often convert infix expressions into prefix or postfix (reverse Polish notation) for internal processing and evaluation. This conversion process is known as parsing or expression parsing.

Prefix expressions (Polish notations)

Prefix expressions, also known as Polish notation, are used in computers for several reasons, primarily related to their efficiency in parsing and evaluating mathematical expressions: Some of the advantage are as follows:

  1. Ease of Parsing: Prefix expressions are easier for computers to parse because they eliminate the need for parentheses to indicate the order of operations. In infix notation (the standard mathematical notation), parentheses are used to specify the precedence of operators, which can make parsing more complex. In contrast, prefix notation specifies the order of operations explicitly, making it easier for a computer to process.
  1. Reduced Ambiguity: In infix notation, expressions like “a – b * c” can be ambiguous without parentheses to clarify the order of operations. In prefix notation, there is no ambiguity because the operator always comes before its operands. For example, “- a * b c” unambiguously means “(a – (b * c))” in prefix notation.
  1. Simplified Expression Evaluation: Evaluating prefix expressions is straightforward and can be done using a stack-based algorithm. This approach allows for efficient expression evaluation, making it suitable for computer programs and calculators.
  1. Expression Trees: Prefix expressions are well-suited for representing expressions as trees, known as expression trees or parse trees. These trees are used in computer algebra systems and symbolic mathematics to manipulate and simplify expressions.
  1. Function Calls: Some programming languages, like Lisp and Scheme, use prefix notation for function calls. This allows for easy nesting of function calls and is a fundamental aspect of these languages’ syntax.

Examples of Infix to prefix

Converting an infix expression to a prefix expression involves reordering the operators and operands so that the operator precedes its operands, following the rules of operator precedence.

  1. Infix Expression: A + B                                        Prefix Expression: + A B
  2. Infix Expression: A + B * C                                   Prefix Expression: + A * B C
  3. Infix Expression: (A + B) * C                                 Prefix Expression: * + A B C
  4. Infix Expression: A + (B * C)                                 Prefix Expression: + A * B C
  5. Infix Expression: A * (B + C) / D                           Prefix Expression: / * + A B C D
  6. Infix Expression: A * (B + C / D) – E                     Prefix Expression: – * A + B / C D E

Postfix expressions (Reverse Polish Notation)

Postfix expressions, also known as Reverse Polish Notation (RPN), are used in computers for several reasons, which includes all the advantages mentioned above (like prefix expressions)

In a postfix expression, each operator is written after its operands, and the expression is evaluated from left to right. To compute the result of a postfix expression, you can use a stack-based algorithm.

It eliminates the need to worry about operator precedence and parentheses and allows for efficient stack-based processing, making it a common choice for calculators, certain programming languages (e.g., Forth), and mathematical software where efficiency and predictability in expression evaluation are important.

  1. Infix Expression: A + B                             Postfix Expression: A B +
  2. Infix Expression: A + B * C                        Postfix Expression: A B C * +
  3. Infix Expression: (A + B) * C                      Postfix Expression: A B + C *
  4. Infix Expression: A + (B * C)                      Postfix Expression: A B C * +
  5. Infix Expression: A * (B + C) / D                Postfix Expression: A B C + * D /
  6. Infix Expression: A * (B + C / D) – E          Postfix Expression: A B C D / + * E –