All Data Structures
Binary tree
Core Implementation
Basic Binary Tree
Implementation Variations
1. Complete Binary Tree
2. Perfect Binary Tree
3. Threaded Binary Tree
Time Complexities
Operation | Perfect/Complete | Skewed | Threaded | Average |
---|---|---|---|---|
Insert | O(log n) | O(n) | O(n) | O(n) |
Search | O(log n) | O(n) | O(n) | O(n) |
Delete | O(log n) | O(n) | O(n) | O(n) |
Inorder | O(n) | O(n) | O(n) | O(n) |
Preorder | O(n) | O(n) | O(n) | O(n) |
Postorder | O(n) | O(n) | O(n) | O(n) |
Level Order | O(n) | O(n) | O(n) | O(n) |
Height | O(log n) | O(n) | O(n) | O(n) |
Space Complexities
Implementation | Space Complexity | Additional Notes |
---|---|---|
Perfect | O(2^h - 1) | h is height, all levels filled |
Complete | O(n) | Last level filled from left to right |
Skewed | O(n) | Degenerates to linked list |
Threaded | O(n) | Extra space for thread pointers |
Common Data Structure Patterns
-
Tree Traversal Patterns
- DFS (Inorder, Preorder, Postorder)
- BFS (Level Order)
- Vertical Order
- Boundary Traversal
-
Path Patterns
- Root to Leaf Paths
- Maximum Path Sum
- Path with Given Sum
- Longest Path
-
View Patterns
- Left/Right View
- Top/Bottom View
- Vertical View
-
Level-based Patterns
- Level Order
- Level Sum
- Level Average
- Connect Level Nodes
-
Tree Construction Patterns
- From Traversal Arrays
- From Level Order
- Serialization/Deserialization
-
Tree Property Patterns
- Height/Depth
- Diameter
- Balance
- Symmetry
Edge Cases to Consider
- Empty Tree
- Single Node
- Full/Complete/Perfect Checks
Optimization Techniques
- Node Cache
- Level Linking
- Parent Pointers
Memory Management
- Node Pool
- Tree Cleanup
Performance Considerations
- Iterative vs Recursive Traversals
- Level Order Optimization
Common Pitfalls
- Incorrect Height Calculation
- Memory Leaks in Deletion
- Incorrect Level Order Traversal