Priority Queues and Heaps. Overview Our last ADT: PriorityQueueADT A new data structure: heaps One more sorting algorithm: heapsort Priority Queues and.

Slides:



Advertisements
Similar presentations
CS 206 Introduction to Computer Science II 10 / 22 / 2008 Instructor: Michael Eckmann.
Advertisements

Priority Queues. 2 Priority queue A stack is first in, last out A queue is first in, first out A priority queue is least-first-out The “smallest” element.
Queues and Priority Queues
Searching and Sorting. Overview Search Analysis of search algorithms Sorting Analysis of sort algorithms Recursion p. 2 of 26.
Review. What to know You are responsible for all material covered in lecture, the readings, or the programming assignments There will also be some questions.
© 2006 Pearson Addison-Wesley. All rights reserved12 B-1 Chapter 12 (continued) Tables and Priority Queues.
The Heap ADT In this section of notes you will learn about a new abstract data type, the heap, as well how heaps can be used.
DictionaryADT and Trees. Overview What is the DictionaryADT? What are trees? Implementing DictionaryADT with binary trees Balanced trees DictionaryADT.
Stacks. What is a stack? Last-in first-out data structure (LIFO) New objects are placed on top Removal restricted to top object Examples?
CS 315 March 24 Goals: Heap (Chapter 6) priority queue definition of a heap Algorithms for Insert DeleteMin percolate-down Build-heap.
Priority Queues. Container of elements where each element has an associated key A key is an attribute that can identify rank or weight of an element Examples.
Linked structures. Overview Overview of linked structures (for lists) Design decision: arrays vs. linked lists Implementing the StackADT with linked lists.
OrderedListADT. Overview OrderedListADT: not the same as linked lists The OrderedListADT: operations and sample applications An ArrayList implementation.
Main Index Contents 11 Main Index Contents Model for a Queue Model for a Queue The Queue The Queue ADTQueue ADT (3 slides) Queue ADT Radix Sort Radix Sort.
A Heap Implementation Chapter Chapter Contents Reprise: The ADT Heap Using an Array to Represent a Heap Adding an Entry Removing the Root Creating.
Stacks.
Priority Queues. Priority queue A stack is first in, last out A queue is first in, first out A priority queue is least-first-out –The “smallest” element.
Priority Queues. Priority queue A stack is first in, last out A queue is first in, first out A priority queue is least-first-out –The “smallest” element.
© 2006 Pearson Addison-Wesley. All rights reserved12 A-1 Chapter 12 Heaps.
Priority Queues. Container of elements where each element has an associated key A key is an attribute that can identify rank or weight of an element Examples.
CHAPTER 6 Stacks. 2 A stack is a linear collection whose elements are added and removed from one end The last element to be put on the stack is the first.
Queues. From last time: Java's built-in java.util.Stack class Consider the javadoc for Java’s built-in java.util.Stack class (
Heaps and heapsort COMP171 Fall 2005 Part 2. Sorting III / Slide 2 Heap: array implementation Is it a good idea to store arbitrary.
CSE 373 Data Structures and Algorithms Lecture 13: Priority Queues (Heaps)
Priority Queues, Heaps & Leftist Trees
CSC 172 DATA STRUCTURES. Priority Queues Model Set with priorities associatedwith elements Priorities are comparable by a < operator Operations Insert.
1 HEAPS & PRIORITY QUEUES Array and Tree implementations.
Queues and Priority Queues
9/17/20151 Chapter 12 - Heaps. 9/17/20152 Introduction ► Heaps are largely about priority queues. ► They are an alternative data structure to implementing.
Chapter 21 Binary Heap.
data ordered along paths from root to leaf
1 Heaps and Priority Queues Starring: Min Heap Co-Starring: Max Heap.
Data Structures Using C++ 2E Chapter 9 Searching and Hashing Algorithms.
1 Heaps (Priority Queues) You are given a set of items A[1..N] We want to find only the smallest or largest (highest priority) item quickly. Examples:
COSC2007 Data Structures II Chapter 12 Tables & Priority Queues III.
1 Heaps and Priority Queues v2 Starring: Min Heap Co-Starring: Max Heap.
CHAPTER 5 PRIORITY QUEUES (HEAPS) §1 ADT Model Objects: A finite ordered list with zero or more elements. Operations:  PriorityQueue Initialize( int.
Programming Abstractions Cynthia Lee CS106X. Topics:  Priority Queue › Linked List implementation › Heap data structure implementation  TODAY’S TOPICS.
8 January Heap Sort CSE 2011 Winter Heap Sort Consider a priority queue with n items implemented by means of a heap  the space used is.
Chapter 13 Priority Queues. 2 Priority queue A stack is first in, last out A queue is first in, first out A priority queue is least-in-first-out The “smallest”
Heaps Chapter 17 Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2013.
CSE 373: Data Structures and Algorithms Lecture 2: Queues.
HEAPS. Review: what are the requirements of the abstract data type: priority queue? Quick removal of item with highest priority (highest or lowest key.
Java Methods A & AB Object-Oriented Programming and Data Structures Maria Litvin ● Gary Litvin Copyright © 2006 by Maria Litvin, Gary Litvin, and Skylight.
CS 367 Introduction to Data Structures Lecture 8.
Priority Queues CS 110: Data Structures and Algorithms First Semester,
1 Chapter 6 Heapsort. 2 About this lecture Introduce Heap – Shape Property and Heap Property – Heap Operations Heapsort: Use Heap to Sort Fixing heap.
Week 13 - Wednesday.  What did we talk about last time?  NP-completeness.
Priority Queues CS /02/05 L7: PQs Slide 2 Copyright 2005, by the authors of these slides, and Ateneo de Manila University. All rights reserved.
Course: Programming II - Abstract Data Types HeapsSlide Number 1 The ADT Heap So far we have seen the following sorting types : 1) Linked List sort by.
Tables and Priority Queues
Chapter 11 Heap.
Priority Queues and Heaps
Priority Queues Chuan-Ming Liu
Programming Abstractions
Bohyung Han CSE, POSTECH
Chapter 13 Queues and Priority Queues
CMSC 341 Lecture 5 Stacks, Queues
Draw pictures to indicate the subproblems middleMax solves at each level and the resulting maxPtr and PrevPtr for each on this linked list:
CSE 373: Data Structures and Algorithms
Programming Abstractions
Dr.Surasak Mungsing CSE 221/ICT221 Analysis and Design of Algorithms Lecture 05-2: Analysis of time Complexity of Priority.
Heaps © 2014 Goodrich, Tamassia, Goldwasser Heaps Heaps
CSE 12 – Basic Data Structures
Priority Queues CSE 373 Data Structures.
CSE 373, Copyright S. Tanimoto, 2002 Priority Queues -
Chapter 12 Heap ADT © 2011 Pearson Addison-Wesley. All rights reserved.
Presentation transcript:

Priority Queues and Heaps

Overview Our last ADT: PriorityQueueADT A new data structure: heaps One more sorting algorithm: heapsort Priority Queues and Heaps 2/17

Problems we’d like to solve Patients arrive in an emergency room. Each has a priority and they are treated in priority order. If two items have the same priority, they are served in a first- come-first-served order. Items are added to a todo list. They are generally done in the order they were added to the list, but it’s possible to add new items with a high priority. Priority Queues and Heaps p. 3/17

PriorityQueueADT // Note: this is not in the book. public interface PriorityQueueADT { public void insert (E element); public E deleteMin(); public E peek(); public int size(); public boolean isEmpty(); public String toString(); } Priority Queues and Heaps p. 4/17

Implementing PriorityQueueADT with a heap What is a heap? What is the “heap condition”? Priority Queues and Heaps p. 5/17

Implementing PriorityQueueADT with a heap What is a heap? What is the “heap condition”? Draw two trees with integer labels on the nodes, one that is a heap and one that isn’t. Explain. Priority Queues and Heaps p. 6/17

Implementing PriorityQueueADT with a heap How could we use a heap to implement a priority queue? Priority Queues and Heaps p. 7/17

Implementing a heap: diagrams Take the heaps on the top of page 338 (Figure 11.6) and draw a picture showing how their values could be stored in an array. Priority Queues and Heaps p. 8/17

Implementing a heap: diagrams Consider the following array: Does the array correspond to a heap? Why or why not? If yes, draw the tree. Priority Queues and Heaps p. 9/17

Inserting into a heap Show step by step what happens when we insert the values 1, 8, 7, 2, 3, 6, 5, 4 into a heap (in that order) by drawing a series of diagrams. Priority Queues and Heaps p. 10/17

Inserting into a heap Show step by step what happens when we insert the values 1, 8, 7, 2, 3, 6, 5, 4 into a heap (in that order) by drawing a series of diagrams. Consider the code for an array implementation of a heap on p. 351 (Section 11.4). Show how it works on the above example, drawing the array corresponding to each diagram. What is the O( ) of inserting into a heap and why? Priority Queues and Heaps p. 11/17

deleteMin Show step by step what happens when we delete the maximum value from the heap created in the previous example. Consider the code p. 352 (Section 11.4). Show how it works with your sample heap, drawing the corresponding arrays step by step. deleteMin is also O(log 2 n) – why? Explain. Priority Queues and Heaps p. 12/17

Return to sorting: heapsort How could you use a heap to sort a list of numbers? What would the time complexity of the sort be? Priority Queues and Heaps p. 13/17

Review questions: can you … Given a drawing of a heap and a new value, show the tree diagrams that would result as that value is being added to the heap? Show the tree diagrams that would result as the max value is being removed from the heap? Show how it would be represented as an array? Priority Queues and Heaps p. 14/17

Review questions (2) Given an array of numbers, can you: Determine whether it is a heap? If it is a heap, draw the corresponding tree? Priority Queues and Heaps p. 15/17

Review questions (3) Can you: Describe how a heap could be used to sort a list of numbers? Give the pros and cons of implementing heaps using arrays vs. linked lists? Explain how a priority queue could be used to solve two other problems besides sorting? Priority Queues and Heaps p. 16/17

Coming Attractions Congratulations – we made it! This is the last topic to be covered in this class. Homework: review the slides, the book, and the midterm, and bring any questions you may have to the next class. Priority Queues and Heaps p. 17/17