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

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

View Full Document Right Arrow Icon
CMPT 225 Priority Queues and Heaps
Background image of page 1

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

View Full DocumentRight Arrow Icon
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!)
Background image of page 2
6 6 5 5 4 4 Using a Priority Queue 3 3 2 2 1 1
Background image of page 3

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

View Full DocumentRight Arrow Icon
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
Background image of page 4
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
Background image of page 5

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

View Full DocumentRight Arrow Icon
Complete Binary Trees complete binary trees incomplete binary trees
Background image of page 6
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
Background image of page 7

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

View Full DocumentRight Arrow Icon
Priority Queues and Heaps A heap can be used to implement a priority queue
Background image of page 8
Image of page 9
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 Right Arrow Icon
Ask a homework question - tutors are online