Evaluation of Prefix Expression Examples

Prefix notation, also known as Polish notation, is a way of writing math expressions where the operator comes before the operands (the numbers or variables). For example, instead of writing A + B, in prefix notation, it would be written as + A B. To evaluate prefix expressions, we follow these steps:

  • Start from the rightmost character and move left.
  • When an operand (A, B, C, etc.) is encountered, push it onto the stack.
  • When an operator (+, -, *, /) is encountered, pop the required number of operands from the stack, perform the operation, and push the result back onto the stack.
  • The final result will be the only value left in the stack.

Examples of Prefix Expression Evaluation

Let’s evaluate the given prefix expressions using the values where A = 4, B = 3, C = 2, D = 1, E = 10.

Example 1: +AB

Prefix Expression: +AB

  • Start with the rightmost character (B): Operand B = 3, push to stack. Stack = [3]
  • Next, A: Operand A = 4, push to stack. Stack = [3, 4]
  • Operator +: Pop 4 and 3, compute 4 + 3 = 7, push result. Stack = [7]

Result: 7

Example 2: ++ABC

Prefix Expression: ++ABC

  • Start with C: Operand C = 2, push to stack. Stack = [2]
  • Next, B: Operand B = 3, push to stack. Stack = [2, 3]
  • Operator +: Pop 3 and 2, compute 2 + 3 = 5, push result. Stack = [5]
  • Next, A: Operand A = 4, push to stack. Stack = [5, 4]
  • Operator +: Pop 4 and 5, compute 5 + 4 = 9, push result. Stack = [9]

Result: 9

Example 3: ++AB-CD

Prefix Expression: ++AB-CD

  • Start with D: Operand D = 1, push to stack. Stack = [1]
  • Next, C: Operand C = 2, push to stack. Stack = [1, 2]
  • Operator -: Pop 2 and 1, compute 1 - 2 = -1, push result. Stack = [-1]
  • Next, B: Operand B = 3, push to stack. Stack = [-1, 3]
  • Next, A: Operand A = 4, push to stack. Stack = [-1, 3, 4]
  • Operator +: Pop 4 and 3, compute 3 + 4 = 7, push result. Stack = [-1, 7]
  • Operator +: Pop 7 and -1, compute -1 + 7 = 6, push result. Stack = [6]

Result: 6

Example 4: *-A +BCD

Prefix Expression: *-A +BCD

  • Start with D: Operand D = 1, push to stack. Stack = [1]
  • Next, C: Operand C = 2, push to stack. Stack = [1, 2]
  • Next, B: Operand B = 3, push to stack. Stack = [1, 2, 3]
  • Operator +: Pop 3 and 2, compute 2 + 3 = 5, push result. Stack = [1, 5]
  • Next, A: Operand A = 4, push to stack. Stack = [1, 5, 4]
  • Operator -: Pop 4 and 5, compute 5 - 4 = 1, push result. Stack = [1, 1]
  • Operator *: Pop 1 and 1, compute 1 * 1 = 1, push result. Stack = [1]

Result: 1

Example 5: -*+ABC/DE

Prefix Expression: -*+ABC/DE

  • Start with E: Operand E = 10, push to stack. Stack = [10]
  • Next, D: Operand D = 1, push to stack. Stack = [10, 1]
  • Operator /: Pop 1 and 10, compute 10 / 1 = 10, push result. Stack = [10]
  • Next, C: Operand C = 2, push to stack. Stack = [10, 2]
  • Next, B: Operand B = 3, push to stack. Stack = [10, 2, 3]
  • Next, A: Operand A = 4, push to stack. Stack = [10, 2, 3, 4]
  • Operator +: Pop 4 and 3, compute 3 + 4 = 7, push result. Stack = [10, 2, 7]
  • Operator -: Pop 7 and 2, compute 2 - 7 = -5, push result. Stack = [10, -5]
  • Operator *: Pop -5 and 10, compute 10 * -5 = -50, push result. Stack = [-50]

Result: -50