Balanced BSTs (AVL, Red-Black — Conceptual)
📖 Concept
Self-balancing BSTs guarantee O(log n) operations by maintaining tree height ≈ log n. You likely won't implement them from scratch in interviews, but understanding them is crucial.
AVL Tree:
- Balance factor = height(left) - height(right), must be {-1, 0, 1}
- After insert/delete, if unbalanced: perform rotations
- Rotations: Left, Right, Left-Right, Right-Left
- Strictly balanced → faster lookups than Red-Black
- More rotations per operation → slower insertions
Red-Black Tree:
- Each node is Red or Black with rules:
- Root is black
- No two adjacent red nodes
- Every path from root to leaf has same number of black nodes
- Less strictly balanced than AVL → fewer rotations
- Used in: Java TreeMap, C++ std::map, Linux kernel
AVL vs Red-Black vs Skip List:
| Feature | AVL | Red-Black | Skip List |
|---|---|---|---|
| Height | 1.44 log n | 2 log n | ~log n |
| Lookup | Faster | Slightly slower | Same |
| Insert/Delete | More rotations | Fewer rotations | Simpler |
| Implementation | Complex | Very complex | Moderate |
| Used in | Databases | Language STL | Redis, LevelDB |
In interviews: Know WHAT they do, WHY they exist, and WHEN to mention them. You'll almost never implement one from scratch.
🏠 Real-world analogy: An AVL tree is like a very strict manager who rebalances the team immediately when one side gets too heavy. A Red-Black tree is a more relaxed manager who allows a bit of imbalance but keeps things within bounds.
💻 Code Example
1// AVL TREE — Conceptual implementation (simplified)2class AVLNode {3 constructor(val) {4 this.val = val;5 this.left = null;6 this.right = null;7 this.height = 1;8 }9}1011class AVLTree {12 constructor() { this.root = null; }1314 height(node) { return node ? node.height : 0; }15 balanceFactor(node) { return this.height(node.left) - this.height(node.right); }16 updateHeight(node) { node.height = 1 + Math.max(this.height(node.left), this.height(node.right)); }1718 // RIGHT ROTATION19 rotateRight(y) {20 const x = y.left;21 y.left = x.right;22 x.right = y;23 this.updateHeight(y);24 this.updateHeight(x);25 return x;26 }2728 // LEFT ROTATION29 rotateLeft(x) {30 const y = x.right;31 x.right = y.left;32 y.left = x;33 this.updateHeight(x);34 this.updateHeight(y);35 return y;36 }3738 insert(val) { this.root = this._insert(this.root, val); }3940 _insert(node, val) {41 if (!node) return new AVLNode(val);42 if (val < node.val) node.left = this._insert(node.left, val);43 else if (val > node.val) node.right = this._insert(node.right, val);44 else return node; // No duplicates4546 this.updateHeight(node);47 const bf = this.balanceFactor(node);4849 // Left-Left → Right rotate50 if (bf > 1 && val < node.left.val) return this.rotateRight(node);51 // Right-Right → Left rotate52 if (bf < -1 && val > node.right.val) return this.rotateLeft(node);53 // Left-Right → Left rotate left child, then right rotate54 if (bf > 1 && val > node.left.val) {55 node.left = this.rotateLeft(node.left);56 return this.rotateRight(node);57 }58 // Right-Left → Right rotate right child, then left rotate59 if (bf < -1 && val < node.right.val) {60 node.right = this.rotateRight(node.right);61 return this.rotateLeft(node);62 }63 return node;64 }65}6667// When you'd mention balanced BSTs in interviews:68// "I'd use a balanced BST here for O(log n) insert + ordered iteration"69// "JavaScript doesn't have a built-in TreeMap, but we could use a sorted array70// with binary search, or implement a skip list"71// "In production, I'd use a library like 'bintrees' for a Red-Black Tree"
🏋️ Practice Exercise
Practice Problems:
- Understand and draw all 4 AVL rotation cases (LL, RR, LR, RL)
- Insert elements 1-7 into an AVL tree and show each rotation
- Explain why Red-Black trees are preferred in language STLs
- Implement basic AVL insert with rotations
- Given a sequence of operations, what does the AVL tree look like after each?
- Compare the height guarantees of AVL vs Red-Black trees
- When would you use a balanced BST vs a hash map? (ordering matters!)
- Implement an order-statistics tree (find kth element in O(log n))
- Design a data structure that supports insert, delete, and getMedian all in O(log n)
- Explain the trade-offs between AVL, Red-Black, B-Tree, and Skip List
⚠️ Common Mistakes
Trying to implement AVL/Red-Black in an interview from scratch — just explain when you'd use them
Not knowing that JavaScript has no built-in ordered set/map — this affects DS selection in interviews
Confusing BST balance with heap property — they're completely different invariants
Using a balanced BST when a hash map suffices — only use BST when you need ordering
Not mentioning balanced BSTs when appropriate — 'I'd use a sorted structure for O(log n) operations'
💼 Interview Questions
🎤 Mock Interview
Practice a live interview for Balanced BSTs (AVL, Red-Black — Conceptual)