0

Assume I have a balanced AVL tree and after an ADD, it becomes unbalanced. Will an AVL tree always be rebalanced by a one single or one double rotation, or is there a case that would require more rotations?

graviton
  • 465
  • 3
  • 13

1 Answers1

0

A single rebalancing will always be sufficient to restore the balance invariant, assuming the balance invariant was satisfied prior to the insertion.

The AVL invariant is that any node has child depths which differ by at most 1. After a single insertion, the child depths can differ by at most 2. A single traversal down the path to the inserted node, rotating as necessary, is capable of resolving the imbalance.

Sneftel
  • 40,271
  • 12
  • 71
  • 104
  • I didn't downvote, but I think a claim like this need to go together with a proof. – Bernhard Barker Mar 12 '14 at 23:24
  • It's pretty trivial. The AVL invariant is that any node has child depths which differ by at most 1. After a single insertion, the child depths can differ by at most 2. A single traversal down the path to the inserted node, rotating as necessary, is capable of resolving the imbalance. – Sneftel Mar 12 '14 at 23:30
  • @Sneftel: can you move that comment into your answer? – halfer Mar 13 '14 at 00:09