# What Is Infix Prefix and Postfix in Data Structure?

//

Larry Thompson

What Is Infix, Prefix, and Postfix in Data Structure?

In computer science, the terms infix, prefix, and postfix are used to describe different notations for writing arithmetic expressions. These notations specify the order of operations and the placement of operators and operands within an expression.

## Infix Notation

Infix notation is the most commonly used notation for writing arithmetic expressions. In this notation, operators are placed between their operands. For example:

• 2 + 3
• 4 * (5 – 2)
• (a + b) / c

This notation is intuitive as it closely resembles the way we write mathematical expressions on paper. However, it can sometimes lead to ambiguity when dealing with complex expressions or nested parentheses.

## Prefix Notation

Prefix notation, also known as Polish notation, reverses the order of operators and operands. In this notation, operators are placed before their operands. For example:

• + 2 3
• * 4 – 5 2
• / + a b c

This notation eliminates the need for parentheses as the order of operations is determined solely by the position of operators. However, it can be challenging to read and write expressions in prefix notation without proper formatting.

## Postfix Notation

Postfix notation, also known as Reverse Polish Notation (RPN), places operators after their operands. For example:

• 2 3 +
• 4 5 2 – *
• a b + c /

Similar to prefix notation, postfix notation eliminates the need for parentheses and ensures unambiguous evaluation of expressions. It is often used in stack-based calculators and compilers.

## Conversion between Notations

To convert an arithmetic expression from infix to prefix or postfix notation, we can use algorithms such as the Shunting Yard algorithm or recursive methods.

### Shunting Yard Algorithm

The Shunting Yard algorithm is a popular method for converting infix expressions to postfix (or prefix) notation. It uses a stack to store operators and parentheses while rearranging the expression. The algorithm proceeds as follows:

1. Initialize an empty stack for operators and an empty output queue.
2. Scan the infix expression from left to right.
3. If the scanned token is an operand, add it to the output queue.
4. If the scanned token is an operator:
• If the stack is empty or contains a left parenthesis on top, push the operator onto the stack.
• If the precedence of the scanned operator is higher than that of the operator on top of the stack, push it onto the stack.
• If the precedence of the scanned operator is lower than or equal to that of the operator on top of the stack, pop operators from the stack and add them to the output queue until a lower precedence operator is encountered. Then push the scanned operator onto the stack.

The above steps are repeated until all tokens have been scanned. Finally, pop any remaining operators from the stack and add them to the output queue.

## Conclusion

Infix, prefix, and postfix notations are alternative ways of representing arithmetic expressions. While infix notation is the most commonly used in human-readable form, prefix and postfix notations are often used for efficient computation by machines.

Understanding these notations can be helpful when working with data structures and expression evaluation algorithms. The conversion algorithms mentioned can be used to convert between different notations as required.

By incorporating the proper use of HTML styling elements such as bold text, underlined text,

unordered lists

, and

• list items
• , this article aims to provide you with an informative and visually engaging overview of infix, prefix, and postfix notations in data structures.