Link

Priority Queues and Heaps Study Guide

Priority Queue. A Max Priority Queue (or PQ for short) is an ADT that supports at least the insert and delete-max operations. A MinPQ supports insert and delete-min.

Heaps. A max (or min) heap is an array representation of a binary tree such that every node is larger (or smaller) than all of its children. This definition naturally applies recursively, i.e. a heap of height 5 is composed of two heaps of height 4 plus a parent.

Tree Representations. Know that there are many ways to represent a tree. Because heaps are complete trees, we prefer to use represent them with arrays. This leads to better real-world performance than using nodes. (More on this later.)

Recommend Problems

Note
The reason there are many problems here is not because this is a more important topic but because there are just so many interesting problems.
  1. Is an array that is sorted in descending order also a max-oriented heap?
  2. Why do we leave the zeroth index empty in our array representation for heaps?
  3. [Textbook 2.4.7] The largest item in a heap must appear in position 1, and the second largest must appear in position 2 or 3. Give the list of positions in a heap of size 31 where the kth largest can appear, and where the kth largest cannot appear for k = 2, k = 3, k = 4. Assume values are distinct.
  4. [Textbook 2.4.21] Explain how to use a priority queue to implement the stack and queue data types.
  5. [Adapted from Textbook 2.4.27] Add a min() method to a maximum-oriented PQ. Your implementation should use constant time and constant extra space.
  6. [Textbook 2.4.14] What is the minimum number of items that must be exchanged during a remove-the-max operationin a heap of size N with no duplicate keys? Give a heap of size 15 for which the minimum is achieved. Answer the same qusetion for two and three successive remove-the-maximum operations.
  7. Design a data type that supports insert in O(log N) time, find-the-median in O(1) time, and delete-the-median in O(log N) time.
  8. Design a data type that supports insert in O(log N) time, delete-the-max in O(log N) time, and delete-the-minimum in O(log N) time.

  9. Q1a from CS 61B 15sp MT2
  10. Q8a, Q8c from CS 61B 16sp MT2
  11. Q6 from CS 61B 18sp MT2
  12. Q2 from CS 61BL 19su MT2