# 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
This is the end of the preview. Sign up to access the rest of the document.

## This note was uploaded on 04/26/2010 for the course CMPT 225 taught by Professor Annelavergne during the Spring '07 term at Simon Fraser.

### Page1 / 25

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

This preview shows document pages 1 - 9. Sign up to view the full document.

View Full Document
Ask a homework question - tutors are online