With a binary heap, insert and delete-max are:
Heaps and Priority Queues
The priority queue API and applications, elementary implementations, complete binary trees, binary heaps in an array, swim (promotion) and sink (demotion), and the cost summary.
How to read this page
Every original slide is shown unchanged. Yellow marks on a slide point to a numbered note: hover or tap a mark to jump to its note, and hover a note to see its marks. Questions tagged Past exam appeared on a real paper; Prediction questions are likely angles, not confirmed. Pick an answer to each question: a wrong pick tells you why it is wrong, so you can try again.
All 21 slides
- 1 A l g o r i t h m s
- 2 Slide 2
- 3 Collections
- 4 Priority queue
- 5 Priority queue API
- 6 Priority queue applications
- 7 Priority queue elementary implementations
- 8 Slide 8
- 9 Complete binary tree
- 10 Slide 10
- 11 A complete binary tree in nature
- 12 Binary heap representations
- 13 Binary heap properties
- 14 Binary heap demo
- 15 Binary heap demo
- 16 Promotion in a heap
- 17 Insertion in a heap
- 18 Demotion in a heap
- 19 Delete the maximum in a heap
- 20 Binary heap: Java implementation
- 21 Priority queues implementation cost summary
Priority queue elementary implementations
Notes
Unordered array: insert O(1), delMax O(N). Ordered array: insert O(N), delMax O(1). Binary heap: both O(log N).
Exam angle
Binary heap properties
Notes
Heap-ordered: every parent ≥ its children (max-heap). Array: parent of k is k/2; children 2k, 2k + 1.
Exam angle
In a 1-based heap array, the parent of the node at index 9 is at:
Insertion in a heap
Notes
Insert: add at the end, swim up while larger than parent.
Delete the maximum in a heap
Notes
Delete max: swap root with the last item, remove it, sink the new root.