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

Binary Trees

Data Structures PHP 5.0+ Intermediate
debt(d5/e5/b3/t7)
d5 Detectability Operational debt — how invisible misuse is to your safety net

Closest to 'specialist tool catches it' (d5). The detection_hints specify xdebug as the tool, which is a runtime profiler/debugger — not an automated linter. Stack overflow from deep recursion requires runtime execution or profiling to detect; static analysis won't catch degenerate BSTs or suboptimal tree structures. Common mistakes like naive BST degeneracy are invisible to syntax checkers.

e5 Effort Remediation debt — work required to fix once spotted

Closest to 'touches multiple files / significant refactor in one component' (e5). The quick_fix suggests replacing recursion with an explicit stack array, which is a localized refactor within the tree traversal logic. However, if a codebase has tree traversal scattered across multiple methods or classes, or if the tree structure itself needs rebalancing (AVL/Red-Black conversion), the fix becomes more costly and crosses component boundaries. This lands at e5 rather than e3 because it often requires testing multiple traversal sites.

b3 Burden Structural debt — long-term weight of choosing wrong

Closest to 'localized tax' (b3). Binary tree choice applies primarily to components that need ordered lookups or priority queues — typically confined to a single module or data-access layer. The term's applies_to scope is broad (web/cli contexts), but the actual architectural weight depends on whether the tree is a local data structure or shared across the system. In most PHP applications, tree usage is localized to specific algorithms rather than system-defining, so the burden is moderate but not pervasive.

t7 Trap Cognitive debt — how counter-intuitive correct behaviour is

Closest to 'serious trap' (t7). The misconception field explicitly states that developers confuse naive BST with balanced BST performance ('A binary search tree always gives O(log n) search — only balanced BSTs guarantee O(log n)') and conflate BSTs with heaps. The common_mistakes also note confusion between BSTs and B-Trees (different node arity), contradicting how similar tree concepts work elsewhere. The 'obvious' assumption that BST = log(n) performance is demonstrably wrong when data is sorted, making this a serious learning trap.

About DEBT scoring →

Also Known As

BST binary search tree AVL tree Red-Black tree

TL;DR

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.

Explanation

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.

Diagram

flowchart TD
    ROOT[8 root] --> L[3] & R[10]
    L --> LL[1] & LR[6]
    LR --> LRL[4] & LRR[7]
    R --> RR[14]
    RR --> RRL[13]
    INFO[BST: left < parent < right<br/>In-order: 1 3 4 6 7 8 10 13 14]
style ROOT fill:#6e40c9,color:#fff
style L fill:#1f6feb,color:#fff
style R fill:#238636,color:#fff

Common 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.

Code Examples

✗ Vulnerable
// Naive BST with sorted input — degenerates to linked list:
$bst = new BST();
foreach (range(1, 10000) as $n) {
    $bst->insert($n); // Each node has only right child
}
$bst->search(9999); // O(n) — 9999 comparisons, not O(log n)
✓ Fixed
// PHP SplMinHeap — O(log n) insert and extract-min:
$heap = new SplMinHeap();
foreach ([5, 2, 8, 1, 9, 3] as $n) {
    $heap->insert($n); // O(log n)
}
while (!$heap->isEmpty()) {
    echo $heap->extract() . ' '; // 1 2 3 5 8 9 — sorted, O(n log n) total
}

Added 15 Mar 2026
Edited 22 Mar 2026
Views 61
Rate this term
No ratings yet
🤖 AI Guestbook educational data only
| |
Last 30 days
0 pings T 1 ping W 1 ping T 0 pings F 0 pings S 0 pings S 0 pings M 0 pings T 0 pings W 2 pings T 1 ping F 1 ping S 6 pings S 2 pings M 0 pings T 1 ping W 0 pings T 0 pings F 0 pings S 1 ping S 0 pings M 0 pings T 1 ping W 0 pings T 0 pings F 0 pings S 0 pings S 1 ping M 0 pings T 0 pings W
No pings yet today
No pings yesterday
Scrapy 12 Amazonbot 9 Google 6 Perplexity 5 Ahrefs 4 Unknown AI 3 Claude 2 Bing 2 ChatGPT 2 Meta AI 1 PetalBot 1
crawler 42 crawler_json 4 pre-tracking 1
DEV INTEL Tools & Severity
🟢 Low ⚙ Fix effort: Medium
⚡ Quick Fix
When implementing tree traversal in PHP, use an explicit stack array instead of recursion to avoid stack overflow on deep trees
📦 Applies To
PHP 5.0+ web cli
🔗 Prerequisites
🔍 Detection Hints
Recursive tree traversal without depth limit on user-controlled tree depth; PHP stack overflow on deep recursion
Auto-detectable: ✗ No xdebug
⚠ Related Problems
🤖 AI Agent
Confidence: Low False Positives: High ✗ Manual fix Fix: Medium Context: Function Tests: Update


✓ schema.org compliant