Infix to Prefix conversion is the process of converting an arithmetic expression from infix notation into prefix notation, where operators are placed before their operands. This conversion is commonly performed using a stack data structure and follows operator precedence, associativity and parentheses rules.
The infix expression A + B * C is converted into the prefix expression + A * B C. In this expression, multiplication is performed before addition because the multiplication operator has higher precedence than the addition operator.
What is Infix Notation?
Infix notation is a mathematical expression format in which an operator is placed between two operands.
For example:
A + B
Here:
Ais the first operand.+is the operator.Bis the second operand.
Infix notation is the most commonly used expression format in mathematics and programming languages.
Examples of infix expressions:
A + BA * B + C(A + B) * CA + B * C - D
What is Prefix Notation?
Prefix notation is a mathematical expression format in which the operator is placed before its operands. It is also called Polish notation.
For example:
+ A B
Here:
+is the operator.Ais the first operand.Bis the second operand.
Prefix expressions do not require parentheses to specify the order of operations when the expression is properly structured and operators have known arities.
Examples of prefix expressions:
+AB*+ABC+A*BC-*A+BCD
Convert Infix to Prefix Expression
Infix to Prefix conversion is a process of changing an arithmetic expression from infix notation into prefix notation, where operators are placed before their operands. This conversion is commonly performed using a stack data structure and follows operator precedence, associativity, and parentheses rules.
For example, the infix expression A + B * C is converted into the prefix expression + A * B C. Prefix notation is also known as Polish notation and helps represent expressions without requiring parentheses to specify the order of operations.
Which Data Structure is Used for Infix to Prefix Conversion?
A stack is used to convert infix expressions into prefix expressions.
A stack is a linear data structure that follows the LIFO (Last In, First Out) principle. It temporarily stores operators and parentheses during the conversion process.
The main stack operations are:
| Operation | Description |
|---|---|
| Push | Inserts an element into the stack |
| Pop | Removes the top element from the stack |
| Peek | Returns the top element without removing it |
| IsEmpty | Checks whether the stack is empty |
| IsFull | Checks whether an array-based stack is full |
In infix to prefix conversion, operands are directly added to the output, while operators are stored in the stack until their precedence and associativity rules require them to be removed.
Operator Precedence and Associativity Rules
Operator precedence determines which operator is evaluated first when an expression contains multiple operators.
Associativity determines the evaluation direction when operators have the same precedence.
| Operator | Description | Precedence | Associativity |
|---|---|---|---|
^ | Exponentiation | 3 | Right to Left |
*, /, % | Multiplication, Division, Modulus | 2 | Left to Right |
+, - | Addition, Subtraction | 1 | Left to Right |
Example 1: Higher precedence
A + B * C
Multiplication has higher precedence than addition.
Prefix expression:
+A*BC
Example 2: Left-to-right associativity
A - B - C
The expression is evaluated as:
(A - B) - C
Prefix expression:
--ABC
Example 3: Right-to-left associativity
A ^ B ^ C
The expression is evaluated as:
A ^ (B ^ C)
Prefix expression:
^A^BC
Infix to Prefix Conversion Algorithm
The following algorithm converts an infix expression into its equivalent prefix expression using a stack.
- Start.
- Reverse the given infix expression.
- Interchange every opening parenthesis
(with closing parenthesis). - Initialize an empty operator stack and an empty postfix output.
- Scan the modified expression from left to right.
- If the scanned character is an operand, append it to the output.
- If the scanned character is an opening parenthesis
(, push it onto the stack. - If the scanned character is a closing parenthesis
), pop operators into the output until an opening parenthesis is found. Remove the opening parenthesis. - If the scanned character is an operator:
- Compare its precedence with the operator at the top of the stack.
- Pop operators with higher precedence.
- For equal precedence, pop only when the incoming operator is
^under the usual precedence and associativity rules. - Push the current operator onto the stack.
- Repeat until all characters have been processed.
- Pop all remaining operators from the stack into the output.
- Reverse the output to obtain the prefix expression.
- Stop.
Note: This algorithm assumes valid expressions with single-character operands. For multi-character identifiers or numbers, tokenize the expression first and reverse the token sequence rather than individual characters.
Working of Infix to Prefix Conversion
The most common stack-based approach uses the reverse-and-convert method.
It involves three major steps:
Step 1: Reverse the infix expression
Reverse the given infix expression from right to left.
Step 2: Interchange parentheses
Replace every opening parenthesis ( with a closing parenthesis ) and every closing parenthesis ) with an opening parenthesis (.
Step 3: Convert into postfix and reverse the result
Convert the modified expression into postfix notation using a stack. Finally, reverse the postfix expression to obtain the prefix expression.
Example:
Original infix:
A + B * C
Reverse the expression:
C * B + A
Modified expression:
C*B+A
Convert into postfix:
CB*A+
Reverse the postfix expression:
+A*BC
Therefore, the final prefix expression is:
+A*BC
Algorithm for Infix to Prefix Conversion
The following algorithm converts an infix expression into its equivalent prefix expression using a stack.
Algorithm: InfixToPrefix(expression)
- Start.
- Reverse the given infix expression.
- Interchange every opening parenthesis
(with a closing parenthesis). - Initialize an empty operator stack and an empty output sequence.
- Scan the modified expression from left to right.
- If the scanned symbol is an operand, append it to the output.
- If the scanned symbol is an opening parenthesis
(, push it onto the stack. - If the scanned symbol is a closing parenthesis
), pop operators into the output until an opening parenthesis is found. Remove the opening parenthesis. - If the scanned symbol is an operator:
- Push it if the stack is empty or its top contains an opening parenthesis.
- Pop operators with higher precedence than the incoming operator.
- For equal precedence, pop only when the incoming operator is
^for the conventional operators and associativity defined above. - Otherwise, push the incoming operator.
- Repeat until all symbols are processed.
- Pop all remaining operators into the output.
- Reverse the output to obtain the prefix expression.
- Stop.
Infix to Prefix Conversion Using a Stack
Let’s understand the complete conversion process with a detailed example.
Given infix expression:
(A+B)+C-(D-E)^F
Step 1: Reverse the infix expression
Original expression:
(A+B)+C-(D-E)^F
Reverse the expression and interchange the parentheses.
Modified expression:
F^(E-D)-C+(B+A)
Step 2: Convert the modified expression into postfix notation
Scan the modified expression from left to right. Store operators in the stack and append operands directly to the output.
Step-by-step Stack Representation
| Scanned Symbol | Stack (Bottom → Top) | Output | Explanation |
|---|---|---|---|
| F | Empty | F | F is an operand, so add it directly to output. |
| ^ | ^ | F | ^ is an operator, so push it onto the stack. |
| ( | ^ ( | F | Opening parenthesis is pushed. |
| E | ^ ( | FE | E is an operand, so add it to output. |
| – | ^ ( – | FE | Push the subtraction operator. |
| D | ^ ( – | FED | D is an operand, so add it to output. |
| ) | ^ | FED- | Pop – into output until ( is found. Remove (. |
| – | – | FED-^ | ^ has higher precedence, so pop it. Push -. |
| C | – | FED-^C | C is an operand, so add it to output. |
| + | – + | FED-^C | + and – have equal precedence. Do not pop; push +. |
| ( | – + ( | FED-^C | Push opening parenthesis. |
| B | – + ( | FED-^CB | B is an operand, so add it to output. |
| + | – + ( + | FED-^CB | Push + onto the stack. |
| A | – + ( + | FED-^CBA | A is an operand, so add it to output. |
| ) | – + | FED-^CBA+ | Pop + into output and remove (. |
| End | Empty | FED-^CBA++- | Pop remaining operators in stack order. |
Step 3: Reverse the postfix expression
Intermediate postfix expression:
FED-^CBA++-
Reverse the expression:
-++ABC^-DEF
Applications of Prefix Notation
Prefix notation is useful in several areas of computer science and mathematical expression processing.
- Compiler Design: Helps represent and process arithmetic expressions during compilation.
- Expression Evaluation: Allows expressions to be evaluated without relying on conventional infix precedence rules.
- Expression Trees: Helps represent operators and operands in tree structures.
- Programming Languages: Used in languages such as Lisp that employ prefix-style expressions.
- Mathematical Expression Processing: Represents complex expressions without requiring parentheses to establish evaluation order.