Module P-5·18 min read

Binary search tree invariants, insert/search/delete, and why balance is the difference between O(log n) and O(n).

JJS
Written by Jatin Jain Saraf · Senior Software Engineer

Module P-5 — Binary Search Trees: Insert, Search, Delete, and Validation

What this module covers: Module P-4 traversed a generic binary tree, whatever shape it happened to be. This module adds one invariant — every left subtree holds smaller values, every right subtree holds larger ones — and that single rule turns a tree into a structure that supports O(log n) search, exactly like Module F-11's binary search, but over a branching structure instead of a flat array. This module covers insert, search, delete (the trickiest of the three), and how to correctly validate that the invariant actually holds.


The BST Invariant

A binary search tree enforces one rule at every single node, not just at the root: everything in the left subtree is smaller than this node's value, and everything in the right subtree is larger.

text

Notice this holds recursively — not just "2 < 4 < 6" at the root, but also "1 < 2 < 3" at the node holding 2, and "5 < 6 < 7" at the node holding 6. This recursive property is exactly what makes Module P-4's inorder traversal produce sorted output: at every level, left-then-node-then-right is already the ascending order, because the invariant guarantees it.


Search: Binary Search, Applied to a Tree

Searching a BST follows the identical halving logic from Module F-11's binary search — at each node, one comparison rules out an entire subtree:

typescript

Just as Module F-11 discarded half the remaining array with each comparison, this discards an entire subtree with each comparison — if the tree is balanced (each subtree roughly half the size of its parent), this is O(log n), the same complexity class, same reasoning, now on a branching structure instead of a flat sorted array.


Insert: Find Where It Belongs, Then Attach

Inserting into a BST follows the same "compare and go left or right" logic as search — you simply keep going until you fall off the tree, then attach the new node there:

typescript

This is O(log n) on a balanced tree, for the same reason search is — but there's an important caveat worth stating plainly: a BST built by inserting already-sorted data degenerates into a straight line, not a balanced tree, because every new value is larger (or smaller) than everything already inserted, so it always attaches at the same end.

typescript

This is the exact same shape as Module F-1's best/average/worst-case discussion: a plain BST is O(log n) on average, for reasonably random insertion order, but O(n) in the worst case. Self-balancing variants (AVL trees, Red-Black trees) exist specifically to guarantee O(log n) even in this worst case, by restructuring the tree during insertion/deletion to keep it roughly balanced — a mechanism this course doesn't implement in full, but worth knowing by name, because it's exactly what production-grade ordered structures (including the B-Trees covered in Module A-13) are built to guarantee.


Delete: Three Cases

Deletion is the operation that actually requires care, because removing a node must preserve the BST invariant for everything that's left. There are three distinct cases:

typescript

Sign in to keep reading

The rest of this module is free — sign in with Google to unlock it and track your progress.

Sign in & Register

Discussion

0

Join the discussion

Loading comments...

© 2026 Jatin Jain Saraf (JJS). All rights reserved.