# What Is Postfix and Infix in Data Structure?

//

Heather Bennett

In the field of data structures, there are various ways to represent and manipulate expressions. Two commonly used notations for expressing arithmetic expressions are postfix and infix notations. Let’s dive deeper into what these notations mean and how they differ from each other.

## Postfix Notation

Postfix notation, also known as Reverse Polish Notation (RPN), is a mathematical notation in which the operator is placed after the operands. In simpler terms, it means that the operator follows its operands.

To better understand postfix notation, let’s consider an example expression:

• Expression: 4 5 +

In this expression, the numbers 4 and 5 are operands, and the plus sign (+) is the operator. In postfix notation, we first write the operands (4 and 5), followed by the operator (+). So, in postfix notation, this expression would be written as:

• Postfix Notation: 4 5 +

Now let’s see how we evaluate this expression in postfix notation:

• We start reading from left to right.
• When we encounter an operand (in this case, a number), we push it onto a stack.
• When we encounter an operator (in this case, +), we pop the top two elements from the stack (which are 5 and then 4).
• We perform the operation on those two elements (addition of 5 and 4), which gives us a result of 9.
• We push this result back onto the stack.

The final result left on the stack is our answer. In this case, it is 9.

## Infix Notation

Infix notation is the most commonly used notation in arithmetic expressions. It is the standard mathematical notation we are all familiar with, where the operators are placed between the operands.

Let’s consider the same example expression we used for postfix notation:

• Expression: 4 + 5

In infix notation, we write the operands (4 and 5), followed by the operator (+). So, in infix notation, this expression would be written as:

• Infix Notation: 4 + 5

To evaluate an expression in infix notation, we use operator precedence rules and parentheses to determine the order of operations. The result of this expression would be obtained by performing the addition of numbers 4 and 5, which gives us a result of 9.

## Differences Between Postfix and Infix Notations

The main difference between postfix and infix notations lies in their order of writing operators and operands. In postfix notation, operators follow their operands, while in infix notation, operators are placed between operands.

Postfix notation eliminates the need for parentheses to indicate precedence since it uses a stack-based evaluation method. On the other hand, infix notation relies on parentheses to specify which operations should be performed first based on their precedence.

Although postfix notation may seem unusual at first glance, it has its advantages. It eliminates ambiguity that can arise from operator precedence in infix notation and makes it easier to evaluate expressions programmatically using stacks or other data structures.

### Conclusion

In summary, postfix and infix are two different notations used to represent arithmetic expressions. Postfix notation places operators after operands, while infix notation places operators between operands. Each notation has its own advantages and use cases, and understanding both can be beneficial when working with data structures and algorithms.