What is the need of prefix and postfix notation?
Apparently computer does understand the operator precedence but if there is a big expression like:
So to make it easier for machine to interpret and evaluation expressions according to rules of mathematics which we understand implicitly, postfix and prefix notation is used.
Infix:
The expressions that we have been seeing since kindergarten are all infix expressions. Just to refresh your memory:
Prefix Notation:
In prefix notation operators is written before the operands in the expression. So
A+B will be written as +AB.
How to write Prefix expressions?
Take the expression:
Step 1: You understand the small calculations you have to make in order to evaluate this expression correctly. Lets show them in parenthesis:
A-(B/(C*(D$E)))
Step 2: Now starting with the innermost parenthesis start writing the postfix form. I'll show it in steps to make it easier to understand:
(i) A-(B/(C*(D$E)))
(ii) A-(B/(C*(DE$)))
(iii) A-(B/(C(DE$)*))
(iv) A-(B(C(DE$)*)/)
(v) A(B(C(DE$)*)/)-
Step 3: Remove all the parenthesis after you are done with converting the expression into prefix. We get:
ABCDE$*/-
PS1: Better and alternate ways of thinking are always there. Do suggest some in your comments.
PS2: For reference, the document is attached: Infix, Prefix, Postfix
Apparently computer does understand the operator precedence but if there is a big expression like:
y = a + b*c + k/{d + e/(a+g+h*d)} - g + (e+ f/(s+t*k/(blah*klah)))/u
with lots of parenthesis' there is no way it understands which part of the expression to evaluate first.So to make it easier for machine to interpret and evaluation expressions according to rules of mathematics which we understand implicitly, postfix and prefix notation is used.
Infix:
The expressions that we have been seeing since kindergarten are all infix expressions. Just to refresh your memory:
3+5*6 -9
is an infix expression. You can see all the operators in infix expression are in-between the operands.Prefix Notation:
In prefix notation operators is written before the operands in the expression. So
A+B will be written as +AB.
How to write Prefix expressions?
Take the expression:
A-B/(C*D$E)
Step 1: You understand the small calculations you have to make in order to evaluate this expression correctly. Lets show them in parenthesis:
A-(B/(C*(D$E)))
Step 2: Now starting with the innermost parenthesis start writing the prefix expression. I'll show it in steps to make it easier to understand:
(i) A-(B/(C*(D$E)))
(ii) A-(B/(C*($DE)))
(iii) A-(B/(*C($DE)))
(iv) A-(/B(*C($DE)))
(v) -A(/B(*C($DE)))
Step 3: Remove all the parenthesis after you are done with converting the expression into prefix. We get:
-A/B*C$DE
Postfix Notation
Operators are written after the operands. Simply put:
A+B will be written as AB+
How to write Postfix expressions?
We will take the same example as above.
A-B/(C*D$E)
Step 1: You understand the small calculations you have to make in order to evaluate this expression correctly. Lets show them in parenthesis:
A-(B/(C*(D$E)))
Step 2: Now starting with the innermost parenthesis start writing the postfix form. I'll show it in steps to make it easier to understand:
(i) A-(B/(C*(D$E)))
(ii) A-(B/(C*(DE$)))
(iii) A-(B/(C(DE$)*))
(iv) A-(B(C(DE$)*)/)
(v) A(B(C(DE$)*)/)-
Step 3: Remove all the parenthesis after you are done with converting the expression into prefix. We get:
ABCDE$*/-
PS1: Better and alternate ways of thinking are always there. Do suggest some in your comments.
PS2: For reference, the document is attached: Infix, Prefix, Postfix
No comments:
Post a Comment