Matching Balancing Parenthesis - PowerPoint PPT Presentation

1 / 11
About This Presentation
Title:

Matching Balancing Parenthesis

Description:

What happen if the stack is empty (meaning there is no such (') at that moment ? What happen if we have finished reading the expression, but the stack is not empty ? ... – PowerPoint PPT presentation

Number of Views:26
Avg rating:3.0/5.0
Slides: 12
Provided by: math81
Category:

less

Transcript and Presenter's Notes

Title: Matching Balancing Parenthesis


1
Applications of Stacks
  • Matching Balancing Parenthesis
  • Evaluating an postfix expression
  • Infix to postfix expression

2
Matching Balancing Parenthesis
  • Given an expression of ( and ), a ( must
    match with a ), or else it is illegal.
  • ( )( ), ( ( ( ) ) ), ( ( ( ) ) ( ) ) are all
    legal, while
  • ( ( ) (, ) ( ) ( are all illegal.
  • Obviously, counting the numbers of ( and ) in
    the expression is not enough.

3
Matching Balancing Parenthesis
  • Insights ? By convention, the expression is
    reading from left-to-right. A ) is matched
    with the closest, unmatched ( on its left.
  • For example,
  • ( ( ( ) ) ( ) )

4
Matching Balancing Parenthesis
  • Suppose we have a ). How do we know which (
    is closest and unmatched?
  • If we read the expression from left-to-right, the
    MOST RECENTLY UNMATCHED ( is cancelled with
    ).
  • How can we keep track of the MOST RECENTLY READ
    (LAST) ( ? (keep in mind there are many pending
    unmatched ().
  • Which data structure is keeping track of the most
    recent item ?

Stack ? LIFO structure
5
Matching Balancing Parenthesis
  • When we see a (, we push it into a stack.
  • When we see a ), we pop ( from the stack.
    This ( matches with current ).
  • What happen if the stack is empty (meaning there
    is no such () at that moment ?
  • What happen if we have finished reading the
    expression, but the stack is not empty ?
  • It means there are more ( than ) in the
    expression.

It means the input is illegal.
It means the input is illegal.
6
Infix to Postfix Expressions
  • Infix expression
  • Suppose the expression only has operators and
    and has a higher precedence than .
  • So, 523 10, and 1249, etc.
  • The expression may also have parenthesis to
    complicate the problem, i.e.,
  • (12)412
  • (1251)336.
  • (12(51))339.

7
Infix to Postfix Expressions
  • Postfix expression
  • 1 3
  • 1 2 4
  • 1 2 4
  • 6 5 2 3 8 3
  • are all postfix expressions.
  • No (, ) is in the expression.
  • To evaluate a postfix expression, we need a stack.

8
Infix to Postfix Expressions
  • Evaluate a postfix expression.
  • Read the expression from left-to-right.
  • When a number is seen, it is pushed onto the
    stack.
  • When an operator is seen, the operator is applied
    to the two numbers that are popped from the
    stack. The result is pushed on the stack.

9
Infix to Postfix Expressions
  • Example 6 5 2 3 8 3

10
Infix to Postfix Expressions
  • How to convert an infix expression to a postfix
    expression? Use a stack.
  • Read the infix expression from left-to-right.
  • When an operand (number) is read, output it.
  • If an operator (other than (, )) is read, pop
    the stack (and output the operator) until a lower
    precedence operator or ( is on the top of
    stack. Then push the current operator on the
    stack.
  • If ( is read, push it on the stack.
  • If a ) is read, pop the stacks (and output the
    operators), until we meet (. Pop that ( (do
    no output it).
  • Finally, if we read the end of the expression,
    pop the stack (and output the operators) until
    the stack is empty.

11
Infix to Postfix Expressions
  • Example (12(51))3
  • postfix expression ? 1 2 5 1 3
Write a Comment
User Comments (0)
About PowerShow.com