DSA DSA In Depth
0%
lesson bstrandomized-algorithmstree-height

Randomly Built BSTs: Expected Height O(log N) via Quicksort Dual Proof

7 min read Module 3.1: Binary Trees, Traversals, BSTs & Priority Heaps Updated Oct 23, 2026
📄 Cheat Sheet
MIT 6.046J Lecture 9: Relation of BSTs to Quicksort & Analysis of Randomly Built BST Height
MIT OpenCourseWare (Charles Leiserson) MIT 6.046J Lecture 9: Relation of BSTs to Quicksort & Analysis of Randomly Built BST Height Watch on YouTube ↗

CLRS co-author Charles Leiserson proves the exact bijection between Randomized QuickSort pivot comparisons and Randomly Built BST node depth, then derives the O(log N) expected height bound via convex exponential height variables (2^X_n) and Jensen's Inequality.

0:00 / --:--

The QuickSort–BST Bijection & Indicator Random Variables

Let $z_1 < z_2 < \dots < z_N$ denote the $N$ keys in sorted order, and suppose we insert them into an initially empty BST according to a uniformly random permutation $\pi$. In the resulting tree $T$, the depth $d(z_i)$ of node $z_i$ equals the number of strict ancestors of $z_i$. Using Linearity of Expectation and indicator random variables

$$X_{ij} = \mathbb{I}\{\text{node } z_i \text{ is an ancestor of node } z_j\}$$

we can express the depth of $z_j$ as $d(z_j) = \sum_{i \ne j} X_{ij}$, and the Total Internal Path Length $D(T) = \sum_{j=1}^N d(z_j)$ as:

$$\mathbb{E}[D(T)] = \sum_{j=1}^N \sum_{i \ne j} \Pr[z_i \text{ is an ancestor of } z_j]$$

Metric on $N$-Node BST Uniformly Random Permutation $\pi \in S_N$ Uniformly Random Tree Topology (Catalan $C_N$) Adversarial Sorted Input
Expected Node Depth $\mathbb{E}[d(x)]$ 2 ln N ≈ 1.386 log₂ N Θ(√N) (N - 1) / 2
Expected Tree Height $\mathbb{E}[h(T)]$ ≈ 4.311 ln N ≈ 2.988 log₂ N √(π N) N - 1
Probability of Balanced Split ($1/4 \le k/N \le 3/4$) p = 1/2 per level Exponentially small p = 0

Theorem: Ancestor Probability $\Pr[X_{ij} = 1] = \frac{2}{|j - i| + 1}$ & Reed's Height Constant

Consider the contiguous sorted subarray of keys $Z_{ij} = \{z_{\min(i,j)}, \dots, z_{\max(i,j)}\}$, which contains $|j - i| + 1$ elements. Look at the very first element $z_k \in Z_{ij}$ to be inserted under permutation $\pi$:

  • If $z_k = z_i$ executes first, $z_i$ becomes the root of the subtree containing $z_j$, so $z_i$ is an ancestor of $z_j$.
  • If $z_k$ is strictly between $z_i$ and $z_j$ ($\min(i,j) < k < \max(i,j)$), then $z_k$ separates $z_i$ and $z_j$ into its left and right subtrees, making it impossible for $z_i$ to ever be an ancestor of $z_j$!
  • If $z_k = z_j$ executes first, $z_j$ is an ancestor of $z_i$, so $z_i$ is not an ancestor of $z_j$.

Since $\pi$ is uniformly random, each of the $|j - i| + 1$ elements in $Z_{ij}$ is equally likely to appear first. Therefore, $\Pr[z_i \text{ is an ancestor of } z_j] = \frac{1}{|j - i| + 1}$, and summing over all pairs yields:

$$\mathbb{E}[D(T)] = 2 \sum_{i=1}^N \sum_{j=i+1}^N \frac{1}{j - i + 1} = 2N H_N - 4N + 2N = 2(N + 1)H_N - 4N \approx (2 \ln 2) N \log_2 N \approx 1.386 N \log_2 N$$

Furthermore, using Jensen's Inequality on the exponential potential $\mathbb{E}[3^{h(T)}]$ (CLRS §12.4) bounds $\mathbb{E}[h(T)] \le 3 \log_{4/3} N$, and Bruce Reed (2003) proved the exact asymptotic expansion $\mathbb{E}[h(T)] = \alpha \ln N - \beta \ln \ln N + O(1)$ where $\alpha \approx 4.31107$ satisfies $\alpha \ln(2e/\alpha) = 1$.

BST Hibbard Successor Deletion, Tree Diameter & QuickSort Duality
Expected Height ≈ 4.311 ln N · O(H) Delete

Step through Hibbard’s 3-case BST deletion (splicing out the in-order successor y = min(z.right) when deleting a 2-child node z), or inspect the exact bijection between Random BST depth and QuickSort pivot comparisons.

Step 1 / 8
Custom Input:
Timeline

Invariant & State

Delete z = 20 (Case 3: Two Children): Locate in-order successor y = min(z.right) = 25. By definition, y has NO left child (y.left == null)!
z=20 10 30 y=25 35
Hibbard Step
2 Children → Find min(z.right)
Successor Node y
y = 25 (y.left == null)
In-Order Sequence
[10, 20, 25, 30, 35]
Asymmetry Note
Repeated delete skews height → Θ(√N)
[Step 1] Delete z = 20 (Case 3: Two Children): Locate in-order successor y = min(z.right) = 25. By definition, y has NO left child (y.left == null)!

Visualizing the QuickSort Pivot ↔ BST Ancestor Isomorphism

Sorted Keys: [z₁, z₂, | z₃, z₄, z₅, z₆, z₇ | z₈, z₉] Subarray Z₃,₇ = {z₃, z₄, z₅, z₆, z₇} (size 5) First inserted in Z₃,₇ = z₃ ⇒ z₃ is ancestor of z₇ (P = 1/5) First inserted in Z₃,₇ ∈ {z₄,z₅,z₆} ⇒ splits z₃ & z₇ (P = 3/5) Average Node Depth: 2 ln N ≈ 1.386 log₂ N Expected Max Height: 4.311 ln N ≈ 2.988 log₂ N z₅ z₂ z₈ z₁ z₄
flowchart LR Perm["Random Permutation π ∈ S_N (N! equally likely orders)"] --> Map["Many-to-One BST Mapping (Balanced trees correspond to exponentially more permutations!)"] Map --> Dist["Randomly Built BST Distribution E[depth] = 1.386 log₂ N E[height] = 2.988 log₂ N"]

Hardware Reality: Why $1.386 \log_2 N$ Beats Rotation-Heavy Trees on Static Read-Mostly Workloads

A strictly balanced AVL tree has worst-case height $\le 1.44 \log_2 N$ and average depth $\approx 1.04 \log_2 N$, whereas a Randomly Built BST has average depth $\approx 1.386 \log_2 N$—only 34% more comparisons on average, while requiring zero balance-factor metadata bytes and zero pointer-modifying rotations! When building a BST offline from a known dataset, shuffling the input array with Fisher-Yates (or assigning random heap priorities as in a Treap, Module 08) guarantees $O(\log N)$ expected height against any adversarial input order.

Implementation: Monte Carlo Verification of Ancestor Harmonic Law & Fisher-Yates Pre-Shuffle

// C++20: Offline Randomized BST Builder via Fisher-Yates Shuffle + Exact Harmonic Depth Estimator
#include <vector>
#include <random>
#include <cmath>

struct Node {
    int key;
    Node *left = nullptr, *right = nullptr;
    explicit Node(int k) : key(k) {}
};

void insertBST(Node*& root, int key) {
    Node** curr = &root;
    while (*curr != nullptr) {
        curr = (key < (*curr)->key) ? &((*curr)->left) : &((*curr)->right);
    }
    *curr = new Node(key);
}

// Eliminates O(N) sorted-input degeneration before bulk BST insertion
Node* buildRandomBST(std::vector<int> keys, uint64_t seed = 42) {
    std::mt19937_64 rng(seed);
    for (size_t i = keys.size(); i > 1; --i) {
        std::uniform_int_distribution<size_t> dist(0, i - 1);
        std::swap(keys[i - 1], keys[dist(rng)]);
    }
    Node* root = nullptr;
    for (int k : keys) insertBST(root, k);
    return root;
}

// Exact theoretical expected depth of a random node in an N-node Randomly Built BST
double expectedRandomBSTDepth(int n) {
    double harmonic = 0.0;
    for (int k = 1; k <= n; ++k) harmonic += 1.0 / k;
    return (2.0 * (n + 1) / n) * harmonic - 4.0;
}

Failure Modes & Adversarial Pitfalls

  • Confusing "Randomly Built BST" with "Uniformly Random Binary Tree": There are $N!$ permutations in $S_N$ but only $C_N = \frac{1}{N+1}\binom{2N}{N}$ distinct BST topologies. The mapping from permutations to trees is heavily non-uniform: for $N=3$, both permutations $(2, 1, 3)$ and $(2, 3, 1)$ build the balanced tree rooted at $2$ (probability $2/6 = 1/3$), whereas each of the four height-2 chains is built by only $1/6$ of permutations! If you sample a tree uniformly from the $C_N$ Catalan topologies instead of inserting a random permutation, the expected height explodes from $\Theta(\log N)$ to $\Theta(\sqrt{N})$!
  • Online Adversaries Without Randomization: A Randomly Built BST assumes the input permutation is random. In an online server accepting network requests, an attacker can send sorted keys to force $O(N)$ height unless the data structure itself randomizes inside insert (which is exactly what a Randomized Treap or Skip List does).
Check yourself Question 1 of 4
Concept Check Expected Depth and Height Constants of a Randomly Built BST recall

When N distinct keys are inserted in a uniformly random permutation into an initially empty unbalanced BST, what are the asymptotic expected node depth E[d(x)] and expected tree height E[h(T)]?

⚡ Interactive Challenge · Staff Level

Algorithmic Design & Complexity Check

Analyze the invariant, asymptotic bound, and hardware behavior before selecting.

For N = 3 keys {1, 2, 3}, there are 3! = 6 input permutations but only C₃ = 5 distinct BST shapes. How does sampling a BST via a uniformly random input permutation differ from sampling uniformly among all C_N Catalan binary tree topologies?

Key Takeaways

A Randomly Built BST is structurally isomorphic to the pivot partition tree of Randomized QuickSort.
Node z_i is an ancestor of z_j if and only if z_i is the very first element inserted from the contiguous sorted range {z_min(i,j), ..., z_max(i,j)}, occurring with probability 1 / (|j - i| + 1).
Summing the harmonic series over all pairs proves expected node depth is 2(1 + 1/N)H_N - 4 ≈ 1.386 log₂ N and expected height is ≈ 4.311 ln N ≈ 2.988 log₂ N.
Random permutations over-weight balanced trees compared to uniform Catalan tree sampling, which has expected height Θ(√N).
Was this lesson helpful? Spot an issue or have a suggestion? Let us know on GitHub.
Report an issue