Presentation is loading. Please wait.

Presentation is loading. Please wait.

Stacks Chapter 5 Adapted from Pearson Education, Inc.

Similar presentations


Presentation on theme: "Stacks Chapter 5 Adapted from Pearson Education, Inc."— Presentation transcript:

1 Stacks Chapter 5 Adapted from Pearson Education, Inc.

2 Contents Specifications of the ADT Stack
Using a Stack to Process Algebraic Expressions A Problem Solved: Checking for Balanced Delimiters in an Infix Algebraic Expression A Problem Solved: Transforming an Infix Expression to a Postfix Expression A Problem Solved: Evaluating Postfix Expressions A Problem Solved: Evaluating Infix Expressions The Program Stack Java Class Library: The Class Stack Adapted from Pearson Education, Inc.

3 Objectives Describe operations of ADT stack
Use stack to decide whether delimiters in an algebraic expression are paired correctly Use stack to convert infix expression to postfix expression Use stack to evaluate postfix expression Use stack to evaluate infix expression Use a stack in a program Describe how Java run-time environment uses stack to track execution of methods Adapted from Pearson Education, Inc.

4 Specifications of a Stack
Organizes entries according to order added All additions added to one end of stack Added to “top” Called a “push” Access to stack restricted Access only top entry Remove called a “pop” Adapted from Pearson Education, Inc.

5 ADT Stack Adapted from Pearson Education, Inc.

6 ADT Stack Adapted from Pearson Education, Inc.

7 Adapted from Pearson Education, Inc.
A Stack Interface public interface StackInterface < T > { /** Adds a new entry to the top of this newEntry an object to be added to the stack */ public void push (T newEntry); /** Removes and returns this stacks top either the object at the top of the stack or, if the stack is empty before the operation, null */ public T pop (); /** Retrieves this stacks top either the object at the top of the stack or null if the stack is empty */ public T peek (); /** Detects whether this stack is true if the stack is empty */ public boolean isEmpty (); /** Removes all entries from this stack */ public void clear (); } // end StackInterface Adapted from Pearson Education, Inc.

8 Adapted from Pearson Education, Inc.
Stack Usage Example Adapted from Pearson Education, Inc.

9 Adapted from Pearson Education, Inc.
Stack Usage Example Adapted from Pearson Education, Inc.

10 Algebraic Expressions
Algebraic expressions are composed of Operands (variables, constants) Operators (+, -, /, *, ^) Operators can be unary or binary Different precedence notations Infix a + b Prefix + a b Postfix a b + Precedence must be maintained Order of operators Use of parentheses (must be balanced) Use stacks to evaluate parentheses usage Scan expression Push symbols Pop symbols Adapted from Pearson Education, Inc.

11 Algebraic Expressions
Algebraic expressions are composed of Operands (variables, constants) Operators (+, -, /, *, ^) Operators can be unary or binary Precedence must be maintained According to order of operators According to parentheses When parentheses are used, they must be balanced Different notations Infix a + b Prefix + a b Postfix a b + Adapted from Pearson Education, Inc.

12 Parentheses Balance Checker
Scan expression from left to right When you encounter: ( { [  Push opening parenthesis symbols onto stack ) } ]  Pop symbol from stack and check if it matches Figure The contents of a stack during the scan of an expression that contains the balanced delimiters { [ ( ) ] } Adapted from Pearson Education, Inc.

13 Parentheses Balance Checker
Figure 5-4 The contents of a stack during the scan of an expression that contains the unbalanced delimiters { [ ( ] ) } Adapted from Pearson Education, Inc.

14 Parentheses Balance Checker
Figure 5-5 The contents of a stack during the scan of an expression that contains the unbalanced delimiters [ ( ) ] } Can not pop to match } Adapted from Pearson Education, Inc.

15 Parentheses Balance Checker
Figure 5-6 The contents of a stack during the scan of an expression that contains the unbalanced delimiters { [ ( ) ] Reached end of string Adapted from Pearson Education, Inc.

16 Infix vs. Postfix Expressions
In-fix expressions: Operator comes between two operands  a + b Precedence of operators determined as follows: + and -  have lowest precedence, are evaluated left-to-right * and /  have higher precedence, are evaluated left-to-right ^  has highest precedence, is evaluated right-to-left Parentheses  override precedence Post-fix expressions: Operator comes after two operands  a b + Precedence is determined by order of operators in the expression No parentheses are used in postfix expressions Adapted from Pearson Education, Inc.

17 Infix to Postfix Conversion - Manual
Steps: Put parentheses according to correct operator precedence Move operator to right inside parentheses Remove parentheses Example: (a + b) * c ( ( a + b ) * c ) ( ( a b + ) c * ) a b + c * Notice the following: Postfix expressions do NOT need parentheses to specify precedence The order of operands in the postfix expression remains the same as in the infix expression Example: a + b * c ( a + ( b * c ) ) ( a ( b c * ) + ) a b c * + Adapted from Pearson Education, Inc.

18 Infix to Postfix Conversion - Algorithm
Algorithm basics Scan infix expression left to right When operand found, place it at end of new postfix expression When operator found, save it until new position is determined Why? The 2nd operand must be output before the operator Maybe there will be an operator with higher precedence, then it needs to be output first Adapted from Pearson Education, Inc.

19 Infix to Postfix Conversion - Algorithm
Operand Append to end of output expression Operator ^ Push ^ onto stack Operators +, -, *, / Pop from stack, append to output expression, until stack empty or top operator has lower precedence than new operator Push new operator onto stack Open parenthesis Push ( onto stack Close parenthesis Pop operators from stack and append to output Until open parenthesis is popped. Discard both parentheses Keep popping all operators that have higher or equal precedence Adapted from Pearson Education, Inc.

20 Infix to Postfix Conversion – Example (1)
Figure 5-7 Converting a + b * c to postfix form Next Char Postfix Operator Stack (bottom to top) a (empty) + b a b * + * c a b c (end of infix) a b c * a b c * + Adapted from Pearson Education, Inc.

21 Infix to Postfix Conversion – Example (1)
Figure 5-7 Converting a + b * c to postfix form Next Char Postfix Operator Stack (bottom to top) a (empty) + b a b * + * c a b c (end of infix) a b c * a b c * + Adapted from Pearson Education, Inc.

22 Infix to Postfix Conversion – Example (2)
Converting the following to postfix form 7 + 2 ^ ( ) / ( * 3 – 5 ) The only thing that can pop an open parenthesis is a matching closing parenthesis Next Char Postfix Operator Stack (bottom to top) 7 (empty) + 2 72 ^ +^ ( +^( 1 721 +^(+ 3 7213 Adapted from Pearson Education, Inc.

23 Infix to Postfix Conversion – Example (2)
Converting the following to postfix form 7 + 2 ^ ( ) / ( * 3 – 5 ) Next Char Postfix Operator Stack (bottom to top) 3 7213 +^(+ ) 7213+ +^ / 7213+^ +/ ( +/( 6 7213+^6 + +/(+ 5 7213+^65 * +/(+* Adapted from Pearson Education, Inc.

24 Infix to Postfix Conversion – Example (2)
Converting the following to postfix form 7 + 2 ^ ( ) / ( * 3 – 5 ) Are we done? We are only done when we reach the end of the infix expression AND the stack is empty. Next Char Postfix Operator Stack (bottom to top) * 7213+^65 +/(+* 3 7213+^653 - 7213+^653* +/(+ 7213+^653*+ +/(- 5 7213+^653*+5 ) 7213+^653*+5- +/ (end of infix) 7213+^653*+5-/ + 7213+^653*+5-/+ (empty) Adapted from Pearson Education, Inc.

25 Infix to Postfix Conversion – Example (2)
Converting the following to postfix form 7 + 2 ^ ( ) / ( * 3 – 5 ) Can you evaluate this? 7 + 2 ^ 4 / (6 + 5 * 3 – 5) = / ( – 5) = / (21 – 5) = / 16 = 7 + 1 = 8 Next Char Postfix Operator Stack (bottom to top) * 7213+^65 +/(+* 3 7213+^653 - 7213+^653* +/(+ 7213+^653*+ +/(- 5 7213+^653*+5 ) 7213+^653*+5- +/ (end of infix) 7213+^653*+5-/ + 7213+^653*+5-/+ (empty) Adapted from Pearson Education, Inc.

26 Postfix Evaluation - Algorithm
Any operator operates on its two previous operands Example - manual ^ * / + 7 2 4 ^ * / + / + / + / + 7 1 + 8 Algorithm basics Scan postfix expression left to right When operand found, push it onto operand stack When operator found: Pop stack for 2nd operand Pop stack for 1st operand Evaluate Push result onto operand stack Adapted from Pearson Education, Inc.

27 Evaluating Postfix Expression – Example (1)
^ * / + 1 + 3 2 ^ 4 7 2 7 1 2 7 3 1 2 7 427 167 6 167 56167 5 * 3 6 + 15 21 - 5 16 / 16 7 + 1 356167 156167 21167 5 21 167 16167 17 8 Adapted from Pearson Education, Inc.

28 Evaluating Infix Expressions
evaluation of a + b * c when a is 2, b is 3, and c is 4 after reaching the end of the expression while performing the multiplication; while performing the addition Adapted from Pearson Education, Inc.

29 The Program Stack when main begins execution
when methodA begins execution when methodB begins execution Adapted from Pearson Education, Inc.

30 Java Class Library: The Class Stack
Has a single constructor Creates an empty stack Remaining methods – differences from our StackInterface are highlighted public T push(T item); public T pop(); public T peek(); public boolean empty(); Adapted from Pearson Education, Inc.

31 Adapted from Pearson Education, Inc.
End Chapter 5 Adapted from Pearson Education, Inc.


Download ppt "Stacks Chapter 5 Adapted from Pearson Education, Inc."

Similar presentations


Ads by Google