CMPT 225 Stacks-part2.

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.
COSC 2006 Chapter 7 Stacks III
COMPSCI 105 S Principles of Computer Science 13 Stacks.
Chapter 6 Stacks. © 2005 Pearson Addison-Wesley. All rights reserved6-2 ADT Stack Figure 6.1 Stack of cafeteria dishes A stack –Last-in, first-out (LIFO)
Data Abstraction and Problem Solving with JAVA Walls and Mirrors Frank M. Carrano and Janet J. Prichard © 2001 Addison Wesley Data Abstraction and Problem.
CMPT 225 Stacks.
Chapter 3 Stacks.
© 2006 Pearson Addison-Wesley. All rights reserved7 B-1 Chapter 7 (continued) Stacks.
Infix, Postfix, Prefix.
© 2006 Pearson Addison-Wesley. All rights reserved7A-1 Chapter 7 Stacks.
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,
Chapter 6 Stacks. © 2005 Pearson Addison-Wesley. All rights reserved6-2 ADT Stack Figure 6.1 Stack of cafeteria dishes A stack –Last-in, first-out (LIFO)
1 Stacks (Walls & Mirrors - Chapter 6). 2 Overview The ADT Stack Array Implementation of a Stack Linked-List Implementation of a Stack Application Domain:
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.
Chapter 6 Stacks. © 2005 Pearson Addison-Wesley. All rights reserved6-2 The Abstract Data Type Specifications of an abstract data type for a particular.
Nirmalya Roy School of Electrical Engineering and Computer Science Washington State University Cpt S 122 – Data Structures Data Structures Stacks.
Stacks Chapter 6 Data Structures and Problem Solving with C++: Walls and Mirrors, Frank Carrano, © 2012.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley. Ver Chapter 6: Stacks Data Abstraction & Problem Solving with C++
© 2006 Pearson Addison-Wesley. All rights reserved7A-1 Chapter 7 Stacks (and a bit of generics for flavor)
Fundamentals of Python: From First Programs Through Data Structures Chapter 14 Linear Collections: Stacks.
1 CSC 222: Computer Programming II Spring 2005 Stacks and recursion  stack ADT  push, pop, peek, empty, size  ArrayList-based implementation, java.util.Stack.
Implementing Stacks Ellen Walker CPSC 201 Data Structures Hiram College.
CHAPTER 05 Compiled by: Dr. Mohammad Omar Alhawarat Stacks & Queues.
Computer Science 112 Fundamentals of Programming II Introduction to Stacks.
Computer Science Department Data Structure & Algorithms Problem Solving with Stack.
COP3530 Data Structures600 Stack Stack is one the most useful ADTs. Like list, it is a collection of data items. Supports “LIFO” (Last In First Out) discipline.
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.
Recursion as a Problem- Solving Technique Chapter 5 Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2013.
CHAPTER 3 STACK CSEB324 DATA STRUCTURES & ALGORITHM.
1 Stacks and Queues Reading: Sections 3.6 and 3.7.
Recursion as a Problem-Solving Technique. What is recursion? There are two types of recursive functions: Direct and Indirect Explain… There are two basic.
Chapter 6 B Stacks. © 2004 Pearson Addison-Wesley. All rights reserved6 B-2 Comparing Implementations All of the three implementations are ultimately.
Recursion as a Problem- Solving Technique Chapter 5 Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2013.
COMPSCI 105 SS 2015 Principles of Computer Science 09 ADT & Stacks.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley. Ver Chapter 13: Graphs Data Abstraction & Problem Solving with C++
COSC 2007 Data Structures II
© 2006 Pearson Addison-Wesley. All rights reserved 14 A-1 Chapter 14 Graphs.
CHP-3 STACKS.
Stacks & Queues. Introduction to Stacks and Queues Widely used data structures Ordered List of element Easy to implement Easy to use.
11/07/141 Abstract Data Types (ADTs) An abstract data type (ADT) is an abstraction of a data structure An ADT specifies: –Data stored –Operations on the.
Stacks A stack is a linear data structure that can be accessed only at one of its ends for storing and retrieving data LIFO (Last In First Out) structure.
CSE 3358 NOTE SET 8 Data Structures and Algorithms.
11/07/141 Abstract Data Types (ADTs) An abstract data type (ADT) is an abstraction of a data structure An ADT specifies: –Data stored –Operations on the.
6/12/20161 Abstract Data Types (ADTs) An abstract data type (ADT) is an abstraction of a data structure An ADT specifies: –Data stored –Operations on the.
Stacks The Stack ADT stores arbitrary objects. Insertions and deletions follow the last-in first-out (LIFO) scheme. The last item placed.
 Chapter 7 introduces the stack data type.  Several example applications of stacks are given in that chapter.  This presentation shows another use called.
Data Abstraction and Problem Solving with JAVA Walls and Mirrors Frank M. Carrano and Janet J. Prichard © 2001 Addison Wesley Data Abstraction and Problem.
Abstract Data Types (ADTs)
CS Data Structures Chapter 6 Stacks Mehmet H Gunes
CENG 213 Data Structures Stacks 5/15/2018 CENG 213.
Stacks Chapter 6.
Chapter 5 Recursion as a Problem-Solving Technique
COMPSCI 107 Computer Science Fundamentals
Infix to postfix conversion
Abstract Data Types (ADTs)
Copyright ©2012 by Pearson Education, Inc. All rights reserved
Visit for more Learning Resources
Chapter 6 Stacks.
Stacks Chapter 5 Adapted from Pearson Education, Inc.
Stacks Chapter 6 Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2013.
Abstract Data Types (ADTs)
Figure 6.1 Stack of cafeteria dishes. Figure 6.1 Stack of cafeteria dishes.
Chapters 6 and 7 Stacks.
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.
Presentation transcript:

CMPT 225 Stacks-part2

Converting Infix Expressions to Equivalent Postfix Expressions An infix expression can be evaluated by first being converted into an equivalent postfix expression Facts about converting from infix to postfix 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 All parentheses are removed

Let e be the next element in the infix expr if e is an operand append it to the output string. if e is “(“ push e to the stack if e is an operator If stack is empty push e into the stack Otherwise, pop operators of greater or equal precedence from the stack and append them to output string-stop when you see a “(“ or an operator of lower precedence. if e is “)” pop operators from the stack and append them to the output string until you see the matching “(“. when you reach the end of the string pop all the operators and attach them to the output.

for ( each character ch in the infix expr){ switch (ch) { case operand: postfixExp += ch; break; case ‘(‘: aStack.push(ch); break; case ‘)’: while (top of the stack is not ‘(‘) postfixExp += aStack.pop() aStack.pop(); break; case operator: while (!aStack.isEmpty() && top of the stack is not ‘(‘ && preced(ch) <= preced(top of the stack)) postfixExpr += aStack.pop(); aStack.push(ch); } while (!aStack.isEmpty()) postfixExp += aStack.pop();

Converting Infix Expressions to Equivalent Postfix Expressions Figure 7-9 A trace of the algorithm that converts the infix expression a - (b + c * d)/e to postfix form

Recognizing Strings in Languages Recognize whether a particular string is in the language: L={ w$w’: w is possibly an empty string of characters other than $ and w’=reverse(w)} Example ABC$CBA, $, AAB$BAA, …

Application: A Search Problem High Planes Airline Company (HPAir) Problem For each customer request, indicate whether a sequence of HPAir flights exists from the origin city to the destination city

Representing the Flight Data The flight map for HPAir is a graph Adjacent vertices Two vertices that are joined by an edge Directed path A sequence of directed edges Figure 7-10 Flight map for HPAir

A Nonrecursive Solution that Uses a Stack The solution performs an exhaustive search Beginning at the origin city, the solution will try every possible sequence of flights until either It finds a sequence that gets to the destination city It determines that no such sequence exists The ADT stack is useful in organizing an exhaustive search Backtracking can be used to recover from a wrong choice of a city

Start from the origin Select an arbitrary city and fly to it. Repeat step 2 until you reach the destination Possible outcomes: You eventually reach the destination You reach a city from which there is no departing flight. You go around a circle.

The first outcome is lucky and only happens if you select a correct flight at each step. Since we don’t know whether we are making the right decision at each step, we need to keep track of our decision so that if we made a mistake we could go back and make another decision. Using stack can help us keep track of our decisions.

We may end up in a loop with the above algorithm. aStack.push(originCity) while (destination is not on top && !aStack.isEmpty()){ if ( there is no flight out of the city on top of stack){ aStack.pop(); } else{ Select a destination city C from the city on top of the stack. aStack.push( C ); if (aStack.isEmpty()) return false; else return true; We may end up in a loop with the above algorithm.

To avoid the loop we must make sure we don’t visit a city more than once. aStack.push(originCity) Mark the originCity as visited while (destination is not on top && !aStack.isEmpty()){ if ( there is no flight out of the city on top of stack to an unvisited city){ aStack.pop(); } else{ Select an unvisited destination city C from the city on top of the stack. aStack.push( C ); Mark C as visited; if (aStack.isEmpty()) return false; else return true;

A Nonrecursive Solution that Uses a Stack Figure 7-11 The stack of cities as you travel a) from P; b) to R; c) to X; d) back to R; e) back to P; f) to W

A Nonrecursive Solution that Uses a Stack Figure 7-13 A trace of the search algorithm, given the flight map in Figure 6-9

A Recursive Solution boolean searchR(originCity, destinationCity){ Mark originCity as visited if (originCity is destinationCity) { return true } else { for (each unvisited city C adjacent to originCity) { if (searchR(C, destinationCity)) return false

The Relationship Between Stacks and Recursion The ADT stack has a hidden presence in the concept of recursion Typically, stacks are used by compilers to implement recursive methods During execution, each recursive call generates an activation record that is pushed onto a stack Stacks can be used to implement a nonrecursive version of a recursive algorithm

Summary ADT stack operations have a last-in, first-out (LIFO) behavior Algorithms that operate on algebraic expressions are an important application of stacks A stack can be used to determine whether a sequence of flights exists between two cities A strong relationship exists between recursion and stacks