Postfix notation
. Linearized representation of a syntax tree
. List of nodes of the tree
. Nodes appear immediately after its children
. The postfix notation for an expression E is defined as follows:
- If E is a variable or constant then the postfix notation is E itself
- If E is an expression of the form E 1 op E 2 where op is a binary operator then the postfix notation for E is
. E 1 ' E 2 ' op where E 1 ' and E 2 ' are the postfix notations for E 1 and E 2 respectively
- If E is an expression of the form (E 1 ) then the postfix notation for E 1 is also the postfix notation for E
At some point in your career you will be asked to write a program that can interpret an expression in a notation similar to that of algebra. Getting something like (A+B)/(C- D) right can seem like a daunting task, especially when you are asked to write code that will interpret any valid expression. It turns out to be surprisingly easy if the program is decomposed into two steps: translation to postfix notation and evaluation of the postfix notation expression. This is not a new idea... it was described by Donald Knuth in 1962 and he was writing a history!
Postfix notation is a way of writing algebraic expressions without the use of parentheses or rules of operator precedence. The expression above would be written as AB+CD-/ in postfix notation. (Don't panic! We'll explain this in a moment.) Postfix notation had its beginnings in the work of Jan L ukasiewicz * (1878-1956), a Polish logician, mathematician, and philosopher. L ukasiewicz developed a parenthesis-free prefix notation that came to be called Polish notation and a postfix notation now called Reverse Polish Notation or RPN. From these ideas, Charles Hamblin developed a postfix notation for use in computers. L ukasiewicz's work dates from about 1920. Hamblin's work on postfix notation was in the mid-1950's. Calculators, notably those from Hewlett-Packard, used various postfix formats beginning in the 1960s.
Postfix notation is a linearized representation of a syntax tree; it is a list of nodes of the tree which appear immediately after its children. You must read the three points written in the slide above to see how postfix expressions are made corresponding to a set of expressions.
|