Trees
Binary Search Tree
Master Binary Search Tree (BST) operations: insertion, search, and deletion (3 cases) with balanced vs skewed analysis.
Last Updated: August 2, 2026
•
15 min read
1. Introduction
What is a Binary Search Tree (BST)?
A Binary Search Tree is a node-based binary tree data structure which has the following properties:Why is it Important?
This sorted invariant allows you to bypass half of the tree during each comparison. In a balanced BST, lookup, insertion, and deletion takeO(log N) logarithmic time, mirroring binary search arrays but supporting dynamic adjustments.
Where is it Used?
TreeSet or C++'s std::set).2. Mental Model: The Sorted Phone Book
Imagine searching for a contact in a phone book:
3. Core BST Operations & Implementations
BST Deletion Cases
1. Case 1: Node is a Leaf: Simply delete the node (returnnull).
2. Case 2: Node has One Child: Bypass the node, connecting its parent directly to its child (return the non-null child).
3. Case 3: Node has Two Children: Find the In-order Successor (the smallest node in the right subtree). Copy its value to the target node, then recursively delete the in-order successor from the right subtree.4. Visual Trace: Deleting a Node with Two Children
Let's delete node 20 from the tree below (Case 3):
5. Real-World Examples
6. Interview Perspective
How Interviewers Ask This Topic
Interviewers test validation and pointer tracking:[min, max] intervals down:isValid(node, \min, \max) \implies \min < node.val < \max
Common Mistakes
Warning: 1. Wrong BST Validation: Checking only
root.left.val < root.val < root.right.val recursively. This is incorrect. Consider: [10, 5, 15, null, null, 6, 20]. Here, 6 is a left child of 15, but 6 is smaller than the root 10, violating the global BST invariant.> 2. Skewed BST Complexity: Assuming BST search is alwaysO(log N). If elements are inserted in sorted order (e.g.1, 2, 3, 4), the BST becomes skewed, degrading operations toO(N)linear time.
7. Summary
< node < right subtree.O(log N) time, skewed is O(N) time.8. Quiz
Question 1: What is the time complexity of searching in a perfectly balanced BST of size N?
Answer:O(log N) time. At each step, we discard exactly half of the remaining search space.
Question 2: What is the in-order predecessor of a node in a BST?
Answer: The largest element in the node's left subtree. It is found by going left once, then right as far as possible.Question 3: If you insert elements in the order [5, 3, 8, 2, 4, 7, 9], what is the value of the root node?
Answer:5. The first element inserted into an empty BST always becomes the permanent root node.
Question 4: True or False: Every binary search tree is balanced.
Answer: False. BSTs only guarantee key sorting, not height balancing. Self-balancing variations (like AVL or Red-Black trees) are required to guarantee balance.Question 5: What is the complexity of validating a BST of N nodes?
Answer:O(N) time, since we must visit each of the N nodes exactly once to verify they satisfy their respective range boundaries.