Optimized tree-rebalancing code to avoid redundant pointer updates.