# What Is Infix Postfix and Prefix in Data Structure?

//

Angela Bailey

In data structures, infix, postfix, and prefix are three different ways to represent arithmetic expressions. These notations are used to define the order of operations when evaluating an expression.

## Infix Notation:

Infix notation is the most common way of writing arithmetic expressions. In this notation, operators are placed between the operands.

For example:

2 + 3

Here, the plus operator (+) is placed between the operands 2 and 3. Infix notation is easy to understand for humans but can be difficult to evaluate for machines.

## Postfix Notation:

Postfix notation, also known as Reverse Polish Notation (RPN), is a way of writing arithmetic expressions where operators are placed after their operands. For example:

2 3 +

In postfix notation, the plus operator (+) comes after the operands 2 and 3.

To evaluate a postfix expression, we start from left to right and perform the operation whenever we encounter an operator. In this case, we add 2 and 3 together to get a result of 5.

## Prefix Notation:

Prefix notation is also known as Polish Notation. It’s similar to postfix notation but with one key difference – operators come before their operands.

For example:

+ 2 3

In prefix notation, the plus operator (+) precedes the operands 2 and 3. To evaluate a prefix expression, we start from right to left and perform the operation whenever we encounter an operator.

## Conversion between Infix, Postfix, and Prefix:

Converting expressions between these notations can be useful for different purposes. To convert an infix expression to postfix or prefix notation, we need to follow specific rules and algorithms.

### Infix to Postfix Conversion:

To convert an infix expression to postfix notation, we can use the Shunting Yard Algorithm. This algorithm uses a stack to store operators and ensures the correct order of operations. The algorithm scans the infix expression from left to right and performs the following steps:

• If the scanned character is an operand, output it.
• If the scanned character is an operator:
• While there are operators on top of the stack with higher precedence than the scanned operator, pop them from the stack and output them.
• Push the scanned operator onto the stack.
• If the scanned character is an opening parenthesis ‘(‘, push it onto the stack.
• If the scanned character is a closing parenthesis ‘)’:
• While there are operators on top of the stack and they are not opening parentheses, pop them from the stack and output them.
• Pop and discard the opening parenthesis from the stack.

### Infix to Prefix Conversion:

The process of converting an infix expression to prefix notation is similar to converting it to postfix notation. However, we need to reverse the expression before applying any conversion algorithm. Once reversed, we can use similar rules as postfix conversion but scan from right to left instead of left to right.

## Conclusion:

Infix, postfix, and prefix notations are different ways of representing arithmetic expressions. Infix notation is commonly used by humans, while postfix and prefix notations are more suitable for machine evaluation.

Converting between these notations can be accomplished using specific algorithms like the Shunting Yard Algorithm. Understanding these notations is essential for working with data structures and evaluating arithmetic expressions efficiently.