Prefix, Postfix, & Infix Notation
Prefix/Postfix Wksheet #1, Worksheet #2, Worksheet #3
We normally learn mathematics via infix notation. That is, math teachers write binary operators (such as '+') surrounded by two operands like this: A + B. We compute the expression by knowing that somehow the values A and B must be added (isn't it obvious?) This is only natural because of our math development.
In some areas of computer science (such as compiler design), it makes much more sense to order expressions with prefix notation. In that case, the earlier example would be written as: +AB. The operator, +, precedes (comes before) the operands hence it is called prefix notation. Doesn't it really make sense to know first what operation that you will be computing so that you can sort of get ready for the right kind of problem and then to be told what the two values are. Prefix notation is sometimes called Polish notation because it was created by Jan Lukasiwicz.
Sometimes computer scientists use postfix notation. With postfix notation, you place the operator AFTER the operands so the example above would be written as: AB+. Postfix notation is sometimes called Reverse Polish notation. Note that prefix (or Polish notation) is sometimes called RPN which stands for Registered Parameter Numbers (see the Kent City High School Computer Club for details).
|(A + B) / D||/ + A B D||A B + D /|
|(A + B) / (D + E)||/ + A B + D E||A B + D E + /|
|(A - B / C + E)/(A + B)||/ + - A / B C E + A B||A B C / - E + A B + /|
|B ^ 2 - 4 * A * C||- ^ B 2 * * 4 A C||B 2 ^ 4 A * C * -|
Let A = 2, B = 3, D = 4, and E = 5. Answer the following:
1. What is the final value of the prefix expression: + * A B - C D
2. What is the final value of the postfix expression: B C D A D - + - +
3. Which of the following is a valid expression (either postfix or prefix)?
B C * D - +
* A B C -
B B B * *
ACSL Home Page | Mr. Minich's Wyo Home Page | Minich.com Web Design