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

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

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

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

View Full Document Right 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!)
Image of page 2
6 6 5 5 4 4 Using a Priority Queue 3 3 2 2 1 1
Image of page 3

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

View Full Document Right 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
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
Image of page 5

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

View Full Document Right Arrow Icon
Complete Binary Trees complete binary trees incomplete binary trees
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
Image of page 7

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

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

{[ snackBarMessage ]}

What students are saying

  • Left Quote Icon

    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.

    Student Picture

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

  • Left Quote Icon

    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.

    Student Picture

    Dana University of Pennsylvania ‘17, Course Hero Intern

  • Left Quote Icon

    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.

    Student Picture

    Jill Tulane University ‘16, Course Hero Intern