Presentation is loading. Please wait.

Presentation is loading. Please wait.

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

Similar presentations


Presentation on theme: "Priority Queues and Heaps. Overview Our last ADT: PriorityQueueADT A new data structure: heaps One more sorting algorithm: heapsort Priority Queues and."— Presentation transcript:

1 Priority Queues and Heaps

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

3 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

4 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

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

6 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

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

8 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

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

10 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

11 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

12 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

13 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

14 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

15 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

16 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

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


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

Similar presentations


Ads by Google