0% found this document useful (0 votes)
15 views6 pages

Explanation of Infix, Prefix and Postfix

Infix, Postfix, and Prefix are three notations for writing mathematical expressions, differing in operator placement relative to operands. Infix expressions place operators between operands and require parentheses for clarity, while Postfix (Reverse Polish Notation) places operators after operands and eliminates the need for parentheses, making it easier for computers to evaluate. Prefix (Polish Notation) places operators before operands and is evaluated from right to left, also without the need for parentheses.

Uploaded by

hellodavetech
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
15 views6 pages

Explanation of Infix, Prefix and Postfix

Infix, Postfix, and Prefix are three notations for writing mathematical expressions, differing in operator placement relative to operands. Infix expressions place operators between operands and require parentheses for clarity, while Postfix (Reverse Polish Notation) places operators after operands and eliminates the need for parentheses, making it easier for computers to evaluate. Prefix (Polish Notation) places operators before operands and is evaluated from right to left, also without the need for parentheses.

Uploaded by

hellodavetech
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 6

Explanation of Infix, Postfix and Prefix Expressions .

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

 Definition: Operators are written between the operands.

 Example: A + B

 Characteristics:

o This is the most commonly used and familiar form of expression.

o Requires parentheses to indicate the precedence of operators (e.g., (A + B) * C).

o Order of operations (precedence) and associativity must be explicitly defined or


derived.

 Evaluation:

o To evaluate, parentheses and operator precedence rules are followed.

2. Postfix Expression (Reverse Polish Notation)

 Definition: Operators are written after the operands.

 Example: A B +

 Characteristics:

o Eliminates the need for parentheses because the order of operations is implicit.

o The operands are directly evaluated in the order they appear.

 Evaluation:

o Postfix is typically evaluated using a stack.

o Operands are pushed onto the stack.

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 *

3. Prefix Expression (Polish Notation)

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.

o Evaluated from right to left.

 Evaluation:

o Prefix expressions are evaluated using a stack but in reverse order (from right to
left).

o When an operand is encountered, it is pushed onto the stack.

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

Aspect Infix Postfix Prefix

Operator Position Between operands After operands Before operands

Parentheses Needed Yes No No

Ease for Humans High Moderate Moderate

Ease for Computers Moderate High High

Evaluation Order Depends on precedence Left to right Right to left

Example Expression: ((A + B) * (C - D)) / E

Expression Type Representation

Infix ((A + B) * (C - D)) / E

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.

 Prefix: Similar to postfix, but evaluated right-to-left.

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

 In this notation, the operator is placed between the operands.

 Example: A + B

 This is the most familiar notation and is commonly used in everyday math.

2. Postfix Expression (Reverse Polish Notation)

 In this notation, the operator comes after the operands.

 Example: A B +

 How it works:

o Read operands and operators in the order they appear.

o Start evaluating when an operator is encountered, using the most recent operands.

 Advantages:

o Parentheses are not needed to define the order of operations.

o Easier for computers to evaluate expressions using a stack.

Example Conversion (Infix to Postfix):

For the infix expression A + B:

1. Operand A is output.

2. Operand B is output.

3. Operator + is output.

Result: A B +

3. Prefix Expression (Polish Notation)

 In this notation, the operator comes before the operands.

 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.

o Can also be easily evaluated using a stack.

Example Conversion (Infix to Prefix):

For the infix expression A + B:

1. Operator + is placed first.

2. Operands A and B are placed after the operator.

Result: + A B

Key Differences

Aspect Infix Postfix Prefix

Operator Position Between After Before

Readability Familiar Less Intuitive Less Intuitive

Parentheses Needed Yes No No

Evaluation Complex Easy using a stack Easy using a stack

Example for Understanding

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

 Operators are placed between operands.

 Example: (A + B) * C

 Humans find infix expressions easy to read because they follow the natural flow of
mathematical operations.

2. Postfix Expression (Reverse Polish Notation)

 Operators are placed after operands.

 Example: A B + C *

4
Conversion from Infix to Postfix

Infix: (A + B) * C
Steps:

1. Start with the innermost parentheses: A + B → Postfix: A B +

2. Replace (A + B) in the infix expression with its postfix: A B + * C

3. Process the * C: A B + C *

Postfix Result: A B + C *

3. Prefix Expression (Polish Notation)

 Operators are placed before operands.

 Example: * + A B C

Conversion from Infix to Prefix

Infix: (A + B) * C
Steps:

1. Start with the innermost parentheses: A + B → Prefix: + A B

2. Replace (A + B) in the infix expression with its prefix: * + A B C

3. Process the * C: * + A B C

Prefix Result: * + A B C

More Complex Example: ((A + B) * (C - D)) / E

Step-by-Step Conversion to Postfix

1. Start with (A + B) → Postfix: A B +

2. Convert (C - D) → Postfix: C D -

3. Combine (A + B) * (C - D) → Postfix: A B + C D - *

4. Divide the result by E → Postfix: A B + C D - * E /

Postfix Result: A B + C D - * E /

Step-by-Step Conversion to Prefix

1. Start with (A + B) → Prefix: + A B

2. Convert (C - D) → Prefix: - C D

3. Combine (A + B) * (C - D) → Prefix: * + A B - C D

4. Divide the result by E → Prefix: / * + A B - C D E

Prefix Result: / * + A B - C D E

5
Comparing the Results

Expression Type Example

Infix ((A + B) * (C - D)) / E

Postfix AB+CD-*E/

Prefix /*+AB-CDE

How They Work in Practice

1. Postfix Evaluation (Left to Right):

o Operands are pushed onto a stack.

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 +

 Push A → Push B → Pop A and B, compute A + B, push result.

2. Prefix Evaluation (Right to Left):

o Operators are encountered first.

o Operands are evaluated as the operator's arguments are encountered.

o Example: + A B

 Read + → Read A → Read B → Compute A + B.

You might also like