3. The Heap Data Structure

Now that we understand the concept of a binary tree, we can define a Heap. A Heap is a specialized tree-based data structure that satisfies two specific properties.

The Two Defining Properties of a Heap

For a binary tree to be considered a Heap, it must adhere to the following rules:

  1. Structure Property: It must be an Essentially Complete Binary Tree.
    This means that the tree is completely filled on all levels, with the possible exception of the last level, which must be filled from left to right without any gaps. This "no gaps" property is crucial, as it allows a heap to be stored efficiently in an array.

  2. Heap Property (Order Property): The nodes must be ordered in a specific way relative to their children. This ordering defines the type of heap.

Types of Heaps

There are two main types of heaps, distinguished by the Heap Property they enforce:

The Array Representation

The complete binary tree property is precisely what makes an array a perfect and highly efficient way to store a heap. The hierarchical relationships are not stored with pointers, but are instead calculated mathematically based on an element's index.

For an element at a zero-based index i in an array representing a heap:

For example, for the node at index i = 3:


Revision #1
Created 2025-09-24 08:22:18 UTC by GI
Updated 2025-09-24 08:22:32 UTC by GI