Binary Search Tree
🌲 Binary Search Tree
Binary Search Tree (BST) — Complete Guide
Learn BST concepts, operations, and interview patterns with clean explanations.
Topic Trees
|
Difficulty Easy → Hard
|
Focus Search + Traversal
Concept
Easy
BST
What is a Binary Search Tree?
A Binary Search Tree is a binary tree where:
left < node < right.
This property enables efficient searching, insertion, and deletion.
Time: O(log n) (average)
Rules
Easy
BST Properties
- Left subtree values are smaller
- Right subtree values are larger
- No duplicates (generally)
Operations
Medium
BST Operations
Insertion
- Compare and move left/right
- Insert at null position
Deletion
- Leaf → remove
- One child → replace
- Two children → use inorder successor
Search
- Compare values
- Move left/right
Code
Medium
Java
Search in BST (Java)
public TreeNode searchBST(TreeNode root, int val) {
while (root != null) {
if (val == root.val) return root;
root = val < root.val ? root.left : root.right;
}
return null;
}
Advanced
Hard
Advanced BST Topics
- AVL Trees → strictly balanced
- Red-Black Trees → balanced with colors
FAQ
Common
FAQs
- Why BST? → Fast search (O(log n))
- When slow? → Skewed tree (O(n))
- AVL vs RB? → AVL stricter, RB faster updates
Practice BST problems to master tree-based interviews 🚀
Explore More →