Explanation of Infix, Prefix and Postfix
Explanation of Infix, Prefix and Postfix
Infix, Postfix, and Prefix are different ways to write expressions involving operators and operands.
They differ in the placement of operators relative to their operands.
1. Infix Expression
Example: A + B
Characteristics:
Evaluation:
Example: A B +
Characteristics:
o Eliminates the need for parentheses because the order of operations is implicit.
Evaluation:
o When an operator is encountered, the top two operands are popped, the
operation is applied, and the result is pushed back onto the stack.
Example:
Infix: (A + B) * C
Postfix: A B + C *
1
Definition: Operators are written before the operands.
Example: + A B
Characteristics:
o Like postfix, parentheses are not required because the order of operations is
explicit.
Evaluation:
o Prefix expressions are evaluated using a stack but in reverse order (from right to
left).
o When an operator is encountered, the top two operands are popped, the
operation is applied, and the result is pushed back onto the stack.
Example:
Infix: (A + B) * C
Prefix: * + A B C
Comparison Table
Postfix AB+CD-*E/
Prefix /*+AB-CDE
Summary
Infix: Most natural for humans, but needs parentheses and operator precedence for clarity.
2
Postfix: Ideal for computers; no need for parentheses; evaluated left-to-right.
Infix, Postfix, and Prefix expressions are ways to represent mathematical expressions. These terms
relate to the position of the operator with respect to the operands.
1. Infix Expression
Example: A + B
This is the most familiar notation and is commonly used in everyday math.
Example: A B +
How it works:
o Start evaluating when an operator is encountered, using the most recent operands.
Advantages:
1. Operand A is output.
2. Operand B is output.
3. Operator + is output.
Result: A B +
Example: + A B
How it works:
3
o Read the operator first, then apply it to the operands that follow.
Advantages:
o Like postfix, it does not require parentheses to define the order of operations.
Result: + A B
Key Differences
Infix: (A + B) * C
1. Postfix: A B + C *
2. Prefix: * + A B C
This shows how the operator positions change while preserving the order of operations.
1. Infix Expression
Example: (A + B) * C
Humans find infix expressions easy to read because they follow the natural flow of
mathematical operations.
Example: A B + C *
4
Conversion from Infix to Postfix
Infix: (A + B) * C
Steps:
3. Process the * C: A B + C *
Postfix Result: A B + C *
Example: * + A B C
Infix: (A + B) * C
Steps:
3. Process the * C: * + A B C
Prefix Result: * + A B C
2. Convert (C - D) → Postfix: C D -
3. Combine (A + B) * (C - D) → Postfix: A B + C D - *
Postfix Result: A B + C D - * E /
2. Convert (C - D) → Prefix: - C D
3. Combine (A + B) * (C - D) → Prefix: * + A B - C D
Prefix Result: / * + A B - C D E
5
Comparing the Results
Postfix AB+CD-*E/
Prefix /*+AB-CDE
o When an operator is encountered, the top two operands are popped, the operation
is performed, and the result is pushed back onto the stack.
o Example: A B +
o Example: + A B