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
): OperandB = 3
, push to stack. Stack =[3]
- Next,
A
: OperandA = 4
, push to stack. Stack =[3, 4]
- Operator
+
: Pop4
and3
, compute4 + 3 = 7
, push result. Stack =[7]
Result: 7
Example 2: ++ABC
Prefix Expression: ++ABC
- Start with
C
: OperandC = 2
, push to stack. Stack =[2]
- Next,
B
: OperandB = 3
, push to stack. Stack =[2, 3]
- Operator
+
: Pop3
and2
, compute2 + 3 = 5
, push result. Stack =[5]
- Next,
A
: OperandA = 4
, push to stack. Stack =[5, 4]
- Operator
+
: Pop4
and5
, compute5 + 4 = 9
, push result. Stack =[9]
Result: 9
Example 3: ++AB-CD
Prefix Expression: ++AB-CD
- Start with
D
: OperandD = 1
, push to stack. Stack =[1]
- Next,
C
: OperandC = 2
, push to stack. Stack =[1, 2]
- Operator
-
: Pop2
and1
, compute1 - 2 = -1
, push result. Stack =[-1]
- Next,
B
: OperandB = 3
, push to stack. Stack =[-1, 3]
- Next,
A
: OperandA = 4
, push to stack. Stack =[-1, 3, 4]
- Operator
+
: Pop4
and3
, compute3 + 4 = 7
, push result. Stack =[-1, 7]
- Operator
+
: Pop7
and-1
, compute-1 + 7 = 6
, push result. Stack =[6]
Result: 6
Example 4: *-A +BCD
Prefix Expression: *-A +BCD
- Start with
D
: OperandD = 1
, push to stack. Stack =[1]
- Next,
C
: OperandC = 2
, push to stack. Stack =[1, 2]
- Next,
B
: OperandB = 3
, push to stack. Stack =[1, 2, 3]
- Operator
+
: Pop3
and2
, compute2 + 3 = 5
, push result. Stack =[1, 5]
- Next,
A
: OperandA = 4
, push to stack. Stack =[1, 5, 4]
- Operator
-
: Pop4
and5
, compute5 - 4 = 1
, push result. Stack =[1, 1]
- Operator
*
: Pop1
and1
, compute1 * 1 = 1
, push result. Stack =[1]
Result: 1
Example 5: -*+ABC/DE
Prefix Expression: -*+ABC/DE
- Start with
E
: OperandE = 10
, push to stack. Stack =[10]
- Next,
D
: OperandD = 1
, push to stack. Stack =[10, 1]
- Operator
/
: Pop1
and10
, compute10 / 1 = 10
, push result. Stack =[10]
- Next,
C
: OperandC = 2
, push to stack. Stack =[10, 2]
- Next,
B
: OperandB = 3
, push to stack. Stack =[10, 2, 3]
- Next,
A
: OperandA = 4
, push to stack. Stack =[10, 2, 3, 4]
- Operator
+
: Pop4
and3
, compute3 + 4 = 7
, push result. Stack =[10, 2, 7]
- Operator
-
: Pop7
and2
, compute2 - 7 = -5
, push result. Stack =[10, -5]
- Operator
*
: Pop-5
and10
, compute10 * -5 = -50
, push result. Stack =[-50]
Result: -50