Balancing a binary search tree
Master System Design with Codemia
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.
Introduction
Balancing a binary search tree matters because the tree only gives fast search, insert, and delete performance when its height stays under control. If the tree becomes skewed, operations degrade from logarithmic time to linear time and the structure behaves more like a linked list than a search tree.
What It Means for a BST to Be Balanced
A binary search tree is balanced when the left and right subtrees stay reasonably close in height. Different self-balancing tree families define this differently, but the goal is always the same: keep the height near log n so lookups and updates remain efficient.
If you insert sorted values into a plain BST, you get the worst case:
- insert
1 - insert
2 - insert
3 - insert
4
The result is a long chain leaning to one side. Search becomes O(n).
Two Different Ways to Balance
There are two broad strategies.
The first is rebuilding. If you already have a BST and want to rebalance it once, do an inorder traversal to collect values in sorted order, then rebuild a new tree by always choosing the middle value as the root.
The second is maintaining balance incrementally. Trees such as AVL trees and red-black trees rebalance during insert and delete operations using rotations.
The rebuilding approach is simple and great for interviews or one-off rebalancing. Self-balancing trees are what you use when the tree changes continuously.
Rebuild from Sorted Order
Here is a Python example that rebalances a BST by rebuilding it:
This approach runs in O(n) time because each node is visited a constant number of times.
Rotations in Self-Balancing Trees
If you need the tree to remain balanced after every update, rebuilding the whole tree each time is too expensive. That is where rotations come in.
A left rotation and a right rotation are local restructuring operations that preserve the BST ordering while changing shape. AVL trees use them to correct height imbalances. Red-black trees use them together with coloring rules.
A simplified right rotation looks like this:
The actual implementation in an AVL tree also updates node heights and may combine rotations for left-right or right-left cases.
Which Approach Should You Use
Use rebuild-from-inorder when:
- you already have a plain BST
- you want a clean rebalance pass
- you do not need to preserve the exact node identities
Use a self-balancing tree when:
- inserts and deletes happen frequently
- you need predictable worst-case performance
- rebalancing must happen continuously
In production code, you usually pick a tree type that maintains balance automatically instead of bolting rebalancing onto a plain BST later.
Common Pitfalls
The most common mistake is saying a BST is balanced simply because the root has two children. Balance is about subtree height across the whole tree, not appearance near the top.
Another issue is rebuilding without doing an inorder traversal first. If the input list is not sorted, the rebuilt tree will not preserve BST ordering.
Developers also sometimes implement rotations without updating height or metadata fields. That produces trees that look balanced briefly but break later.
Finally, do not choose a plain BST when the workload obviously needs predictable performance under adversarial insertion order.
Summary
- Balancing keeps BST height near logarithmic size and preserves fast operations.
- A skewed BST degrades to linear-time behavior.
- You can rebalance once by rebuilding from the inorder traversal.
- Self-balancing trees such as AVL and red-black trees use rotations during updates.
- Pick the strategy based on whether the tree changes occasionally or continuously.

