You are on page 1of 26

19AIE111 Data Structure and Algorithms 1 (3-1-0-4)

S2 B.Tech CSE(AI)
Week 3 Lecture 1 STACK ADT
Overview
• STACK ADT. And it’s operations. Implementation completed.
• Applications discussion ongoing .
• Undo operation.
• Recursive calls
3. Checking balancing parenthesis
• Given an expression we need to find whether the parenthesis in the
expression are balanced or not .
Examples: (A+B)
(A+B)*(C+D)

((X+y)+(a+b) .

Compilers performs this check .


Naïve approaches
• Counting the opening and closing parenthesis.
[]()
)(
Apart from count some other properties should be satisfied.
Every opening parenthesis must find it’s closing parenthesis to right of
it. And closing one should find it’s counter part to the left.
[(])

Stack based Solution


Stack Based Solution Logic
• Scan the expression from left to right.
• Whenever an opening parenthesis is encountered . Push it on to the
stack.
• If a closing parathesis is encountered check the stack top and scanned
symbol. If they are compatible then pop and continue scaning. Else
report they are not balanced.
• Example 1.

Not Balanced.
• Example 2.

Balanced.
Algorithm

Time complexity= O(n), n is the


expression length .
• Example 3

Not Balanced.
Another application of STACK
• Evaluation of Arithmetic expressions.

Examples of expressions: 2+3, A*B, (P+Q)*R …


Infix expression:
<Operand > <Operator><Operand>.

How to evaluate these expressions?


2+3*5= 25 or 17.
• So we have precedence rule.

(* ,/) same precedence and (+ , –) same precedence.


• Infix expressions are readable and solvable by humans because of
easily distinguishable order of operators, but compiler doesn't have
integrated order of operators.

• Hence to solve the Infix Expression compiler will scan the expression
multiple times to solve the sub-expressions in expressions orderly
which is very in-efficient.
• Example: a+b*c+d

• To avoid this traversing, Infix expressions are converted to Postfix


expression or Prefix expression before evaluation. Example:
abc*+d+.
• Free from parenthesis and doesn’t consider this precedence ,
associativity
Prefix notation and Postfix notation
Prefix Notation:
• <Operator><Operand > <Operand >

Postfix Notation .
• <Operand > <Operand > <Operator>
Examples
Postfix is good? Why?
• While evaluating the expression order of evaluation is not disrupted?
Prefix notation:
Postfix notation
Algorithm
Time complexity= O(n), n is the
expression length .
Stack Based Solution
• Q– Infix Expression
• P- Post fix expression
• S- Character Stack – Temporarily stores parenthesis and operators.
• Initial step: Push ‘(’ on to stack and ‘)’ onto Q, Infix expression.
• Operand------ Add it to P
• Operator ----Perform some operations . Add the scanned operator to
Stack
• (------- push on to stack
• ) ------- pop from the stack Until a ( is found in the stack.
Example
• Step 1:
Example 2:
Example :
• Step 1:
Example: Find the answers

1. (A-B)*(D/E)
2. 2.
TO Do List
• Revise the applications.
• Next week a MCQ Quiz will be there on stack. Details we will discuss
on Friday
• Attend the lab .( 1- 33) Join in channel 1 with Ms Lekshmi Ma’am
• (34- 76) ) Join in channel 2 with me.
• Lab from 2:00 3:30 PM be in the meeting. After that leave the
channel be online till 5:00 PM. Upload the class work and leave.

You might also like