🎯 Introduction to Binary Search Trees
What is a Binary Search Tree (BST)?
A BST is a binary tree in symmetric order. This means:
- Each node contains a key and an associated value
- Every node's key is larger than all keys in its left subtree
- Every node's key is smaller than all keys in its right subtree
Binary Tree Structure
A binary tree is either:
- Empty (null)
- A node with references to two disjoint binary trees (left and right subtrees)
BST Anatomy
Note: Keys smaller than E are on the left, keys larger than E are on the right
🏗️ BST Fundamentals
Node Structure
⚠️ Key Requirements
The Key type must implement Comparable interface for ordering operations.
Keys must be unique and support comparison operations.
BST Properties
| Property | Description |
|---|---|
| Symmetric Order | Left subtree < Node < Right subtree |
| Root | Top node of the tree |
| Leaf | Node with no children (both links null) |
| Parent | Node that has a link to another node |
| Depth | Number of links from root to node |
| Height | Maximum depth of any node |
⚙️ BST Operations
1. Search Operation
Search Algorithm
Rule: If less, go left; if greater, go right; if equal, search hit!
✅ Search Complexity
Cost: Number of compares = 1 + depth of node
Best case: O(log n) for balanced tree
Worst case: O(n) for degenerate tree
2. Insert Operation
Insert Algorithm
Rule: If less, go left; if greater, go right; if null, insert!
Insert Example: Adding G to a BST
- Start at root S (G < S, go left)
- Compare with E (G > E, go right)
- Compare with R (G < R, go left)
- Compare with H (G < H, go left)
- H.left is null → Insert G here!
3. Finding Minimum/Maximum
🗑️ BST Deletion (Hibbard Deletion)
⚠️ Deletion Complexity
Deletion is the most complex operation in BSTs. There are three cases to handle.
Case 0: No Children (Leaf Node)
Solution: Simply delete the node by setting the parent link to null.
Case 1: One Child
Solution: Replace the node with its child (bypass the node).
Case 2: Two Children (Most Complex)
Hibbard Deletion Algorithm
- Find the node
tto delete - Find successor
x= minimum node int's right subtree - Delete the minimum in
t's right subtree - Replace
twithx:- Set
x.left = t.left - Set
x.right = deleteMin(t.right)
- Set
Example: Deleting E (has 2 children)
Delete Minimum Helper Function
Complete Delete Implementation
⚠️ Hibbard Deletion Problem
Unsatisfactory solution: Not symmetric!
Consequence: Trees become unbalanced after many deletions
Performance degradation: √N per operation (instead of log N)
Open problem: Simple and efficient delete for BSTs remains unsolved!
Lazy Deletion Alternative
Tombstone Approach
- Set the node's value to null
- Leave key in tree to guide searches
- Don't consider it equal during search
Problem: Memory overhead increases over time (tombstone overload)
Cost: ~2 ln N' where N' = total keys ever inserted
📊 Performance Analysis
Tree Shape Matters!
Mathematical Analysis
Random Insertion (Average Case)
Proposition: If N distinct keys are inserted in random order:
- Expected number of compares: ~2 ln N ≈ 1.39 log₂ N
- Expected tree height: ~4.311 ln N (Reed, 2003)
Proof: 1-1 correspondence with quicksort partitioning
Worst Case
- Height: N - 1 (linked list)
- Occurs when keys inserted in sorted order
- Probability: Exponentially small for random insertions
Complexity Summary Table
| Operation | Best Case | Average Case | Worst Case |
|---|---|---|---|
| Search | log N | 1.39 log N | N |
| Insert | log N | 1.39 log N | N |
| Delete | log N | √N | N |
| Min/Max | log N | 1.39 log N | N |
⚠️ Delete Performance Degradation
After many deletions using Hibbard deletion, trees become unbalanced!
All operations degrade to √N on average
💻 Complete BST Implementation
Class Structure
Additional Useful Methods
Tree Traversals
💡 Traversal Tip
Inorder traversal of a BST produces keys in sorted order!
This is why it's called "symmetric order" - the inorder traversal respects the ordering.
📝 Summary & Comparison
Symbol Table Implementation Comparison
| Implementation | Search (Worst) | Insert (Worst) | Delete (Worst) | Search (Avg) | Insert (Avg) | Delete (Avg) | Ordered? |
|---|---|---|---|---|---|---|---|
| Sequential Search (Unordered List) |
N | N | N | N/2 | N | N/2 | ❌ |
| Binary Search (Ordered Array) |
log N | N | N | log N | N/2 | N/2 | ✅ |
| BST | N | N | N | 1.39 log N | 1.39 log N | √N | ✅ |
Key Takeaways
✅ BST Advantages
- Efficient search and insert: O(log N) on average
- Supports ordered operations (min, max, floor, ceiling, rank)
- Inorder traversal gives sorted order
- Dynamic structure (no need to specify size upfront)
- Simple recursive implementation
⚠️ BST Limitations
- No performance guarantee: can degenerate to O(N)
- Shape depends on insertion order
- Delete operation degrades performance to √N
- Not suitable when guaranteed performance is required
🚀 What's Next?
Balanced BSTs solve the performance guarantee problem!
- 2-3 Trees: Guaranteed log N performance
- Red-Black Trees: Practical implementation of balanced BSTs
- AVL Trees: Strictly balanced trees
- B-Trees: For disk-based storage
Next lecture will cover how to guarantee logarithmic performance for all operations!
Quick Reference Card
BST Operations Cheat Sheet
| Operation | Algorithm | Complexity (Avg) |
|---|---|---|
| Search | Compare, go left/right, repeat | O(log N) |
| Insert | Search, insert at null link | O(log N) |
| Delete (0 children) | Set parent link to null | O(log N) |
| Delete (1 child) | Replace with child | O(log N) |
| Delete (2 children) | Replace with successor (Hibbard) | O(√N) |
| Min/Max | Go all left/right | O(log N) |
| Floor/Ceiling | Search with fallback logic | O(log N) |
Study Tips
📚 How to Master BSTs
- Draw trees! Practice drawing insertion and deletion by hand
- Trace algorithms: Follow the recursive calls step by step
- Compare cases: Best vs average vs worst - understand when each occurs
- Implement from scratch: Code it without looking at notes
- Analyze complexity: Always count comparisons and consider tree height
Common Exam Questions
🎓 Practice Problems
- Draw the BST resulting from inserting keys in order: 5, 2, 8, 1, 3, 7, 9
- Show step-by-step deletion of a node with 2 children
- What is the worst-case height of a BST with N nodes?
- Why does Hibbard deletion degrade performance?
- Compare BST vs Binary Search on arrays - when to use each?
- Write recursive code for BST traversals (inorder, preorder, postorder)