INFIX TO POSTFIX

advertisement
INFIX TO POSTFIX
Expressions consists of two components namely operands and operators. Operators indicate the
operation to be carried out on operands. There are two kinds of operators namely unary and binary.
Unary operators require one operand to carry out the intended operation whereas binary operators requires
two operands to carry out the intended operation. Most operators are binary.
There are three ways of representing expressions:



In prefix notation, the operator is written before the operand/s it operates on.
In infix notation, the operator is written in between the operands it operates on as in the case of
binary operators.
In postfix notation, the operator is written after the operand/s it operates on.
Examples:
PREFIX = A /  H + B1 B2 2
INFIX A = H  (B1 + B2) / 2
POSTFIX A H B1 B2 +  2 / =
PREFIX = M /  P i – 1 / ^ + 1 1 i t
INFIX M = (P  i) / (1 – 1 / (1 + i) ^ t)
POSTFIX M P i  1 1 1 i + t ^ / – / =
Regardless of the notation, an expression can be represented by a binary tree as follows:
PREFIX operator operand operand
INFIX operand operator operand
POSTFIX operand operand operator
Observation that the tree representation of an expression eliminates the need for parentheses.
Our world is accustomed to infix notation. However, algorithmically, postfix notation are easier to
evaluate than infix notation. Hence, what is of interest to us is the conversion from infix to postfix
notation.
Conversion from infix to postfix can be achieved by the use of three stacks namely:



an output stack
an operator stack
a comment stack
The following illustrates the evaluation of a postfix notation.
Problems:




Develop an algorithm that will store an expression in a binary tree.
Given the tree representation of an expression, develop the different algorithms that will generate
the equivalent prefix, infix, and postfix notation as desired.
Given an infix expression, determine the algorithm that will generate the equivalent postfix
notation.
Given a postfix expression, determine the algorithm that will evaluate the expression.
© 1994-07-03 cpsm ; last update 2003-01-15 22:28
Download