100% found this document useful (1 vote)
9K views

Postfix To Infix

This document discusses applications of stacks, including using stacks to evaluate expressions in Polish notation and converting expressions between infix and postfix notation. It provides algorithms for evaluating a postfix expression using a stack and converting an infix expression to postfix notation by scanning the expression and either adding elements to the output string or pushing/popping from the stack. Recursion is also mentioned as another application of stacks.

Uploaded by

api-26849388
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as PDF, TXT or read online on Scribd
100% found this document useful (1 vote)
9K views

Postfix To Infix

This document discusses applications of stacks, including using stacks to evaluate expressions in Polish notation and converting expressions between infix and postfix notation. It provides algorithms for evaluating a postfix expression using a stack and converting an infix expression to postfix notation by scanning the expression and either adding elements to the output string or pushing/popping from the stack. Recursion is also mentioned as another application of stacks.

Uploaded by

api-26849388
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 10

Lecture 12: Applications of Stacks

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-D E*F G/H


This is called infix notation. With this notation, we must distinguish between

(A+B)*C and A + (B * C)

by using either parentheses or some operator-precedence convention such as the usual


precedence levels discussed above. Accordingly, the order of the operators and operands in an
arithmetic expression does not uniquely determine the order in which the operations are to be
performed.
Polish notation, named after the Polish mathematician Jan Lukasiewicz, refers to the notation
in which the operator symbol is placed before its two operands. For example,

65
+AB -CD *EF /GH
We translate, step by step, the following infix expressions into Polish notation using brackets [ ]
to indicate a partial translation:

(A + B) *C= [+AB]*C = *+ABC


A + (B*C) = A + [*BC] = +A*BC
(A + B) / (C - D) = [+ AB] / [ - CD] = / + AB - CD

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:

AB+ CD EF* GH/


Again, one never needs parentheses to determine the order of the operations in any arithmetic
expression written in reverse Polish notation. This notation is frequently called postfix (or suffix)
notation, whereas prefix notation is the term used for Polish notation, discussed in the preceding
paragraph.

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.

Evaluation of a Postfix Expression


Suppose P us an arithmetic expression written in postfix notation. The following

66
algorithm, which uses a STACK to hold operands, evaluates P.

Algorithm: This algorithm finds the VALUE of an arithmetic expression P written in


postfix notation.
1. Add a right parenthesis “)” at the end of P. [This acts as a sentinel.]
2. Scan P from left to right and repeat Steps 3 and 4 for each element of P until
the sentinel “)” is encountered.
3. If an operand is encountered, put it on STACK.
4. If an operator x is encountered, then:
a. Remove the two top elements of STACK, where A is the top element
and B is the next-top element.
b. Evaluate B⊗A.
c. Place the result of (b) back on STACK.
[End of If structure.]
[End of Step 2 loop.]
5. Set VALUE equal to the top element to STACK.
6. Exit.

We note that, when Step 5 is executed, there should be only one number on STACK.

Example:

Consider the following arithmetic expression P written in postfix notation:


P : 5, 6, 2, +, *, 12, 4, /, -, )
(Commas are used to separate the elements of P so that, 5,6,2 is not interpreted as the
number 562.)

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

Let Q be an arithmetic expression written in infix notation. Besides operands and


operators, Q may also contain left and right parentheses. We assume that the operators in
Q consist only of exponentiations (↑), multiplications (*), divisions (/), additions (+) and
subtractions (-), and that they have the usual three levels of precedence as given above.
We also assume that operators on the same level, including exponentiations, are
performed from left to right unless otherwise indicated by parentheses. (This is not
standard, since expressions may contain unary operators and some languages perform the
exponentiations from right to left. However, these assumptions simplify our algorithm.)
The following algorithm transforms the infix expression Q into its equivalent postfix
expression P. The algorithm uses a stack to temporarily hold operators and left
parentheses. The postfix expression P will be constructed from left to right using the
operands from Q and the operators, which are removed from STACK. We begin by
pushing a left parenthesis onto STACK and adding a right parenthesis at the end of Q.
The algorithm is completed when STACK is empty.

69
Algorithm: POLISH(Q, P)
Suppose Q is an arithmetic expression written in infix notation. This algorithm
finds the equivalent postfix expression P.

1. Push "(" onto STACK, and add ")" to the end of Q.


2. Scan Q from left to right and repeat Steps 3 to 6 for each element of Q
until the STACK is empty:
3. If an operand is encountered, add it to P.
4. If a left parenthesis is encountered, push it onto STACK.
5. If an operator ⊗ is encountered, then:
(a) Repeatedly pop from STACK and add to P each operator (on
the top of STACK) which has the same precedence as or higher
precedence than ⊗.
(b) Add ⊗ to STACK.
[End of If structure.]
6. If a right parenthesis is encountered, then:
(a) Repeatedly pop from STACK and add to P each operator (on
the top of STACK) until a left parenthesis is encountered.
(b) Remove the left parenthesis. [Do not add the left parenthesis to
P.]
[End of If structure.]
[End of Step 2 loop.]
7. Exit.

The terminology sometimes used for Step 5 is that ⊗ will "sink" to its own level. .

70
EXAMPLE

Consider the following arithmetic infix expression Q:

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.

QUICKSORT is another application of stacks. (covered later on)

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.

A recursive function with these two properties is also said to be well-defined.

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

You might also like