Home » Data Structure » Infix to Prefix conversion Algorithm with example

Infix to Prefix conversion Algorithm with example

Last updated:

Support this post with a reaction:

4,458 views

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:

  • A is the first operand.
  • + is the operator.
  • B is the second operand.

Infix notation is the most commonly used expression format in mathematics and programming languages.

Examples of infix expressions:

  • A + B
  • A * B + C
  • (A + B) * C
  • A + 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.
  • A is the first operand.
  • B is 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:

OperationDescription
PushInserts an element into the stack
PopRemoves the top element from the stack
PeekReturns the top element without removing it
IsEmptyChecks whether the stack is empty
IsFullChecks 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.

OperatorDescriptionPrecedenceAssociativity
^Exponentiation3Right to Left
*, /, %Multiplication, Division, Modulus2Left to Right
+, -Addition, Subtraction1Left 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.

  1. Start.
  2. Reverse the given infix expression.
  3. Interchange every opening parenthesis ( with closing parenthesis ).
  4. Initialize an empty operator stack and an empty postfix output.
  5. Scan the modified expression from left to right.
  6. If the scanned character is an operand, append it to the output.
  7. If the scanned character is an opening parenthesis (, push it onto the stack.
  8. If the scanned character is a closing parenthesis ), pop operators into the output until an opening parenthesis is found. Remove the opening parenthesis.
  9. 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.
  10. Repeat until all characters have been processed.
  11. Pop all remaining operators from the stack into the output.
  12. Reverse the output to obtain the prefix expression.
  13. 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)

  1. Start.
  2. Reverse the given infix expression.
  3. Interchange every opening parenthesis ( with a closing parenthesis ).
  4. Initialize an empty operator stack and an empty output sequence.
  5. Scan the modified expression from left to right.
  6. If the scanned symbol is an operand, append it to the output.
  7. If the scanned symbol is an opening parenthesis (, push it onto the stack.
  8. If the scanned symbol is a closing parenthesis ), pop operators into the output until an opening parenthesis is found. Remove the opening parenthesis.
  9. 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.
  10. Repeat until all symbols are processed.
  11. Pop all remaining operators into the output.
  12. Reverse the output to obtain the prefix expression.
  13. 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 SymbolStack (Bottom → Top)OutputExplanation
FEmptyFF is an operand, so add it directly to output.
^^F^ is an operator, so push it onto the stack.
(^ (FOpening parenthesis is pushed.
E^ (FEE is an operand, so add it to output.
–^ ( –FEPush the subtraction operator.
D^ ( –FEDD 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-^CC is an operand, so add it to output.
+– +FED-^C+ and – have equal precedence. Do not pop; push +.
(– + (FED-^CPush opening parenthesis.
B– + (FED-^CBB is an operand, so add it to output.
+– + ( +FED-^CBPush + onto the stack.
A– + ( +FED-^CBAA is an operand, so add it to output.
)– +FED-^CBA+Pop + into output and remove (.
EndEmptyFED-^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.

  1. Compiler Design: Helps represent and process arithmetic expressions during compilation.
  2. Expression Evaluation: Allows expressions to be evaluated without relying on conventional infix precedence rules.
  3. Expression Trees: Helps represent operators and operands in tree structures.
  4. Programming Languages: Used in languages such as Lisp that employ prefix-style expressions.
  5. Mathematical Expression Processing: Represents complex expressions without requiring parentheses to establish evaluation order.