COSC 2006 Chapter 7 Stacks III

Slides:



Advertisements
Similar presentations
INFIX, PREFIX, & POSTFIX EXPRESSIONS. Infix Notation We usually write algebraic expressions like this: a + b This is called infix notation, because the.
Advertisements

Stacks & Their Applications COP Stacks  A stack is a data structure that stores information arranged like a stack.  We have seen stacks before.
Stacks Chapter 11.
Stacks - 3 Nour El-Kadri CSI Evaluating arithmetic expressions Stack-based algorithms are used for syntactical analysis (parsing). For example.
Prefix, Postfix, Infix Notation
Computer Science 112 Fundamentals of Programming II Applications of Stacks.
Stacks Chapter 21 Slides by Steve Armstrong LeTourneau University Longview, TX  2007,  Prentice Hall.
Arithmetic Expressions Infix form –operand operator operand 2+3 or a+b –Need precedence rules –May use parentheses 4*(3+5) or a*(b+c)
COMPSCI 105 S Principles of Computer Science 13 Stacks.
Lecture 12 – ADTs and Stacks.  Modularity  Divide the program into smaller parts  Advantages  Keeps the complexity managable  Isolates errors (parts.
Stacks21 Stacks II Adventures in Notation. stacks22 The trouble with infix... Rules for expression evaluation seem simple -- evaluate expression left.
Arithmetic Expressions
CMPT 225 Stacks-part2.
Topic 15 Implementing and Using Stacks
Stacks21 Stacks II Adventures in Notation. stacks22 The trouble with infix... Rules for expression evaluation seem simple -- evaluate expression left.
Stacks & Queues Infix Calculator CSC 172 SPRING 2002 LECTURE 5.
Infix to postfix conversion Process the tokens from a vector infixVect of tokens (strings) of an infix expression one by one When the token is an operand.
© 2006 Pearson Addison-Wesley. All rights reserved7 B-1 Chapter 7 (continued) Stacks.
Stacks & Queues Infix Calculator CSC 172 SPRING 2004 LECTURE 13.
Infix, Postfix, Prefix.
© 2006 Pearson Addison-Wesley. All rights reserved7A-1 Chapter 7 Stacks.
Reverse Polish Expressions Some general observations about what they are and how they relate to infix expressions. These 9 slides provide details about.
Fall 2007CS 2251 Stacks Chapter 5. Fall 2007CS 2252 Chapter Objectives To learn about the stack data type and how to use its four methods: push, pop,
Stacks Chapter Chapter Contents Specifications of the ADT Stack Using a Stack to Process Algebraic Expressions Checking for Balanced Parentheses,
Fall 2007CS 2251 Stacks Chapter 5. Fall 2007CS 2252 Chapter Objectives To learn about the stack data type and how to use its four methods: push, pop,
1 CSCD 326 Data Structures I Infix Expressions. 2 Infix Expressions Binary operators appear between operands: W - X / Y - Z Order of evaluation is determined.
Topic 15 Implementing and Using Stacks
© 2006 Pearson Addison-Wesley. All rights reserved7A-1 Chapter 7 Stacks (and a bit of generics for flavor)
Data Structures Lecture : Stacks (Infix, Postfix and Prefix Expressions) Azhar Maqsood NUST Institute of Information Technology (NIIT)
Fundamentals of Python: From First Programs Through Data Structures Chapter 14 Linear Collections: Stacks.
Stack Applications.
CSC 205 Programming II Postfix Expressions. Recap: Stack Stack features Orderly linear structure Access from one side only – top item Stack operations.
Computer Science Department Data Structure & Algorithms Problem Solving with Stack.
SAK 3117 Data Structures Chapter 3: STACKS. Objective To introduce: Stack concepts Stack operations Stack applications CONTENT 3.1 Introduction 3.2 Stack.
Chapter 7 Stacks. © 2004 Pearson Addison-Wesley. All rights reserved 7-2 The Abstract Data Type: Developing an ADT During the Design of a Solution Specifications.
EC-211 DATA STRUCTURES LECTURE 8. STACK APPLICATIONS Infix, Prefix, and Postfix Expressions Example – Infix: A+B – Prefix: +AB – Postfix: AB+
CHAPTER 3 STACK CSEB324 DATA STRUCTURES & ALGORITHM.
Chapter 6 B Stacks. © 2004 Pearson Addison-Wesley. All rights reserved6 B-2 Comparing Implementations All of the three implementations are ultimately.
Basic Data Structures Stacks. A collection of objects Objects can be inserted into or removed from the collection at one end (top) First-in-last-out.
Stacks An Abstract Data Type. Restricted Access Unlike arrays, stacks only allow the top most item to be accessed at any time The interface of a stack.
CHP-3 STACKS.
Prefix, Postfix, Infix Notation. Infix Notation  To add A, B, we write A+B  To multiply A, B, we write A*B  The operators ('+' and '*') go in between.
Stacks Chapter 5 © 2015 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved. Data Structures and Abstractions with Java, 4e Frank Carrano.
DATA STRUCTURES Application of Stack – Infix to Postfix conversion a Joshua Presentation.
CC 215 DATA STRUCTURES MORE ABOUT STACK APPLICATIONS Dr. Manal Helal - Fall 2014 Lecture 6 AASTMT Engineering and Technology College 1.
Applications of Stack Maitrayee Mukerji. Stacks Last In First Out (LIFO List) ◦ FILO? Insertions and Deletions from the same end called the Top Push(),
CSC 172 DATA STRUCTURES. A TALE OF TWO STRUCTURES.
Review Use of Stack Introduction Stack in our life Stack Operations
Stacks Access is allowed only at one point of the structure, normally termed the top of the stack access to the most recently added item only Operations.
CS Data Structures Chapter 6 Stacks Mehmet H Gunes
Stacks Chapter 6.
COMPSCI 107 Computer Science Fundamentals
Infix to postfix conversion
CSC 172 DATA STRUCTURES.
Copyright ©2012 by Pearson Education, Inc. All rights reserved
Stack application: postponing data usage
PART II STACK APPLICATIONS
Stacks Chapter 5 Adapted from Pearson Education, Inc.
Infix to Postfix Conversion
Queue Applications Lecture 31 Mon, Apr 9, 2007.
Infix to Postfix Conversion
Topic 15 Implementing and Using Stacks
(Part 2) Infix, Prefix & Postfix
Queue Applications Lecture 31 Tue, Apr 11, 2006.
Data Structures and Algorithms 2/2561
17CS1102 DATA STRUCTURES © 2016 KL University – The contents of this presentation are an intellectual and copyrighted property of KL University. ALL RIGHTS.
Chapter 7 (continued) © 2011 Pearson Addison-Wesley. All rights reserved.
Stacks A stack is an ordered set of elements, for which only the last element placed into the stack is accessible. The stack data type is also known as.
© 2016 Pearson Education, Ltd. All rights reserved.
Presentation transcript:

COSC 2006 Chapter 7 Stacks III April 12, 2017 CS2006 - Data Structures I Chapter 7 Stacks III Chapter 6: Stacks

Topics Applications Infix to postfix expression Evaluate postfix expression

Infix Expressions Binary operators appear between operands: COSC 2006 April 12, 2017 Infix Expressions Binary operators appear between operands: W - X / Y – Z (4+3)*2 (2+3)/(9-4) Order of evaluation is determined by: precedence rules parentheses association (L to R or R to L) Chapter 6: Stacks

Application: Algebraic Expressions Infix Expression Evaluation To evaluate an infix expression Convert the infix expression into postfix Evaluate the postfix expression using stacks

Postfix Expressions Binary operators appear after both operands: COSC 2006 April 12, 2017 Postfix Expressions Binary operators appear after both operands: X + Y in postfix form is: X Y + Order of operations is completely determined by the expression no parentheses or precedence required for example: A * B - C becomes A B * C - Chapter 6: Stacks

Postfix Expression Infix Postfix 6-1 (4+3)*2 (2+3)/(9-4) COSC 2006 April 12, 2017 Postfix Expression Infix Postfix 6-1 (4+3)*2 (2+3)/(9-4) Chapter 6: Stacks

Postfix Expression Evaluation Assumptions: The string is syntactically correct postfix expression No unary operators are present No exponentiation operators are present

Postfix Expression Evaluation COSC 2006 April 12, 2017 Postfix Expression Evaluation Evaluation of postfix expressions makes use of an operand stack. Algorithm: Parse expression from left to right When an operand is encountered, push it onto stack when an operator is encountered, pop the last two operands off the stack and apply the operation, and push the result onto the stack when the expression is completely scanned, the final result will be on the stack 2 3 + 9 4 - Chapter 6: Stacks

Postfix Expression Evaluation Pseudocode: for ( each character ch in the string ) { if (ch is an operand ) Push value that operand ch represents onto stack else / / ch is an operator named op { / / evaluate and push the result operand2 = top of stack Pop the stack operand1 = top of stack result = operand1 op operand2 Push Result onto stack } / / end if } / / end for

Example Evaluate 2 3 4 5 + 2 * 1 + + 3 2 * + *

Converting Infix into Postfix COSC 2006 April 12, 2017 Converting Infix into Postfix Since postfix expressions are easily evaluated, the easiest way to evaluate infix is to convert from infix to postfix and then evaluate. This conversion uses an operator stack since low precedence operators must be saved and applied after high precedence ones. Chapter 6: Stacks

Converting Infix into Postfix COSC 2006 April 12, 2017 Converting Infix into Postfix The operands always stay in the same order with respect to one another An operator will move only to “ the right” with respect to the operands If in infix expression, the operand x precedes the operator op, in the postfix, the operand x also precedes the operator x All parentheses are removed Chapter 6: Stacks

Converting Infix into Postfix Scan infix string from left to right Each time an operand is encountered, copy it to output When a bracket is encountered, check its orientation. Push left brackets onto stack If it is a right bracket, pop all operators on the stack and copy them to output until a matching left bracket is encountered. Discard the left bracket

Converting Infix into Postfix When an operator is encountered, check the top item of the stack If the priority is >= the current operator, pop the top operator and copy it to output Continue until an operator of lower priority is encountered or until the stack is empty Finally, push current operator onto the stack When the end of the expression is reached, copy the remaining stack contents to output in the order popped

Converting Infix into Postfix Precedence Table Operator Name Precedence when on stack Precedence when on input ( Opening parentheses always stack 7 ) Closing parentheses never stacked ^ Exponentiation 6 5 * / Multiply & divide 4 3 + - Add & Subtract 2 1

Conversion Example W - X /Y + Z '-' is pushed onto operator stack COSC 2006 April 12, 2017 Conversion Example W - X /Y + Z '-' is pushed onto operator stack '/' has higher precedence than '-' on stack - it gets pushed '+' has lower precedence than '/' on stack - pop '/' and output - then '-' is on top of stack - since subtraction associates left to right - pop '-' off stack and output - push '+' onto stack when end of expression is reached pop remaining operator and output resulting expression is: W X Y /- Z + Chapter 6: Stacks

Converting Infix into Postfix COSC 2006 April 12, 2017 Converting Infix into Postfix stack.push(EOL marker) for (each character ch in the infix expression) { switch (ch) { case operand: //append ch to output postfix expression postfixExpr = postfixExpr + ch; case '(': stack.push(ch); // push ch onto operator stack case ')': while (top of stack is not '(' ) { postfixExpr = postfixExpr + stack.pop(); // pop and append expr. } stack.pop(); case operator: while (!stack.isEmpty() && inputPrec(ch) <= stackPrec(stack.top())) { postfixExpr = postfixExpr + stack.pop(); stack.push(ch); } // end for while (!stack.isEmpty() { // pop all remaining operators off stack and append Chapter 6: Stacks

Conversion Example 2 + 3 * [(7-5)/2] 2 3 7 5 2 copy into output COSC 2006 April 12, 2017 Conversion Example 2 + 3 * [(7-5)/2] 2 3 7 5 2 copy into output ‘+' is pushed onto operator stack 3 copy into output ‘*' has higher precedence than ‘+' on stack - it gets pushed ‘[‘ is left bracket, push it onto stack ‘(‘is left bracket, push it onto stack 7 is copied into output ‘-' has higher precedence than ‘(‘, push onto stack Chapter 6: Stacks

Conversion Example 2 + 3 * [(7-5)/2] COSC 2006 April 12, 2017 Chapter 6: Stacks

Prefix Expressions Infix Prefix 6-1 - 6 1 (4+3)*2 * + 4 3 2 COSC 2006 April 12, 2017 Prefix Expressions Infix Prefix 6-1 - 6 1 (4+3)*2 * + 4 3 2 (2+3)/(9-4) / + 2 3 – 9 4 Chapter 6: Stacks

Review Which of the following is NOT true about converting infix expressions to postfix expressions? the operands always stay in the same order with respect to one another the operators always stay in the same order with respect to one another an operator will move only “to the right” with respect to the operands all parentheses are removed

Review Which of the following is the postfix form of the infix expression: (a + b) * c / d a b + c * d / a b * c / d + a + b * c / d a b + c d * /

Review StackInterface provides the specifications for ______. only the array-based implementation of a stack only the reference-based implementation of a stack only an implementation of a stack that uses the ADT list all the implementations of a stack

Review In the StackInterface class, the push method accepts as its parameter an item that is an instance of ______. Integer Double String Object