A Binary Search Tree (BST) is an ordered binary tree that maintains the BST Invariant: $$\text{For every node } X: \quad \text{All nodes in Left Subtree} < X < \text{All nodes in Right Subtree}$$ This ordering property transforms linear searches into fast $O(h)$ tree searches (where $h$ is the height of the tree).
Think of looking up a contact in an alphabetized address book:
'M'.'Smith', you know 'S' > 'M'. You immediately discard the entire first half of the book!'Brown', you would discard the second half.| Operation | Balanced BST Height ($h = \log n$) | Degenerate BST (Skewed $h = n$) |
|---|---|---|
| Search | $O(\log n)$ | $O(n)$ |
| Insert | $O(\log n)$ | $O(n)$ |
| Delete | $O(\log n)$ | $O(n)$ |
Note: If elements are inserted in already sorted order (1, 2, 3, 4, 5), an un-balanced BST degenerates into a singly linked list! Self-balancing trees (AVL, Red-Black Trees) prevent this skewing.
Deleting a node from a BST has three distinct structural cases:
return None).min_node(root.right)).| Step | Current Node | Comparison | Action |
|---|---|---|---|
| 1 | 50 | $25 < 50$ | Traverse Left to Node 30 |
| 2 | 30 | $25 < 30$ | Traverse Left to Node 20 |
| 3 | 20 | $25 > 20$ | Traverse Right (Right is null) |
| 4 | null | Insert position found | Attach new Node 25 as right child of 20! |
node.left.val < node.val && node.right.val > node.val insufficient to validate if a binary tree is a valid BST?Continue learning with hands-on practice, examples, and exercises in the upcoming topic.
Test your understanding of this lesson with 5 questions. Each question has one correct answer.
Practice in live mentor-led sessions with real projects, mock tests, and verifiable certification at MSK Institute.