liveBook · Manning

Tree rotation

As explained in Grokking Algorithms, Second Edition

A tree rotation is a local restructuring operation that changes parent-child relationships while preserving the tree's ordering.

A tree rotation rearranges a small group of nodes so an unbalanced binary search tree becomes more balanced. The nodes keep their ordering, while parent-child relationships change. section 8.5 section 8.4

A simple left rotation

Imagine three connected nodes, A, B, and another child. Starting with A as the root, rotate left so B becomes the root. Adding nodes can make an AVL tree unbalanced; rotating the appropriate subtree restores balance. In one walkthrough, nodes 20 and 30 are rotated at different points as the tree grows. section 8.5

In an AVL tree, rotation fits into a larger balancing process:

  • Insert the new node and update the affected node’s height and balance factor. section 8.6
  • Move upward through the tree, updating ancestors until the imbalance is corrected. section 8.6
  • Rotate the required subtree; AVL trees generally need at most one rebalancing during this upward pass. section 8.6

How Grokking Algorithms, Second Edition covers it

How deep it goes
Demonstrates left rotations and balance-factor updates, but defers the other rotation cases and implementation details.
Examples
Uses three-node A/B arrangements and growing AVL trees containing 20, 30, and 10.
What it assumes
Builds from binary trees and binary search trees before introducing AVL balance and rotations.
What you can do after
Apply rotations to rebalance an AVL tree and preserve logarithmic search and insertion performance.
How it connects
Connects binary trees and binary search trees with AVL trees, balanced trees, height, insertion, and search performance.

Also explained in

Compare how 2 books explain tree rotation →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Grokking Algorithms, Second Edition. The section links point to the parts of the book it draws on.