Unit-1 Applications of Stack_2_PHT
Unit-1 Applications of Stack_2_PHT
Stack
Linear Data Structure
Prepared By:
Ms. Purvi Tandel, Chandni Naik
Topics of Unit 2
Array: Representation of arrays
One dimensional array
Two dimensional array
Applications of array
Stack: Concepts
Operations on stacks
Applications of stacks
• Polish expression
• Reverse polish expression and their compilation
Recursion
Tower of Hanoi
Applications of Stack
• Keeping track of function calls
• Recursion
• Evaluation of expressions
• Reversing characters
------------------------------------------------------------------------------------
• Expression Conversion (Infix to Postfix, Infix to Prefix)
• Microsoft Word (Undo / Redo)
Keeping track of function calls
• Consider an example, where we are executing function A. In the
course of its execution, function A calls another function B. Function
B in turn calls another function C, which calls function D.
• The whole procedure will be repeated until all the function get
executed.
• Here stack ensure a proper execution order of functions. Therefore
When the execution
of B is complete, the stacks are frequently used in situations where the order of
Function A system control will
remove A for
processing is very important, especially when the processing needs
execution. to be postponed until other conditions are fulfilled.
Recursive Call
Procedure:
factorial(int n)
{
if(n==0)
return 1;
else
return n * factorial(n-1);
}
Recursive Call:
=return(5 * factorial(4))
=return(5 * return(4*factorial(3)))
=return(5 * return(4*return(3*factorial(2))))
=return(5 * return(4*return(3*return(2*factorial(1)))))
=return(5 * return(4*return(3*return(2*return(1*factorial(0))))))
=return(5 * return(4*return(3*return(2*return(1*1)))))
=return(5 * return(4*return(3*return(2*1))))
= return(5 * return(4*return(3*2)))
= return(5 * return(4*6))
= return(5 * 24)
=120
Continue..
factorial(1)
factorial(2) factorial(2)
factorial(3) factorial(3) factorial(3)
factorial(4) factorial(4) factorial(4) factorial(4)
factorial(5) factorial(5) factorial(5) factorial(5)
factorial(5)
factorial(4) is factorial(5) is
currently executing. currently executing.
Polish/Reverse Polish Expression & their
Compilation
• Evaluating Infix Expression
Operand
a+b*c+d*e
1 2
Operator 3
4
A repeated scanning from left to right is needed as operators
appears inside the operands.
Repeated scanning is avoided if the infix expression is first
converted to an equivalent parenthesis free prefix or suffix
(postfix) expression.
Prefix Expression(Polish notation): Operator, Operand, Operand
Postfix Expression(Reverse Polish notation): Operand, Operand,
Operator
Polish/Reverse Polish Notation
• This type of notation is known Lukasiewicz Notation or Polish
Notation or Reverse Polish Notation due to Polish logician Jan
Lukasiewicz.
• In both prefix and postfix equivalents of an infix expression, the
variables are in same relative position.
• The expressions in postfix or prefix form are parenthesis free and
operators are rearranged according to rules of precedence for
operators.
Continue..
• Why do we need prefix, postfix notation??
• Infix notation is easy to read for humans, whereas pre-/postfix
notation is easier to parse for a machine.
• The big advantage in pre-/postfix notation is that there never arise any
questions like operator precedence.
• Postfix notation, is very easy to process left-to-right. An operand is
pushed onto a stack; an operator pops its operand(s) from the stack
and pushes the result. Here, Little or no parsing is necessary.
Continue..
Prefix Expression(Polish notation): Operator, Operand, Operand
Postfix Expression(Reverse Polish notation): Operand, Operand, Operator
Rank (E) = R(A) + R(+) + R(B) + R(*) + R(C) + R (/) + R(D) + R(-) + R(E) + R (+) + R(F) + R(/) +
R(G) + R(/) + R(H) + R(+) + R(I)
Rank (E) = 1 + (-1) + 1 + (-1) + 1 + (-1) + 1 + (-1) + 1 + (-1) + 1 + (-1) + 1 + (-1) + 1 + (-1) + 1
Rank (E) = 1
Any Expression is valid if Rank of that expression is 1
Finding Rank of any Expression: Practice
example
E =ab + cd -*
Note: R = Rank, Rank of Variable = 1, Rank of binary operators = -1
Example:
1) E= A * + / BC / % D * EFGH Answer: Invalid
2) E = abcd ^^ + efd /+* Answer: Valid
Continue..
Infix Suffix Polish Rank Valid or Invalid
a + *b ab* + 0 Invalid
ab + c abc+ 2 Invalid
(a + b) * (c – d) ab+cd–* 1 Valid
1) P * Q % R - S / T / U - V ^ W
2) P % Q % R – S * T / U * V ^ W
Answers: 1) PQ*R%ST/U/-VW^-
2) PQ%R%ST*U/VW^*-
Convert Infix to Postfix
Expression(Parenthesised):
•Algorithm : REVPOL
Given an input string INFIX containing an infix expression which has
been padded on the right with ‘)’.
• This algorithm converts INFIX into reverse polish and places the result
in the string POLISH.
• All symbols have precedence value given by table.
• Stack is represented by a vector S, TOP denotes the top of the stack,
Algorithm PUSH and POP are used for stack manipulation.
• Function NEXTCHAR returns the next symbol in given input string.
• The integer variable RANK contains the rank of expression.
• The string variable TEMP is used for temporary storage purpose.
Continue..
) (( AB-DEF+G*/+ 1 POLISH ‘’
8. [Is the expression valid]
RANK 0
IF TOP != 0 OR RANK != 1
( AB-DEF+G*/+ 1 3. [Get first input Then write (‘INVALID‘)
Empty AB-DEF+G*/+ 1 symbol] Else write (‘VALID’)
Practice Examples:
1) ( A – B * ( C + D ) / E * F ) + G
2) ( A / B / ( C + D ) * E ^ F ^ G – H * ( I / J ) )
Answers: 1) ABCD+*E/F*-G+
2) AB/CD+/EFG^^*HIJ/*-
General Infix-to-Postfix Conversion
Create an empty stack called stack for keeping operators. Create an empty list for
output.
Read the character list from left to right and perform following steps
If the character is an operand (Variable), append it to the end of the output list
If the character is a left parenthesis ‘(’, push it on the stack
If the character is a right parenthesis ‘)’, pop the stack until the corresponding left parenthesis ‘)’ is
removed. Append each operator to the end of the output list.
If the token is an operator, *, /, +, or -, push it on the stack. However, first remove any operators
already on the stack that have higher or equal precedence and append them to the output list.
(Condition : if (input operator >= Stack TOP operator) then push
else while (input operator < Stack TOP operator) then pop)
(a+b^c^d)*(e+f/d)
Infix to Prefix Conversion
• To convert from infix to prefix expression, arrange expression in reverse
order.
• Apply generalized algorithm we used to convert infix to postfix.
• Once you get output string, reverse that output string.
• Reverse output string will be the answer of Infix to Prefix expression
conversion.
Ex: a + b / c
Step 1 – c / b + a (Reverse String)
Step 2 – Postfix result – c b / a +
Step 3 – + a / b c (Reserve String given in Step 2 – final answer)
Example:
1) (A + B) * C / D + E ↑ F / G
2) A – B / ( C * D ↑ E)
Evaluation of postfix expression
• Each operator in postfix string refers to the previous two operands in the
string.
• Each time we read an operand, we PUSH it onto Stack.
• When we reach an operator, its operands will be top two elements on the
stack.
• We can then POP these two elements, perform the indicated operation on
them and PUSH the result on the stack so that it will available for use as an
operand of the next operator.
Evaluation of postfix
expression
Evaluate Expression: 5 6 2 - +
Empty Stack
Read - , it is operator? POP
Read 5, it is operand? PUSH
two symbols and perform
Read 6, it is operand? PUSH operation and PUSH result
Read 2, it is operand? PUSH
2
6 4
5 Operand 1 - Operand 2 5
• These rings are of different sizes and stacked upon in an ascending order,
i.e. the smaller one sits over the larger one. There are other variations of
the puzzle where the number of disks increase, but the tower count
remains the same.
Tower of Hanoi Problem
• Rules:
• The mission is to move all the disks to some another tower without
violating the sequence of arrangement. A few rules to be followed for
Tower of Hanoi are −
• Only one disk can be moved among the towers at any given time.
• Only the "top" disk can be removed.
• No large disk can sit over a small disk.
• Tower of Hanoi puzzle with “n” disks can be solved in minimum 2n−1 steps.
• In above, shows that a puzzle with 3 disks has taken 23 - 1 = 7 steps.
1-Disk Problem
A B C
(Source) (Middle) (Destination)
A B C
(Source) (Middle) (Destination)
Problem
ALGORITHM:
START Hanoi(2, 1,3,2)
Procedure Hanoi(n, A, B, C)
1-3 Hanoi(2, 2,1,3)
IF disk == 1, THEN
move disk from A to C
ELSE
Hanoi(1, 1,2,3) 1-2
Hanoi(n - 1, A, C, B) // Step 1 Hanoi(1, 3,1,2) Hanoi(1, 2,3,1) 2-3 Hanoi(1, 1,2,3)
move disk from A to C// Step 2
Hanoi(n - 1, B, A, C) // Step 3
END IF 1-3 3-2 2-1 1-3
END Procedure
STOP
A B C A B C A B C
(Source) (Middle) (Destination) (Source) (Middle) (Destination) (Source) (Middle) (Destination)
c) 3-2
d) 1-3 e) 2-1
A B C A B C A B C
(Source) (Middle) (Destination) (Source) (Middle) (Destination) (Source) (Middle) (Destination)
f) 2-3 f) 1-3
A B C A B C
(Source) (Middle) (Destination) (Source) (Middle) (Destination)
Thank you