Infix, Prefix, Postfix
Infix, Prefix, Postfix
Infix, Prefix, Postfix
process of evaluating expressions and generating machine language code. Humans generally write expressions like 3+4 and 9/3 in which the operator (+ or / here) is written between its operands ----- this is called infix notation.
Computers "prefer" postfix notation in which the
INFIX Expressions in which operands surround the operator, Example A-B, 6*3 This way of writing the Expressions is called infix notation.
To add A, B, we write A+B To multiply A, B, we write
A*B The operators ('+' and '*') go in between the operands ('A' and 'B') This is "Infix" notation.
AB+ and
would first convert the expression to postfix notation, and then evaluate the postfix version of the expression.
PREFIX Also known as polish notation Instead of saying "A plus B", we could say "add A,B " and write +AB "Multiply A,B" would be written *AB This is Prefix notation.
Question:
prefix/postfix notation is easier to parse for a machine. The big advantage in prefix/postfix notation is that there never arise any questions like operator precedence. For example, consider the infix expression 1 # 2 $ 3. Now, we don't know what those operators mean, so there are two possible corresponding postfix expressions: 1 2 # 3 $ and 1 2 3 $ #. Without knowing the rules governing the use of these operators, the infix expression is essentially worthless. Or, to put it in more general terms: it is possible to restore the original (parse) tree from a prefix/postfix expression without any additional knowledge, but the same isn't true for infix expressions
Find out: How a parse tree will be generated
binary code used in the compiled version of a program, the resulting code would be larger the needed and very inefficient. This is the reason, the compiler convert the infix expression into a postfix expression.
postfix is very easy to process left-to-right. An
operand is pushed onto a stack; an operator pops its operand(s) from the stack and pushes the result. Little or no parsing is necessary. It's used by some calculators (HP calculators are noted for using RPN).
the operand associates with the operator that has higher priority.
2. Tie Breaker
When an operand lies between two operators
that have the same priority, the operand associates with the operator on the left.
a+b-c a*b/c/d
3. Delimiters
Sub expression within delimiters is treated as
delimiters. This makes computer evaluation more difficult than is necessary. Postfix and prefix expression forms do not rely on operator priorities, a tie breaker, or delimiters. So it is easier to evaluate expressions that are in these forms.
Examples
Infix
A+B (A+B) * (C + D) A-B/(C*D^E)
Postfix
AB+ AB+CD+* ABCDE^*/-
Prefix
+AB *+AB+CD -A/B*C^DE
Infix to Postfix
Suppose that we would like to rewrite
A+B*C in postfix
A+B*C A+(B*C) Parentheses for emphasis A+(BC*) Convert the multiplication, Let D=BC* A+D Convert the addition A(D)+ ABC*+ Postfix Form
Infix (A+B)/D (A+B) / (D+E) (A-B/C +E) / (A+B) A*(B+C)/D (A+B-C)*D(E+F) 2-3*4+5 (2-3)*(4+5) 2-(3*4+5) 3+4*5/6 3*2+25/5
Postfix AB+D/ AB+DE+/ ABC/-E+AB+/ (Check it once) ABC+*D/ AB + C D * E F + - (Check it) 234*-5+ (replaced with numbers) 23-45+* 234*5+345*6/+ (Is it Correct?) 32*25 5 /+ (Is it Correct)
(a+b-c)*d(e+f)
Postfix expression
a+b-c)*d(e+f)
Postfix expression
+b-c)*d(e+f)
Postfix expression
b-c)*d(e+f)
Postfix expression
a
+
-c)*d(e+f)
Postfix expression
ab
+
c)*d(e+f)
Postfix expression
ab+
-
)*d(e+f)
Postfix expression
ab+c
-
*d(e+f)
Postfix expression
ab+c-
d(e+f)
Postfix expression
ab+c-
(e+f)
Postfix expression
ab+c-d
(e+f)
Postfix expression
ab+cd*
e+f)
Postfix expression
ab+cd*
(
+f)
Postfix expression
ab+cd*e
(
f)
Postfix expression
+
(
ab+cd*e
)
Postfix expression
+
(
ab+cd*ef
Postfix expression
ab+cd*ef+
Postfix expression
ab+cd*ef+-
2 * 3 / ( 2 1 ) + 5 * 3
2 2 23 23* 23* 23*2 23*2 23*21 23*2123*21-/ 23*21-/5 23*21-/5 23*21-/53 23*21-/53* 23*21-/53*+
Example
Infix String: A+B*C-D Postfix String: ABC*+D-
Step 1: A+B*C-D------ A+BC*-D Step 2: A + BC* - D ---- ABC*+ - D Step 3: ABC*+ - D ------- ABC*+Dis postfix conversion
Infix String:
A+B*C-D
Infix expression
A+B*C-D
Postfix expression
Initially the Stack is empty and our Postfix string has no characters. Now, the first character scanned is A'. A' is added to the Postfix string.
Infix expression
+B*C-D
Postfix expression
A
The next character scanned is '+'. It being an operator, it is pushed to the stack.
Infix expression
B*C-D
Postfix expression
A
Next character scanned is B' which will be placed in the Postfix string.
Infix expression
*C-D
Postfix expression
AB
Infix expression
C-D
Postfix expression
AB
* +
Infix expression
-D
Postfix expression
ABC
* +
Next character scanned is '-'. The topmost character in the stack is '*' which has a higher precedence than '-'.
Infix expression
D
Postfix expression
* +
Now, What we are going to do..
ABC
Thus '*' will be popped out from the stack and added to the Postfix string. Even now the stack is not empty.
Infix expression
D
Postfix expression
+
Still some thing is there..
ABC*
Now the topmost element of the stack is '+' which has equal priority to '-'. So pop the '+' from the stack and add it to the Postfix string. The '-' will be pushed to the stack.
Infix expression
D
Postfix expression
ABC*+
Infix expression
Postfix expression
ABC*+D
Now all characters have been scanned so we must pop the remaining elements from the stack and add it to the Postfix string.
Infix expression
Postfix expression
ABC*+DInfix to Postfix expression completed
Exercises
Work out on
3+4*5/6
(Check the result 345*6/+ correct or wrong)
Work out on
Infix Expression:
5 4
Infix Expression:
The next character is +(operator) So, pop the last top two from stack, perform + , Store the result on top of stack
4+5 = 9
9
Infix Expression:
7 9
Infix Expression:
2 7 9
Infix Expression:
2 7 9
Now, Stack has.
5 9
Infix Expression:
5 9
45
Infix Expression:
45
Exercises
1. What is the value of 623+-382/+* (Check it is 7 or not)
2. Convert into postfix, Go for postfix evaluation 3+4*5/6 Is post fix is 345*6/+ Check the answer