1. Gyanastra
  2. Data Structures
  3. Heaps and Priority Queues
  4. Priority Queue and the Heap Property
Free lesson, no account needed 8 min read 13 practice questions after it
Asked in past papers10 times
  • GATE CSE20232016201120092006
  • ISRO20232008
  • UGC NET201620132010

Data Structures · Heaps and Priority Queues

Priority Queue and the Heap Property

Overview

A priority queue is a collection from which you always want the most important element next: the smallest key, or the largest key. A binary heap is the standard way to build one.
It is a complete binary tree in which every node obeys one ordering rule, the heap property. This lesson fixes the vocabulary; the next lessons turn it into index arithmetic, insertion, deletion, building a heap and heap sort.

Key concepts

  • Priority queue (ADT). A set of items, each with a key. Operations: insert(x), find-max (or find-min), extract-max (or extract-min, which finds and removes it). Optional: increase-key/decrease-key, delete.
  • Complete binary tree. Every level is full except possibly the last, and the last level is filled from the left with no gaps. Because of this shape, a heap with nn nodes is stored in an array with no pointers.
  • Max-heap property. For every node other than the root, key(parent)≥key(node)key(\text{parent}) \ge key(\text{node}). The largest key is therefore at the root.
  • Min-heap property. For every node other than the root, key(parent)≤key(node)key(\text{parent}) \le key(\text{node}). The smallest key is at the root.
  • The property relates a node only to its parent and children. It says nothing about two siblings, or about a node and its cousin. A heap is not sorted and is not a binary search tree.
  • Duplicate keys are allowed; the property uses ≥\ge or ≤\le, not strict inequality.

How it works

Two conditions must hold together for an array to be a heap:

  1. Shape: the tree drawn from the array in level order is complete (this is automatic: any array of nn items fills positions 1..n1..n left to right).
  2. Order: at every node the heap property holds with respect to its children.

To check whether a given array is a max-heap, test every internal node: is it ≥\ge its left child and (if present) its right child? You do not compare anything else.

Is  A = [50, 30, 40, 10, 20, 35, 25]  a max-heap?   (1-indexed)
   index:   1   2   3   4   5   6   7
   tree:           50
                 /    \
               30      40
              /  \    /  \
            10   20  35   25
 50>=30,40 ok;  30>=10,20 ok;  40>=35,25 ok   -> yes, a max-heap.

Formulas

  • Max-heap: A[parent(i)]≥A[i]A[\text{parent}(i)] \ge A[i] for all 2≤i≤n2 \le i \le n.
  • Min-heap: A[parent(i)]≤A[i]A[\text{parent}(i)] \le A[i] for all 2≤i≤n2 \le i \le n.
  • Priority queue operation costs on a binary heap with nn items: find-max O(1)O(1), insert O(log⁡n)O(\log n), extract-max O(log⁡n)O(\log n).

Worked examples

Example 1: is it a min-heap? A=[3,8,5,10,9,6,7]A = [3, 8, 5, 10, 9, 6, 7].

  • 3≤8,53 \le 8, 5 ✓. 8≤10,98 \le 10, 9 ✓. 5≤6,75 \le 6, 7 ✓. So it is a min-heap. Note 8>58 > 5 (siblings) and 9>69 > 6: that is allowed, the property never compares siblings.

Example 2: find the violation. A=[40,25,30,10,28,12,7]A = [40, 25, 30, 10, 28, 12, 7] as a max-heap.

  • Node 25 (index 2) has children 10 and 28. 25<2825 < 28, so the property fails at index 2. The array is not a max-heap, even though the root is the maximum.

Example 3: heap is not a BST. In the max-heap [50,30,40,10,20,35,25][50, 30, 40, 10, 20, 35, 25] the left child of the root is 30 and the right child is 40, but the right subtree also contains 35 and 25, which are smaller than 30. A BST would forbid that. So you cannot binary-search a heap.

Example 4: priority queue as a heap. Jobs with priorities 4, 9, 2 are inserted into a max-heap. extract-max returns 9 first, then 4, then 2: the order of priority, not the order of arrival.

Comparison

Structurefind-maxinsertextract-maxsorted ordersearch a key
Unsorted arrayO(n)O(n)O(1)O(1)O(n)O(n)noO(n)O(n)
Sorted arrayO(1)O(1)O(n)O(n)O(1)O(1) (remove from the end)yesO(log⁡n)O(\log n)
Binary max-heapO(1)O(1)O(log⁡n)O(\log n)O(log⁡n)O(\log n)noO(n)O(n)
Balanced BSTO(log⁡n)O(\log n)O(log⁡n)O(\log n)O(log⁡n)O(\log n)yes (in-order)O(log⁡n)O(\log n)

A heap is the best structure when you only ever need the extreme element repeatedly.

Revision summary

  • Heap = complete binary tree + heap property. Either one alone is not enough.
  • The property is parent-versus-child only. Siblings and cousins are unordered.
  • The root of a max-heap is the maximum; the root of a min-heap is the minimum.
    The minimum of a max-heap is somewhere among the leaves.
  • A heap does not support fast search, fast successor, or sorted traversal.
  • A "max-priority queue" and a "min-priority queue" differ only in the direction of the comparison.

Previous-year exam insights

  • A frequent question: "Which array is a max-heap?" Check every internal node against both children; one failing node rules the array out.
  • Typical true/false traps: "a heap is a BST" (false); "the smallest element of a max-heap is a leaf" (true); "the second largest element of a max-heap is a child of the root" (true).
  • A heap with nn nodes is a complete binary tree, so its height is ⌊log⁡2n⌋\lfloor \log_2 n \rfloor (edges), which is why insertion and deletion are O(log⁡n)O(\log n).
Pages 1–2 of 2