The Height Cache
In TreeMap, every node caches the height of its subtree in a field. height(node) reads that field and returns for null. updateHeight(node) recomputes it from the children’s cached heights. On the way back up, the recursive put calls updateHeight and then rebalance on each node, and each rotation updates the heights it changes.
Solution
The cost grows linearly with , even though the tree is balanced. The search in containsKey and the trip down to the new leaf are , and each rotation is . The slow part is the balance factor. Each rebalance on the way back up calls height on both children, and a recursive height visits every node in the subtree it is given. The walk back up always ends at the root, so the last rebalance alone visits all nodes below the root.
Solution
. It visits the nodes on one path, and each costs one updateHeight and one rebalance on the way back up.
Solution
Inside the rotated subtree, only the heights of root and child. They are the only nodes whose children changed. Every subtree below them kept its shape, so its root’s height is still correct.