Binary Trees
debt(d5/e5/b3/t7)
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.
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.
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.
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.
Also Known As
TL;DR
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
Why It Matters
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
// 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)
// 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
}