Skip to main content
← Advanced CS Study Guide

Code Scholars Tutoring · Advanced CS

7.1 The Heap Property

How it works

Every parent compares correctly against its own children. Siblings are unordered and the structure is not sorted, which is why maintaining it is cheap.

Use a heap when only the extreme element is needed repeatedly.

Common error

Expecting sorted order from a heap traversal. Only the root is guaranteed.

Worked example

Example

Min-heap array: [2, 5, 3, 9, 7]
Parent <= each child
Minimum: root
Siblings are not required to be sorted

A binary heap combines a complete-tree shape with a parent-child priority rule. A min-heap exposes the minimum at its root but does not sort every array position.

Root 2 has children 5 and 3; node 5 has children 9 and 7.
Every parent is at most its children; sibling values need not be sorted.

Exercises

7.1.1 · Explain

Is [1, 4, 3, 9, 8] a valid min-heap in zero-based array order?

Show solution

Yes. The root 1 is at most 4 and 3, and parent 4 is at most 9 and 8. No sibling ordering is required.

7.1.2 · Explain

Why cannot a min-heap generally find an arbitrary target in logarithmic time by a BST-style search?

Show solution

Its ordering is only between a parent and its children, not across entire left and right subtrees. An arbitrary target may require scanning many nodes.

Open Java compiler

Need help with this topic?

Work through questions with a tutor and get help with the steps you find difficult.

Get help with this topic