Overview

Prefix, infix, and postfix notations are different ways of writing mathematical expressions. Each has its own advantages and is particularly useful in specific contexts in computer science.

Key Concepts:

  • Infix Notation: The standard mathematical notation where operators are placed between operands (e.g., 5 + 3)
  • Prefix Notation: Also known as Polish notation, where operators are placed before operands (e.g., + 5 3)
  • Postfix Notation: Also known as Reverse Polish notation, where operators are placed after operands (e.g., 5 3 +)

Key Concepts

Infix Notation

This is the standard mathematical notation we use in everyday calculations. While it's the most natural for humans to read, it requires precedence rules and parentheses to avoid ambiguity.

Order of Precedence (PEMDAS):

  1. Parentheses
  2. Exponentiation
  3. Multiplication and Division (left to right)
  4. Addition and Subtraction (left to right)

Prefix Notation (Polish Notation)

In prefix notation, the operator is placed before its operands. This notation eliminates the need for parentheses and operator precedence rules.

Postfix Notation (Reverse Polish Notation)

In postfix notation, the operator follows its operands. Like prefix notation, it eliminates the need for parentheses and operator precedence rules.

Converting Prefix and Postfix to Infix

Converting prefix and postfix expressions to infix notation is useful for understanding and verifying expressions. The key is to work systematically, identifying operators and their operands.

Key Tips:

  • Prefix: Read right to left. Each operator applies to the two operands immediately following it.
  • Postfix: Read left to right. Each operator applies to the two operands immediately preceding it.
  • Always add parentheses around each operation to maintain the correct order of operations in infix notation.

Examples

Evaluating an Infix Expression

5 + 8/(3-1)

Evaluation steps:

  1. Evaluate parentheses: (3-1) = 2
  2. Perform division: 8/2 = 4
  3. Perform addition: 5 + 4 = 9
Converting from Infix to Prefix

5 + 8/(3-1) → + 5 / 8 - 3 1

Steps for conversion:

  1. Start with fully parenthesized infix: (5 + (8 / (3 - 1)))
  2. Convert innermost parentheses first:
    (3 - 1) → - 3 1
  3. Convert next operation:
    (8 / (- 3 1)) → / 8 - 3 1
  4. Convert final operation:
    (5 + (/ 8 - 3 1)) → + 5 / 8 - 3 1
Converting from Infix to Postfix

5 + 8/(3-1) → 5 8 3 1 - / +

Steps for conversion:

  1. Start with fully parenthesized infix: (5 + (8 / (3 - 1)))
  2. Convert innermost parentheses first:
    (3 - 1) → 3 1 -
  3. Convert next operation:
    (8 / (3 1 -)) → 8 3 1 - /
  4. Convert final operation:
    (5 + (8 3 1 - /)) → 5 8 3 1 - / +
Converting Prefix to Infix

+ * 5 3 / 8 2

Method: Read from right to left, identify operators and their operands:

  1. Start from the rightmost operator: /
    Find its two operands: 8 and 2
    Convert: / 8 2 → (8 / 2)
  2. Move left to next operator: *
    Find its two operands: 5 and 3
    Convert: * 5 3 → (5 * 3)
  3. Final operator: +
    Find its two operands: (5 * 3) and (8 / 2)
    Convert: + (5 * 3) (8 / 2) → ((5 * 3) + (8 / 2))

Result: ((5 * 3) + (8 / 2)) = 15 + 4 = 19

Converting Postfix to Infix

5 3 * 8 2 / +

Method: Read from left to right, identify operators and their operands:

  1. Start from the leftmost operator: *
    Find its two operands (immediately before it): 5 and 3
    Convert: 5 3 * → (5 * 3)
  2. Move right to next operator: /
    Find its two operands: 8 and 2
    Convert: 8 2 / → (8 / 2)
  3. Final operator: +
    Find its two operands: (5 * 3) and (8 / 2)
    Convert: (5 * 3) (8 / 2) + → ((5 * 3) + (8 / 2))

Result: ((5 * 3) + (8 / 2)) = 15 + 4 = 19

Practice Problems

Problem 1 Junior

Convert the following infix expression to prefix notation (Write answer with no spaces; you may type ^ or ↑ for exponentiation):

(A * B - C / D) ↑ E

Problem 2 Junior

Evaluate the following postfix expression:

5 3 + 2 * 1 -

Problem 3 Intermediate

Evaluate the following prefix expression:

↑ + * 3 4 / 8 2 - 7 5