{
    "slug": "binary_tree",
    "term": "Binary Trees",
    "category": "data_structures",
    "difficulty": "intermediate",
    "short": "A hierarchical structure where each node has at most two children — Binary Search Trees enable O(log n) search, while balanced variants (AVL, Red-Black) guarantee it.",
    "long": "A binary tree has nodes with left and right children. Binary Search Tree (BST): left child < parent < right child — enables O(log n) search, insert, delete on balanced trees. Unbalanced BSTs degrade to O(n). Self-balancing trees (AVL, Red-Black) maintain O(log n) automatically. Traversals: in-order (left-root-right, gives sorted output on BST), pre-order (root-left-right), post-order (left-right-root). Heaps are specialised binary trees. PHP's SplMaxHeap/SplMinHeap are heap implementations.",
    "aliases": [
        "BST",
        "binary search tree",
        "AVL tree",
        "Red-Black tree"
    ],
    "tags": [
        "data-structures",
        "trees",
        "algorithms"
    ],
    "misconception": "A binary search tree always gives O(log n) search — only balanced BSTs guarantee O(log n); inserting sorted data into a naive BST creates a linked list with O(n) operations.",
    "why_it_matters": "Database indexes (B-Trees), PHP's SplHeap, and file system directories all use tree structures — understanding trees explains why index column order matters and how heap-based priority queues work.",
    "common_mistakes": [
        "Using a naive BST with sorted input — produces a degenerate linear structure; use a self-balancing tree.",
        "Recursive tree traversal without depth limit — deep trees cause stack overflow.",
        "Confusing BST (ordered) with heap (parent > children, not fully ordered) — different operations, different guarantees.",
        "Not understanding that B-Trees (database indexes) are not binary — B-Tree nodes can have many children."
    ],
    "when_to_use": [],
    "avoid_when": [],
    "related": [
        "heap_data_structure",
        "graph_data_structure",
        "big_o_notation",
        "database_indexing"
    ],
    "prerequisites": [
        "big_o_notation",
        "recursion_patterns",
        "b_tree_structure"
    ],
    "refs": [
        "https://en.wikipedia.org/wiki/Binary_search_tree"
    ],
    "bad_code": "// Naive BST with sorted input — degenerates to linked list:\n$bst = new BST();\nforeach (range(1, 10000) as $n) {\n    $bst->insert($n); // Each node has only right child\n}\n$bst->search(9999); // O(n) — 9999 comparisons, not O(log n)",
    "good_code": "// PHP SplMinHeap — O(log n) insert and extract-min:\n$heap = new SplMinHeap();\nforeach ([5, 2, 8, 1, 9, 3] as $n) {\n    $heap->insert($n); // O(log n)\n}\nwhile (!$heap->isEmpty()) {\n    echo $heap->extract() . ' '; // 1 2 3 5 8 9 — sorted, O(n log n) total\n}",
    "quick_fix": "When implementing tree traversal in PHP, use an explicit stack array instead of recursion to avoid stack overflow on deep trees",
    "severity": "low",
    "effort": "medium",
    "created": "2026-03-15",
    "updated": "2026-03-22",
    "citation": {
        "canonical_url": "https://codeclaritylab.com/glossary/binary_tree",
        "html_url": "https://codeclaritylab.com/glossary/binary_tree",
        "json_url": "https://codeclaritylab.com/glossary/binary_tree.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": "[Binary Trees](https://codeclaritylab.com/glossary/binary_tree) (CodeClarityLab)",
                "footer_credit": "Source: CodeClarityLab Glossary — https://codeclaritylab.com/glossary/binary_tree"
            }
        }
    }
}