← CodeClarityLab Home
Browse by Category
+ added · updated 7d
← Back to glossary

Heaps & Priority Queues

data_structures PHP 5.3+ Intermediate

Also Known As

priority queue min-heap max-heap SplMinHeap

TL;DR

A tree-based structure satisfying the heap property — min-heap: parent ≤ children; max-heap: parent ≥ children — enabling O(1) peek and O(log n) insert/extract for priority queues.

Explanation

A binary heap is a complete binary tree stored in an array. For node at index i: left child at 2i+1, right at 2i+2, parent at (i-1)/2. Heapify maintains the heap property in O(log n). Building a heap from n elements is O(n) — not O(n log n). Priority queue applications: task scheduling, Dijkstra's algorithm, Huffman coding, event simulation. PHP's SplMinHeap and SplMaxHeap implement min and max heaps respectively.

Diagram

flowchart TD
    subgraph Min_Heap
        ROOT2[1 - root always minimum]
        ROOT2 --> L3[3]
        ROOT2 --> R3[2]
        L3 --> LL2[7]
        L3 --> LR2[4]
        R3 --> RL2[5]
    end
    INSERT[insert 2] -->|add at end then bubble up| ROOT2
    EXTRACT[extract min] -->|remove root move last to top sink down| ROOT2
    subgraph Complexity
        INS_C[insert: O of log n]
        EXT_C[extract min: O of log n]
        PEEK[peek min: O of 1]
    end
style ROOT2 fill:#238636,color:#fff
style INS_C fill:#1f6feb,color:#fff

Common Misconception

A sorted array is a good priority queue — sorted array insertion is O(n); heap insertion is O(log n) and peek is O(1).

Why It Matters

Priority queues are used in job schedulers, event loops, and pathfinding — using a sorted array instead of a heap turns O(n log n) algorithms into O(n²).

Common Mistakes

  • Using array + sort() as a priority queue — O(n log n) per insertion vs O(log n) for a heap.
  • Not understanding that a heap is not fully sorted — only the root is guaranteed to be the min/max.
  • Using SplMaxHeap when SplMinHeap is needed — SplMaxHeap returns the largest element, not the smallest.
  • Reinventing a priority queue with array sorting in PHP when SplMinHeap exists.

Code Examples

✗ Vulnerable
// Array as priority queue — O(n log n) per operation:
$queue = [];
foreach ($tasks as $task) {
    $queue[] = $task;
    usort($queue, fn($a, $b) => $a['priority'] <=> $b['priority']); // O(n log n) each!
}
$next = array_shift($queue); // O(n) reindex
✓ Fixed
// SplMinHeap — O(log n) insert, O(1) peek, O(log n) extract:
class TaskHeap extends SplMinHeap {
    protected function compare(mixed $a, mixed $b): int {
        return $b['priority'] <=> $a['priority']; // Lower number = higher priority
    }
}
$heap = new TaskHeap();
foreach ($tasks as $task) $heap->insert($task); // O(log n)
$next = $heap->top();    // O(1) peek
$next = $heap->extract(); // O(log n) extract

Added 15 Mar 2026
Edited 22 Mar 2026
Views 26
Rate this term
No ratings yet
🤖 AI Guestbook educational data only
| |
Last 30 days
0 pings W 0 pings T 1 ping F 0 pings S 1 ping S 0 pings M 0 pings T 0 pings W 1 ping T 1 ping F 0 pings S 0 pings S 1 ping M 0 pings T 1 ping W 0 pings T 1 ping F 0 pings S 0 pings S 0 pings M 0 pings T 0 pings W 0 pings T 2 pings F 0 pings S 0 pings S 0 pings M 0 pings T 0 pings W 0 pings T
No pings yet today
No pings yesterday
Amazonbot 6 Perplexity 6 Unknown AI 3 Google 2 Ahrefs 2 SEMrush 2 Majestic 1
crawler 20 crawler_json 1 pre-tracking 1
DEV INTEL Tools & Severity
🟡 Medium ⚙ Fix effort: Medium
⚡ Quick Fix
Use PHP's SplMinHeap or SplMaxHeap for priority queues — they maintain heap order automatically with O(log n) insert/extract, much better than sorting an array each time
📦 Applies To
PHP 5.3+ web cli
🔗 Prerequisites
🔍 Detection Hints
Sorting array to get min/max element repeatedly in O(n log n) when heap would do it O(log n); priority queue implemented as sorted array
Auto-detectable: ✗ No blackfire
⚠ Related Problems
🤖 AI Agent
Confidence: Low False Positives: Medium ✗ Manual fix Fix: Medium Context: Class Tests: Update

✓ schema.org compliant