Computer Science I / Heaps and Priority Management
Practice question · Multiple choice

A binary heap is far weaker than a binary search tree: it only guarantees each parent beats its children, so it cannot search for an arbitrary value in better than O(n). Why is that weaker invariant an advantage rather than a defect?

Hints
  1. Ask what a priority queue is actually asked to do. Does it ever need to find an arbitrary value?
  2. A weaker promise is easier to keep. What does that buy you when the structure changes?
Show the answer

A. Because finding the maximum only needs the root.

Why

A priority queue is asked for the highest-priority item and nothing else, and the heap property puts it at the root by construction. Being weaker pays twice: restoring the invariant means sifting one node up a single path, and because left and right are unconstrained the tree stays complete and lives in a flat array. The best invariant is the weakest one that still works.

Read the lesson: Heaps and Priority Management →

Practise Heaps and Priority Management

The app has 6 more questions on this lesson, and keeps your place in the course. Computer Science I is free to start.

More questions on Heaps and Priority Management