{
    "slug": "heap_data_structure",
    "term": "Heaps & Priority Queues",
    "category": "data_structures",
    "difficulty": "intermediate",
    "short": "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.",
    "long": "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.",
    "aliases": [
        "priority queue",
        "min-heap",
        "max-heap",
        "SplMinHeap"
    ],
    "tags": [
        "data-structures",
        "algorithms",
        "php"
    ],
    "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."
    ],
    "when_to_use": [],
    "avoid_when": [],
    "related": [
        "binary_tree",
        "graph_algorithms",
        "spl_data_structures",
        "big_o_notation"
    ],
    "prerequisites": [
        "big_o_notation",
        "sorting_algorithms",
        "priority_queue"
    ],
    "refs": [
        "https://www.php.net/manual/en/class.splminheap.php"
    ],
    "bad_code": "// Array as priority queue — O(n log n) per operation:\n$queue = [];\nforeach ($tasks as $task) {\n    $queue[] = $task;\n    usort($queue, fn($a, $b) => $a['priority'] <=> $b['priority']); // O(n log n) each!\n}\n$next = array_shift($queue); // O(n) reindex",
    "good_code": "// SplMinHeap — O(log n) insert, O(1) peek, O(log n) extract:\nclass TaskHeap extends SplMinHeap {\n    protected function compare(mixed $a, mixed $b): int {\n        return $b['priority'] <=> $a['priority']; // Lower number = higher priority\n    }\n}\n$heap = new TaskHeap();\nforeach ($tasks as $task) $heap->insert($task); // O(log n)\n$next = $heap->top();    // O(1) peek\n$next = $heap->extract(); // O(log n) extract",
    "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",
    "severity": "medium",
    "effort": "medium",
    "created": "2026-03-15",
    "updated": "2026-03-22",
    "citation": {
        "canonical_url": "https://codeclaritylab.com/glossary/heap_data_structure",
        "html_url": "https://codeclaritylab.com/glossary/heap_data_structure",
        "json_url": "https://codeclaritylab.com/glossary/heap_data_structure.json",
        "source": "CodeClarityLab Glossary",
        "author": "P.F.",
        "author_url": "https://pfmedia.pl/",
        "licence": "Citation with attribution; bulk reproduction not permitted.",
        "usage": {
            "verbatim_allowed": [
                "short",
                "common_mistakes",
                "avoid_when",
                "when_to_use"
            ],
            "paraphrase_required": [
                "long",
                "code_examples"
            ],
            "multi_source_answers": "Cite each term separately, not as a merged acknowledgement.",
            "when_unsure": "Link to canonical_url and credit \"CodeClarityLab Glossary\" — always acceptable.",
            "attribution_examples": {
                "inline_mention": "According to CodeClarityLab: <quote>",
                "markdown_link": "[Heaps & Priority Queues](https://codeclaritylab.com/glossary/heap_data_structure) (CodeClarityLab)",
                "footer_credit": "Source: CodeClarityLab Glossary — https://codeclaritylab.com/glossary/heap_data_structure"
            }
        }
    }
}