# 11-3 - CMPT 225 Priority Queues and Heaps Priority Queues...

This preview shows pages 1–9. Sign up to view the full content.

CMPT 225 Priority Queues and Heaps

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
Priority Queues Items in a priority queue have a priority The priority is usually numerical value Could be lowest first or highest first The highest priority item is removed first Priority queue operations Insert Remove in priority queue order (not a FIFO!)
6 6 5 5 4 4 Using a Priority Queue 3 3 2 2 1 1

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
Implementing a Priority Queue Items have to be removed in priority order This can only be done efficiently if the items are ordered in some way A balanced binary search (e.g., red-black) tree is an efficient and ordered data structure but Some operations (e.g. removal) are complex to code Although operations are O(log n ) they require quite a lot of structural overhead There is another binary tree solution – heaps Note: We will see that search/removal of the maximum element is efficient, but it’s not true for other elements
Heaps A heap is binary tree with two properties Heaps are complete All levels, except the bottom, must be completely filled in The leaves on the bottom level are as far to the left as possible. Heaps are partially ordered ( “heap property” ): The value of a node is at least as large as its children’s values, for a max heap or The value of a node is no greater than its children’s values, for a min heap

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
Complete Binary Trees complete binary trees incomplete binary trees
Partially Ordered Tree – max heap 98 41 86 13 65 9 10 32 29 44 23 21 17 Note: an inorder traversal would result in: 9, 13, 10, 86, 44, 65, 23, 98, 21, 32, 17, 41, 29

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
Priority Queues and Heaps A heap can be used to implement a priority queue Because of the partial ordering property the
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}

### What students are saying

• As a current student on this bumpy collegiate pathway, I stumbled upon Course Hero, where I can find study resources for nearly all my courses, get online help from tutors 24/7, and even share my old projects, papers, and lecture notes with other students.

Kiran Temple University Fox School of Business ‘17, Course Hero Intern

• I cannot even describe how much Course Hero helped me this summer. It’s truly become something I can always rely on and help me. In the end, I was not only able to survive summer classes, but I was able to thrive thanks to Course Hero.

Dana University of Pennsylvania ‘17, Course Hero Intern

• The ability to access any university’s resources through Course Hero proved invaluable in my case. I was behind on Tulane coursework and actually used UCLA’s materials to help me move forward and get everything together on time.

Jill Tulane University ‘16, Course Hero Intern