Trees — Hierarchies, Traversals, and Balanced Search
A scroll-driven visual deep dive into tree data structures. Binary trees, BSTs, AVL trees, heaps, and B-trees — with real-world applications in databases, file systems, priority queues, and decision engines.
🌳
Hierarchies everywhere.
O(log n) guaranteed.
Trees model hierarchical relationships: file systems, HTML DOM, org charts, compiler parse trees, and database indexes. A balanced binary search tree gives O(log n) search, insert, and delete — even in the worst case. From AVL trees to B-trees to heaps, understanding trees unlocks the entire spectrum of efficient data organization.
↓ Scroll to see how trees organize the world’s data
Binary Trees: Nodes With At Most Two Children
Four tree traversal orders
Visit left subtree, then root, then right subtree. For BSTs, this produces elements in sorted order — making it the go-to traversal for ordered output.
Left → Root → Right: 2, 5, 7, 10, 12, 15, 20 Visit root first, then left and right subtrees. Useful for copying or serializing a tree — the root comes first so you can reconstruct the structure top-down.
Root → Left → Right: 10, 5, 2, 7, 15, 12, 20 Visit children first, then root (bottom-up). Ideal for deletion and expression evaluation — you process dependencies before the node that depends on them.
Left → Right → Root: 2, 7, 5, 12, 20, 15, 10 Visit every node at depth d before any node at depth d+1, using a queue. This breadth-first approach is essential for finding shortest paths and visualizing tree structure layer by layer.
BFS: 10, 5, 15, 2, 7, 12, 20 In a binary search tree with n nodes, what's the time complexity of finding the minimum element?
💡 Where is the smallest element in a BST?
In a BST, every node's left child is smaller and right child is larger. The smallest element is the leftmost node — found by following left pointers from root. In a balanced BST with height ≈ log₂(n), this takes O(log n). But if the tree is degenerate (all nodes have only left children), it becomes a linked list with O(n) search. This is why balancing matters.
Binary Search Trees: Order Enables Speed
BST search: binary search on a tree
BST invariant: left.val < node.val < right.val (for ALL nodes) Search: compare target with current node → go left or right → O(h) Insert: search for the key → when you hit null, insert there → O(h) Delete: 3 cases → (1) leaf: just remove, (2) one child: replace with child, (3) two children: replace with in-order successor You insert the keys [1, 2, 3, 4, 5] in order into an empty BST. What does the tree look like, and what's the search time?
💡 Think about what happens when every new key is larger than all existing keys...
Inserting sorted data into a BST creates a linked list: each new key is larger than all existing keys, so it always goes right. The tree becomes: 1→(right)2→(right)3→(right)4→(right)5. Height = n-1 = 4, so search, insert, delete are all O(n). This motivates self-balancing trees: AVL trees rotate on every insert/delete to guarantee height ≤ 1.44 × log₂(n). Red-Black trees use color-based rules for slightly looser balance but simpler rotations.
Self-Balancing Trees: Guaranteed O(log n)
You're building a real-time trading platform that processes 50,000 insert/delete operations per second with occasional lookups. Which self-balancing tree should you use?
💡 Think about which tree minimizes the cost per write operation, not per read...
Red-Black trees are optimized for write-heavy workloads: they guarantee at most 2 rotations per insert and 3 per delete, compared to AVL's potentially O(log n) rotations per delete. The tradeoff: Red-Black trees are slightly taller (up to 2× log₂(n) vs AVL's 1.44× log₂(n)), so lookups are marginally slower. For a trading platform doing 50K writes/sec, fewer rotations = lower latency per operation. B-trees would be the choice if data lived on disk, but for in-memory operations, Red-Black wins.
Heaps: The Priority Queue Engine
Binary heap operations
The fundamental invariant: every parent is smaller (min-heap) or larger (max-heap) than its children. This guarantees the global minimum or maximum is always at the root, accessible instantly.
parent ≤ children (min-heap) Add the new element at the end of the array (bottom of the tree), then repeatedly swap it with its parent until the heap property is restored. At most log n swaps needed.
O(log n) Remove the root (the min or max), replace it with the last leaf, then sift it down by swapping with the smaller (or larger) child until the heap property holds again.
O(log n) Simply return the root element — no modifications needed. The heap property guarantees the extreme value is always sitting at index 0.
O(1) Surprisingly efficient: build a heap from an unsorted array in linear time using bottom-up heapify. Nodes near the bottom (most of them) barely move, making the total work O(n), not O(n log n).
O(n) bottom-up heapify You need to find the k-th smallest element in a stream of 100 million numbers. What's the optimal approach?
💡 Can you solve this without storing all n elements?
A max-heap of size k maintains the k smallest elements seen so far. The root (max of the k smallest) acts as a gatekeeper: only elements smaller than it enter the heap. Each heap operation is O(log k), and we process n elements → O(n log k). For k=10 and n=100M: sorting needs ~2.6 billion comparisons, but the heap approach needs ~330M comparisons. Even better: it works on streams — you don't need all data in memory at once, just O(k) space.
Trees Power Everything
🎓 What You Now Know
✓ Binary trees model hierarchical data — file systems, DOM, org charts. Four traversal orders serve different purposes.
✓ BSTs give O(log n) search — if balanced. Sorted insertion creates O(n) degenerate trees. AVL and Red-Black trees guarantee balance.
✓ B-trees minimize disk I/O — wider branching = shorter height = fewer disk seeks. Every database index is a B-tree.
✓ Heaps power priority queues in O(log n) — stored as arrays with no pointers. Used in Dijkstra’s, task scheduling, and top-k problems.
Trees are the foundation for tries (prefix search), segment trees (range queries), and decision trees (ML). The hierarchy pattern is universal. ⚡
↗ Keep Learning
Linked Lists — Pointers, Flexibility, and the Tradeoffs vs Arrays
A scroll-driven visual deep dive into linked lists. Singly, doubly, and circular variants. Understand pointer manipulation, O(1) insertion/deletion, and real-world uses in LRU caches, OS schedulers, and undo systems.
Tries — Prefix Trees for Autocomplete, Spell Check, and IP Routing
A scroll-driven visual deep dive into trie data structures. From basic prefix trees to compressed tries and radix trees — with real-world applications in autocomplete, spell checking, IP routing tables, and T9 keyboard input.
Decision Trees — How Machines Learn to Ask Questions
A scroll-driven visual deep dive into decision trees. Learn how trees split data, what Gini impurity and information gain mean, and why trees overfit like crazy.
Sorting Algorithms — From Bubble Sort to Radix Sort
A scroll-driven visual deep dive into sorting algorithms. Compare bubble, insertion, merge, quick, heap, counting, and radix sort — with complexity analysis, stability, and real-world applications in databases, graphics, and event systems.
Comments
No comments yet. Be the first!