Binary Heap & Priority Queue

A binary heap stores a complete binary tree compactly in a flat 0-indexed array with zero pointers, supporting O(1) min/max peek, O(log n) push/extract, and O(n) bottom-up Floyd heapify.

Technical Reference & Equations

  • 0-Indexed Array Navigation: \mathrm{parent}(i) = \left\lfloor \frac{i - 1}{2} \right\rfloor, \quad \mathrm{left}(i) = 2i + 1, \quad \mathrm{right}(i) = 2i + 2 (Eliminates pointer overhead while preserving spatial cache locality.)
  • Heap Order Invariant: A[\mathrm{parent}(i)] \le A[i] \;(\text{Min-Heap}), \quad A[\mathrm{parent}(i)] \ge A[i] \;(\text{Max-Heap}) (Restored via Sift-Up on insertion and Sift-Down after extracting index 0.)
  • Floyd Bottom-Up Build-Heap Complexity: T_{\text{build}}(n) = \sum_{h=0}^{\lfloor \log_2 n \rfloor} \left\lceil \frac{n}{2^{h+1}} \right\rceil O(h) = O(n) (Most nodes sit near the leaves and sift down at most 0 or 1 levels.)

View full Data Structures & Algorithms Formula Sheet | Constants Table

Related Data Structures Calculators