Postfix To Infix
Postfix To Infix
Summary of Lecture:
• Recursion
• Polish Notations
APPLICATION OF STACKS:
POLISH NOTATION
For most common arithmetic operations, the operator symbol is placed between its two
operands.
For example,
(A+B)*C and A + (B * C)
65
+AB -CD *EF /GH
We translate, step by step, the following infix expressions into Polish notation using brackets [ ]
to indicate a partial translation:
The fundamental property of Polish notation is that the order in which the operations are to be
performed is completely determined by the positions of the operators and operands in the
expression. Accordingly, one never needs parentheses when writing, expressions in Polish
notation.
Reverse Polish notation refers to the analogous notation in which the operator symbol is placed
after its two operands:
The computer usually evaluates an arithmetic expression written in infix notation in two
steps. First, it converts the expression to postfix notation, and then it evaluates the postfix
expression. In each step, the stack is the main tool that is used to accomplish the given task.
66
algorithm, which uses a STACK to hold operands, evaluates P.
We note that, when Step 5 is executed, there should be only one number on STACK.
Example:
67
The equivalent infix expression Q follows :
Q: 5 * ( 6 + 2 ) - 12 / 4
Note that parentheses are necessary for the infix expression Q but not for the postfix
expression P.
We evaluate P by simulating Algorithm 6.3. First we add a sentinel right
parenthesis at end of P to obtain
P: 5, 6, 2, +, *, 12, 4, /, -, )
(1) (2) (3) (4) (5) (6) (7) (8) (9) (10) The
elements of P have been labeled from left to right for easy reference. Above figure
shows the contents of STACK as each element of P is scanned. The final number in
STACK, 37, which is assigned to VALUE when the sentinel “)” is scanned, is the value
of P.
68
Transforming Infix Expressions into Postfix Expressions
69
Algorithm: POLISH(Q, P)
Suppose Q is an arithmetic expression written in infix notation. This algorithm
finds the equivalent postfix expression P.
The terminology sometimes used for Step 5 is that ⊗ will "sink" to its own level. .
70
EXAMPLE
Q: A+(B*C- (D/E↑F)*G)*H
We simulate the previous algorithm to transform Q into its equivalent postfix expression
P.
First we push “(” onto STACK, and then we add “)” to the end of Q to obtain:
A + ( B * C - ( D / E ↑ F ) * G ) * H )
(1) (2) (3) (4) (5) (6) (7) (8) (9) (10) (11) (12) (13)(14) (15) (16)(17) (18)(19) (20)
The elements of Q have now been labeled from left to right for easy reference. Table
below shows the status of STACK and of the string P as each element of Q is scanned.
Observe that
(1) Each operand is simply added to P and does not change STACK.
(2) The subtraction operator (-) in row 7 sends * from STACK to P before it (-) is
pushed onto STACK.
(3) The right parenthesis in row 14 sends j and then I from STACK to P, and then
removes the left parenthesis from the top of STACK.
(4) The right parenthesis in row 20 sends * and then + from STACK to P, and then
removes-the left parenthesis from the top of STACK.
After Step 20 is executed, the STACK is empty and
P: ABC*DEF↑/ G * -H * +
71
which is the required postfix equivalent of Q.
RECURSION
72
Recursion is an important concept in computer science. Many algorithms can be best
described in terms of recursion.
Suppose P is a procedure containing either a Call statement to itself or a Call
statement to a second procedure that may eventually result in a Call statement back to the
original procedure P. Then P is called a recursive procedure. So that the program will not
continue to run indefinitely, a recursive procedure must have the following two
properties:
(1) There must be certain criteria, called base criteria, for which the procedure does
not call itself.
(2) Each time the procedure does call itself (directly or indirectly), it must be closer to
the base criteria.
A recursive procedure with these two properties is said to be well-defined.
Similarly, a function is said to be recursively defined if the function definition refers
to itself. Again, in order for the definition not to be circular, it must have the following
two properties:
(1) There must be certain arguments, called base values, for which the function does
not refer to itself.
(2) Each time the function does refer to itself, the argument of the function must be
closer to a base value.
73
Review Questions:
1. What is a stack? What different operations can be performed on
stacks?
2. How are stacks used? State the different applications.
3. Explain the concept of recursion.
4. What are Polish Notations?
74