Left and right rotations
A right rotation around node y with left child x makes x the new root of the subtree and y the right child of x. It is symmetric for the other side.
Double rotations (left-right, right-left) combine two simple rotations when the imbalance is deeper.
Rotations preserve the search tree invariant and change the height.
| Role | before | after |
|---|---|---|
| root of the pair | y | x |
| inner edge | y.left = x | x.right = y |
| ## Where used |
Local pointer updates in AVL/Red-Black and related structures. Rotations are the mechanism by which indices become flat again after Insert/Delete without rebuilding everything.
Depth
A rotation changes local parent-child relationships without destroying the ascending in-order sequence of the keys. The middle subtree switches sides while remaining between the same boundary keys.
In addition to child references, parent references, root reference, and stored heights may need to be updated. The order of assignments must retain all necessary old references.
A straightforward imbalance is fixed with a single rotation. For a bent path, a rotation at the child must be performed first, followed by a rotation at the affected node.
Difficulty levels
- Check the key order before and after a rotation.
- Update all affected references and metadata.
- Recognize simple and double rotation cases.
Pitfalls
A correct local drawing is not sufficient if the new subtree root is not reconnected to its previous parent node. Heights should only be recalculated after the references are updated.
Tasks
Card Info
- Topic: Algorithms and Data Structures
- Difficulty: Intermediate
- Completed: 0 users