What is a heap, and what does the heap-order property say?
A heap is a binary tree that stores keys in its nodes such that for every node v other than the root, key(v) ≥ key(parent(v)). As a consequence, the smallest key is always at the root.
* Every child is at least as large as its parent, so the smallest key ends up at the root. *
The heap-order property is a rule about every parent–child pair: a child is never smaller than its parent. Follow any path from the root downwards and the keys never decrease.
That one local rule has a global consequence. The root is an ancestor of every other node, and keys only grow (or stay equal) on the way down, so no node can hold a smaller key than the root. The minimum is therefore always at the top, which is exactly what a priority queue needs.
For example, a heap with root 2, whose children are 5 and 6, where 5 has the children 9 and 7, satisfies the property: 5 ≥ 2, 6 ≥ 2, 9 ≥ 5, 7 ≥ 5.
Because the rule is ≥, not >, duplicate keys are allowed: inserting key 5 twice gives two separate nodes with key 5, and neither overwrites the other.
The heap-order property is only half of the definition. A heap must also have a particular shape (complete binary tree, filled from the left), which is what keeps it short.
Tip: this is a min-heap. Reverse the inequality (child ≤ parent) and you get a max-heap with the largest key at the root; everything else works the same way.
Gotcha: the word heap is overloaded. The heap data structure here has nothing to do with the heap memory area of a program, where objects created with new are allocated.
Go deeper:
Heap (data structure) — min- and max-heaps, the heap property and the heap family beyond the binary heap.