Heapsort

What Is a Heap Data Structure?

A heap is a tree-based data structure where parents are ordered against their children. Learn max-heaps, min-heaps, array storage, and priority-queue uses.

On this page
  1. Short answer
  2. The heap property
  3. How a binary heap is stored in an array
  4. Max-heaps and min-heaps
  5. Basic operations
  6. What heaps are used for
  7. A heap is not a search tree
  8. Heap memory is a different idea
  9. Heap and abstract data type

Short answer

A heap is a tree-based data structure in which each parent is ordered relative to its children. In a max-heap, every parent is greater than or equal to its children. In a min-heap, every parent is less than or equal to its children. Heaps are usually binary heaps, which are commonly stored in arrays, and they are mainly used to get the largest or smallest item quickly.

The heap property

The ordering rule applies only between a parent and its children. Siblings have no required order, and nothing is guaranteed about values on different branches. Here is a small max-heap:

        9
      /   \
     7     8
    / \
   2   6

Every parent here is at least as large as its children. The 8 is larger than the 7 beside it, which the heap property allows, because only parent-child pairs are ordered.

How a binary heap is stored in an array

A binary heap is a complete binary tree: every level is full except possibly the last, which fills from left to right. That shape means the heap can be stored in an array without pointers. With zero-based indexing, the children of the item at index i sit at 2i + 1 and 2i + 2, and its parent sits at floor((i - 1) / 2).

The max-heap above is stored as [9, 7, 8, 2, 6]. The 7 is at index 1, so its children are at indexes 3 and 4, which hold 2 and 6.

Max-heaps and min-heaps

In a max-heap, the root holds the largest value. In a min-heap, the root holds the smallest. The structure is the same, with the comparison reversed. Libraries differ in which kind they provide:

  • Python’s heapq module works on a plain list and gives you a min-heap. A common way to get a max-heap from it is to store negated values.
  • Java’s PriorityQueue is built on a binary heap and returns the smallest item first by default.

Basic operations

  • Peek: read the root without removing it. This takes constant time.
  • Insert: place the new value in the next free position, then sift it up by swapping it with its parent while it breaks the heap order.
  • Remove the root: move the last value into the root, then sift it down by swapping it with its larger child (in a max-heap) until the heap order holds again.

The costs of these operations are the answer to what is the time complexity of a heap. In short, peek is constant time, while insert and removal take logarithmic time, because each step follows a single path from the bottom of the tree to the top or from the top to the bottom.

What heaps are used for

Heaps are most often used to build priority queues, where the next item to handle is the one with the highest priority. Priority-based scheduling in operating systems uses the same idea to pick the next task, as in cpu scheduling algorithms that favor higher-priority work. Dijkstra’s algorithm, which is compared with another method in dijkstra algorithm vs bellman ford, uses a priority queue to pick the closest unvisited node. Heaps are also the basis of heapsort, which builds a max-heap and then repeatedly removes the root.

A heap is not a search tree

Because a heap only orders parents and children, finding an arbitrary value means checking items one by one, which takes O(n) time. A heap is a poor choice when you need fast lookups. For that, a balanced search tree such as the avl tree in data structure is a better fit.

Heap memory is a different idea

The word “heap” also names the region of memory that programs use for dynamic allocation at runtime. That is a separate idea from the heap data structure described here, and the two are easy to mix up when reading quickly. The difference between stack and heap memory is a separate topic with its own rules.

Heap and abstract data type

A priority queue is an abstract data type, meaning it describes which operations are allowed, not how they are carried out. A heap is one common way to implement it. For more on that difference, see adt vs data structure.

The complete guide to heapsort

How the Heapsort Algorithm Works
Read the guide

Keep exploring

Guides from other topics

All topics →

Recursive Algorithm Examples

Recursive algorithm examples in Python: factorial, binary search, tree traversal, Tower of Hanoi, and merge sort, with base cases explained for each.

Recursion 7 min read