Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
70 views

Data Structure

- There are three common notations for writing arithmetic expressions: infix, prefix, and postfix. - Infix notation places the operator between operands, like "a + b". Prefix places the operator before operands, like "+ab". Postfix places the operator after operands, like "ab+". - Converting expressions between these notations allows for easier parsing and evaluation by computing devices. Infix is easiest for humans but more complex for machines to evaluate.

Uploaded by

Jaff Bezos
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
70 views

Data Structure

- There are three common notations for writing arithmetic expressions: infix, prefix, and postfix. - Infix notation places the operator between operands, like "a + b". Prefix places the operator before operands, like "+ab". Postfix places the operator after operands, like "ab+". - Converting expressions between these notations allows for easier parsing and evaluation by computing devices. Infix is easiest for humans but more complex for machines to evaluate.

Uploaded by

Jaff Bezos
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 7

Data Structure - Expression Parsing

The way to write arithmetic expression is known as a notation. An arithmetic


expression can be written in three different but equivalent notations, i.e., without
changing the essence or output of an expression. These notations are −

 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 −

Sr.No. Infix Notation Prefix Notation Postfix Notation

1 a+b +ab ab+

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+∗

6 ((a + b) ∗ c) - d -∗+abcd ab+c∗d-

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

As multiplication operation has precedence over addition, b * c will be evaluated


first. A table of operator precedence is provided later.
Associativity
Associativity describes the rule where operators with the same precedence appear
in an expression. For example, in expression a + b − c, both + and – have the same
precedence, then which part of the expression will be evaluated first, is determined
by associativity of those operators. Here, both + and − are left associative, so the
expression will be evaluated as (a + b) − c.
Precedence and associativity determines the order of evaluation of an expression.
Following is an operator precedence and associativity table (highest to lowest) −

Sr.No. Operator Precedence Associativity

1 Exponentiation ^ Highest Right Associative

2 Multiplication ( ∗ ) & Division ( / ) Second Highest Left Associative

3 Addition ( + ) & Subtraction ( − ) Lowest Left Associative

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.

Postfix Evaluation Algorithm


We shall now look at the algorithm on how to evaluate postfix notation −
Step 1 − scan the expression from left to right
Step 2 − if it is an operand push it to stack
Step 3 − if it is an operator pull operand from stack and perform
operation
Step 4 − store the output of step 3, back to stack
Step 5 − scan the expression until all operands are consumed
Step 6 − pop the stack and perform operation
To see the implementation in C programming language, please click here

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

Input : (A - B/C) * (A/K-L)


Output : *-A/BC-/AKL
To convert an infix to postfix expression refer to this article Stack | Set 2 (Infix
to Postfix). We use the same to convert Infix to Prefix.
 Step 1: Reverse the infix expression i.e A+B*C will become C*B+A. Note
while reversing each ‘(‘ will become ‘)’ and each ‘)’ becomes ‘(‘.
 Step 2: Obtain the postfix expression of the modified expression i.e CB*A+.
 Step 3: Reverse the postfix expression. Hence in our example prefix is
+A*BC.

Stack | Set 2 (Infix to Postfix)


Prerequisite – Stack | Set 1 (Introduction)
Infix expression:The expression of the form a op b. When an operator is in-
between every pair of operands.
Postfix expression:The expression of the form a b op. When an operator is
followed for every pair of operands.
Why postfix representation of the expression?
The compiler scans the expression either from left to right or from right to left.
Consider the below expression: a op1 b op2 c op3 d
If op1 = +, op2 = *, op3 = +
The compiler first scans the expression to evaluate the expression b * c, then
again scan the expression to add a to it. The result is then added to d after
another scan.
The repeated scanning makes it very in-efficient. It is better to convert the

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.

Prefix to Infix Conversion


Infix : An expression is called the Infix expression if the operator appears in
between the operands in the expression. Simply of the form (operand1
operator operand2).
Example : (A+B) * (C-D)
Prefix : An expression is called the prefix expression if the operator appears in
the expression before the operands. Simply of the form (operator operand1
operand2).
Example : *+AB-CD (Infix : (A+B) * (C-D) )
Given a Prefix expression, convert it into a Infix expression.
Computers usually does the computation in either prefix or postfix (usually
postfix). But for humans, its easier to understand an Infix expression rather
than a prefix. Hence conversion is need for human understanding.
Examples:
Input : Prefix : *+AB-CD
Output : Infix : ((A+B)*(C-D))

Input : Prefix : *-A/BC-/AKL


Output : Infix : ((A-(B/C))*((A/K)-L))
Algorithm for Prefix to Infix:
 Read the Prefix expression in reverse order (from right to left)
 If the symbol is an operand, then push it onto the Stack

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.

Prefix to Postfix Conversion


Prefix: An expression is called the prefix expression if the operator appears in
the expression before the operands. Simply of the form (operator operand1
operand2).
Example : *+AB-CD (Infix : (A+B) * (C-D) )
Postfix: An expression is called the postfix expression if the operator appears
in the expression after the operands. Simply of the form (operand1 operand2
operator).
Example : AB+CD-* (Infix : (A+B * (C-D) )
Given a Prefix expression, convert it into a Postfix expression.
Conversion of Prefix expression directly to Postfix without going through the
process of converting them first to Infix and then to Postfix is much better in
terms of computation and better understanding the expression (Computers
evaluate using Postfix expression).
Examples:
Input : Prefix : *+AB-CD
Output : Postfix : AB+CD-*
Explanation : Prefix to Infix : (A+B) * (C-D)
Infix to Postfix : AB+CD-*

Input : Prefix : *-A/BC-/AKL


Output : Postfix : ABC/-AK/L-*
Explanation : Prefix to Infix : (A-(B/C))*((A/K)-L)
Infix to Postfix : ABC/-AK/L-*
Algorithm for Prefix to Postfix:
 Read the Prefix expression in reverse order (from right to left)
 If the symbol is an operand, then push it onto the Stack
 If the symbol is an operator, then pop two operands from the Stack
Create a string by concatenating the two operands and the operator after
them.
string = operand1 + operand2 + operator
And push the resultant string back to Stack
 Repeat the above steps until end of Prefix expression.

6
7

You might also like