CS 206 Introduction to Computer Science II

Similar documents
CS 206 Introduction to Computer Science II

CS 206 Introduction to Computer Science II

STACKS. A stack is defined in terms of its behavior. The common operations associated with a stack are as follows:

STACKS AND QUEUES. Problem Solving with Computers-II

PA3 Design Specification

CS 206 Introduction to Computer Science II

Some Applications of Stack. Spring Semester 2007 Programming and Data Structure 1

09 STACK APPLICATION DATA STRUCTURES AND ALGORITHMS REVERSE POLISH NOTATION

CS W3134: Data Structures in Java

EC8393FUNDAMENTALS OF DATA STRUCTURES IN C Unit 3

Postfix (and prefix) notation

Lecture Data Structure Stack

Data Structure using C++ Lecture 04. Data Structures and algorithm analysis in C++ Chapter , 3.2, 3.2.1

Stack Abstract Data Type

Stack Applications. Lecture 27 Sections Robb T. Koether. Hampden-Sydney College. Wed, Mar 29, 2017

CS 206 Introduction to Computer Science II

12 Abstract Data Types

CS 206 Introduction to Computer Science II

CS 206 Introduction to Computer Science II

Lab 7 1 Due Thu., 6 Apr. 2017

Programming Abstractions

DEEPIKA KAMBOJ UNIT 2. What is Stack?

Lecture 4 Stack and Queue

March 13/2003 Jayakanth Srinivasan,

Stacks, Queues and Hierarchical Collections

Formal Languages and Automata Theory, SS Project (due Week 14)

The Bucharest University of Economic Studies. Data Structures. ADTs-Abstract Data Types Stacks and Queues

CS 206 Introduction to Computer Science II

SimpleCalc. which can be entered into a TI calculator, like the one on the right, like this:

CS 206 Introduction to Computer Science II

Stacks. Chapter 5. Copyright 2012 by Pearson Education, Inc. All rights reserved

Stacks, Queues and Hierarchical Collections. 2501ICT Logan

The Stack and Queue Types

Programming Abstractions

How to Win Coding Competitions: Secrets of Champions. Week 2: Computational complexity. Linear data structures Lecture 5: Stack. Queue.

CSCA48 Summer 2018 Week 3: Priority Queue, Linked Lists. Marzieh Ahmadzadeh University of Toronto Scarborough

Prefix/Infix/Postfix Notation

ADVANCED DATA STRUCTURES USING C++ ( MT-CSE-110 )

CS200: Queues. Prichard Ch. 8. CS200 - Queues 1

C27a: Stack and Queue

DATA STRUCTURE UNIT I

Sample Question Paper

CSE 332: Data Structures. Spring 2016 Richard Anderson Lecture 1

CS 211. Project 5 Infix Expression Evaluation

Lecture 12 ADTs and Stacks

Stacks. stacks of dishes or trays in a cafeteria. Last In First Out discipline (LIFO)

MULTIMEDIA COLLEGE JALAN GURNEY KIRI KUALA LUMPUR

CS 106 Introduction to Computer Science I

Introduction. Problem Solving on Computer. Data Structures (collection of data and relationships) Algorithms

Programming in C++ Prof. Partha Pratim Das Department of Computer Science and Engineering Indian Institute of Technology, Kharagpur

CSCA48 Winter 2018 Week 3: Priority Queue, Linked Lists. Marzieh Ahmadzadeh, Nick Cheng University of Toronto Scarborough

INSTITUTE OF AERONAUTICAL ENGINEERING

R13. II B. Tech I Semester Supplementary Examinations, May/June DATA STRUCTURES (Com. to ECE, CSE, EIE, IT, ECC)

1 P age DS & OOPS / UNIT II

DATA STRUCUTRES. A data structure is a particular way of storing and organizing data in a computer so that it can be used efficiently.

First Semester - Question Bank Department of Computer Science Advanced Data Structures and Algorithms...

Stack Applications. Lecture 25 Sections Robb T. Koether. Hampden-Sydney College. Mon, Mar 30, 2015

Data Structure using C++ Lecture 04. Data Structures and algorithm analysis in C++ Chapter , 3.2, 3.2.1

VALLIAMMAI ENGINEERING COLLEGE SRM Nagar, Kattankulathur

Stacks and Queues

VALLIAMMAI ENGINEERING COLLEGE

Programming Abstractions

Abstract Data Types. Stack. January 26, 2018 Cinda Heeren / Geoffrey Tien 1

Stacks and Queues. Introduction to abstract data types (ADTs) Stack ADT. Queue ADT. Time permitting: additional Comparator example

#06 More Structures LIFO FIFO. Contents. Queue vs. Stack 3. Stack Operations. Pop. Push. Stack Queue Hash table

CSC148 Week 2. Larry Zhang

Programming II (CS300)

CS 106 Introduction to Computer Science I

BBM 201 DATA STRUCTURES

Name CPTR246 Spring '17 (100 total points) Exam 3

MIDTERM EXAMINATION Spring 2010 CS301- Data Structures

-The Hacker's Dictionary. Friedrich L. Bauer German computer scientist who proposed "stack method of expression evaluation" in 1955.

CSCI 204 Introduction to Computer Science II. Lab 6: Stack ADT

R10 SET - 1. Code No: R II B. Tech I Semester, Supplementary Examinations, May

15. Stacks and Queues

RAJALAKSHMI ENGINEERING COLLEGE Thandalam, Chennai Department of Computer Science and Engineering CS17201 DATA STRUCTURES Unit-I-Assignment

Stack and Its Implementation

Motivation for Queues

Stacks Fall 2018 Margaret Reid-Miller

Monday, November 13, 2017

CSCI 200 Lab 4 Evaluating infix arithmetic expressions

Programming Abstractions

Assessment of Programming Skills of First Year CS Students: Problem Set

Linear Data Structure

MAHARASHTRA STATE BOARD OF TECHNICAL EDUCATION (Autonomous) (ISO/IEC Certified)

CSE 143. Lecture 4: Stacks and Queues

Data Structures & Algorithm Analysis. Lecturer: Souad Alonazi

Revision Statement while return growth rate asymptotic notation complexity Compare algorithms Linear search Binary search Preconditions: sorted,

Chapter 04: Instruction Sets and the Processor organizations. Lesson 18: Stack-based processor Organisation

EEE2020 Data Structures and Algorithms Abstract Data Types: Stacks and Queues

CSE 326 Team. Today s Outline. Course Information. Instructor: Steve Seitz. Winter Lecture 1. Introductions. Web page:

CS171 Midterm Exam. October 29, Name:

CDA 3103 Computer Organization Homework #7 Solution Set

Lists, Stacks, and Queues. (Lists, Stacks, and Queues ) Data Structures and Programming Spring / 50

Introduction p. 1 Pseudocode p. 2 Algorithm Header p. 2 Purpose, Conditions, and Return p. 3 Statement Numbers p. 4 Variables p. 4 Algorithm Analysis

Programming, Data Structures and Algorithms Prof. Hema A Murthy Department of Computer Science and Engineering Indian Institute of Technology, Madras

CS-141 Exam 2 Review November 10, 2017 Presented by the RIT Computer Science Community

Advanced Java Concepts Unit 3: Stacks and Queues

Stacks, Queues (cont d)

F453 Module 7: Programming Techniques. 7.2: Methods for defining syntax

Transcription:

CS 206 Introduction to Computer Science II 03 / 31 / 2017 Instructor: Michael Eckmann

Today s Topics Questions? Comments? finish RadixSort implementation some applications of stack Priority Queues Michael Eckmann - Skidmore College - CS 206 - Spring 2017

Queues and Stacks Radix Sort. consider the job of sorting (base 10) integers let's limit them to 0-99 for now (all >=0 and <= 100) we handled this in several ways already a totally different way is the following way start with an unsorted list separate the numbers to be sorted into 10 different bins based on their 1's digit then, get the numbers out of the bins and make a new list (take numbers from bin 0, then add on to the end of the list the numbers in bin 1,... and so on, finally adding to the end of the list the numbers from bin 9) then separate this list into 10 bins based on the 10's digit. get the numbers out of the bins like before to get a sorted list.

Queues and Stacks Applications of stacks postfix expressions (reverse Polish notation) infix (what we're used to) is like: 1.07*3.85 + 14.05 + 1.07*8.75 postfix is like: 1.07, 8.75, *, 14.05, +, 1.07, 3.85, *, + infix result requires us to do the multiplies first and then store the intermediate values and then do the 2 adds. postfix operates in the following way: when see a number, push it when see an operator, perform it to the two top numbers on the stack and push the result final result in our example is: 27.532

Queues and Stacks Applications of stacks converting from infix (with only +, *, (, ) ) to postfix expressions infix example (assume normal precedence): a + b*c + ( d*e + f ) * g postfix for our example could be: a b c * + d e * f + g * + let's look at the algorithm to do this using a stack

Queues and Stacks converting infix to postfix when an operand (a, b, c, etc.) is read, write it to the output if we see a ) then we pop the stack and write to output until a ( is on top of the stack --- at which point we pop ( but do not output it. when a + or * is read, examine the top of the stack if it is empty or if top is of lower precedence than the one just read, or if top is (, then push the operator when a + or * is read if top is of the stack does not have lower precedence than the one just read, then pop the operator and write to output when a ( is read push it When finished reading input, pop stack, write to output until stack is empty Let's try this algorithm with our example: infix: a + b*c + ( d*e + f ) * g postfix (expected output): a b c * + d e * f + g * +

Priority Queues The main difference between a Queue and a Priority Queue is that In a Queue, an item will be dequeued based ONLY on the order in which it was enqueued In a Priority Queue Every item is enqueued with a priority value Higher priority items have preference when dequeuing Items with the same priority can be enqueued / dequeued in any order

Priority Queues Priority queues have the following characteristics Each item placed into a priority queue has a priority value associated with it When a dequeue is requested from a priority queue, we dequeue the highest priority item We need a way to determine if the priority queue is empty We can also have a peek to see what item has the highest priority without removing it It is best to limit priority values to be integers (reason to be seen shortly).

Priority Queues It is also wise to maintain the following values for a priority queue current size (how many items are in the whole priority queue) the priority of the highest priority item in the queue to make dequeue easier however it causes (some) additional work after the dequeue happens we need to possibly change this value after an enqueue we just need to check if the one we added is higher than the highest, if so, change it.

Priority Queues Implementation of a priority queue could be an array of queues. The index of the array is the priority value (see how we want our priority values to be integers?) The range of priority values (all integers) determines how many queues we're storing --- that is, how many elements of the array there will be. Let's implement a priority queue in this way now.