Data Structure
Data Structure
Infix Notation
Prefix (Polish) Notation
Postfix (Reverse-Polish) Notation
These notations are named as how they use operator in expression.
Infix Notation
We write expression in infix notation, e.g. a - b + c, where operators are used in-
between operands. It is easy for us humans to read, write, and speak in infix
notation but the same does not go well with computing devices. An algorithm to
process infix notation could be difficult and costly in terms of time and space
consumption.
Prefix Notation
In this notation, operator is prefixed to operands, i.e. operator is written ahead of
operands. For example, +ab. This is equivalent to its infix notation a + b. Prefix
notation is also known as Polish Notation.
Postfix Notation
This notation style is known as Reversed Polish Notation. In this notation style,
the operator is postfixed to the operands i.e., the operator is written after the
operands. For example, ab+. This is equivalent to its infix notation a + b.
The following table briefly tries to show the difference in all three notations −
2 (a + b) ∗ c ∗+abc ab+c∗
3 a ∗ (b + c) ∗a+bc abc+∗
1
4 a/b+c/d +/ab/cd ab/cd/+
5 (a + b) ∗ (c + d) ∗+ab+cd ab+cd+∗
Parsing Expressions
As we have discussed, it is not a very efficient way to design an algorithm or
program to parse infix notations. Instead, these infix notations are first converted
into either postfix or prefix notations and then computed.
To parse any arithmetic expression, we need to take care of operator precedence
and associativity also.
Precedence
When an operand is in between two different operators, which operator will take the
operand first, is decided by the precedence of an operator over others. For example
−
2
The above table shows the default behavior of operators. At any point of time in
expression evaluation, the order can be altered by using parenthesis. For example
−
In a + b*c, the expression part b*c will be evaluated first, with multiplication as
precedence over addition. We here use parenthesis for a + b to be evaluated first,
like (a + b)*c.
Applications of stack:
Balancing of symbols
Infix to Postfix /Prefix conversion
Redo-undo features at many places like editors, photoshop.
Forward and backward feature in web browsers
Used in many algorithms like Tower of Hanoi, tree traversals, stock span
problem, histogram problem.
Backtracking is one of the algorithm designing technique .Some example
of back tracking are Knight-Tour problem,N-Queen problem,find your way
through maze and game like chess or checkers in all this problems we
dive into someway if that way is not efficient we come back to the
previous state and go into some another path. To get back from current
state we need to store the previous state for that purpose we need stack.
In Graph Algorithms like Topological Sorting and Strongly Connected
Components
In Memory management any modern computer uses stack as the
primary-management for a running purpose.Each program that is running
in a computer system has its own memory allocations
String reversal is also a another application of stack.Here one by one
each character get inserted into the stack.So the first character of string is
on the bottom of the stack and the last element of string is on the top of
stack. After Performing the pop operations on stack we get string in
reverse order .
Implementation:
There are two ways to implement a stack:
Using array
Using linked list
3
Convert Infix To Prefix Notation
While we use infix expressions in our day to day lives. Computers have
trouble understanding this format because they need to keep in mind rules of
operator precedence and also brackets. Prefix and Postfix expressions are
easier for a computer to understand and evaluate.
Given two operands and and an operator , the infix notation implies that
O will be placed in between a and b i.e . When the operator is placed
after both operands i.e , it is called postfix notation. And when the
operator is placed before the operands i.e , the expression in prefix
notation.
Given any infix expression we can obtain the equivalent prefix and postfix
format.
Examples:
Input : A * B + C / D
Output : + * A B/ C D
4
expression to postfix(or prefix) form before evaluation.
The corresponding expression in postfix form is: abc*+d+. The postfix
expressions can be evaluated easily using a stack. We will cover postfix
expression evaluation in a separate post.
Algorithm
1. Scan the infix expression from left to right.
2. If the scanned character is an operand, output it.
3. Else,
1 If the precedence of the scanned operator is greater than the precedence
of the operator in the stack(or the stack is empty or the stack contains a
‘(‘ ), push it.
2 Else, Pop all the operators from the stack which are greater than or equal
to in precedence than that of the scanned operator. After doing that Push the
scanned operator to the stack. (If you encounter parenthesis while popping
then stop there and push the scanned operator in the stack.)
4. If the scanned character is an ‘(‘, push it to the stack.
5. If the scanned character is an ‘)’, pop the stack and and output it until a ‘(‘ is
encountered, and discard both the parenthesis.
6. Repeat steps 2-6 until infix expression is scanned.
7. Print the output
8. Pop and output from the stack until it is not empty.
5
If the symbol is an operator, then pop two operands from the Stack
Create a string by concatenating the two operands and the operator
between them.
string = (operand1 + operator + operand2)
And push the resultant string back to Stack
Repeat the above steps until end of Prefix expression.
6
7