Is it true that inserting an element to an AVL tree requires $O(1)$ rotations?
How many rotations, does deletion from AVL require?
I've searched for these two questions with no luck so far.
Is it true that inserting an element to an AVL tree requires $O(1)$ rotations?
How many rotations, does deletion from AVL require?
I've searched for these two questions with no luck so far.
The obvious resource, Wikipedia, I did not find very helpful.
When inserting an element at most one (single or double) rotation is needed, at the lowest point where the tree is out of balance. After rotation the height of that subtree is the same as in the original subtree, so all nodes upwards are balanced. See the answer by colleague Raphael to "Does the rebalancing propagate upwards only to update the height of the nodes in an AVL tree?"
picture: originaly the balance at node $p$ equals $-1$ (left subtree one deeper than right subtree). After insertion in the left subtree the balance is now $-2$. Then rebalancing at $p$ will restore the balance at the root to $0$, while at the same time the heigth of the tree at new root $q$ is now the same as for the original tree. This means that all balance factors above will not change.
When deleting occurs, one is not always that lucky. Rotation does not necessarily restore the original tree height, so the tree has to be updated at other levels higher up in the tree. Worst case trees are those which are minimal AVL trees, meaning with no node can be removed without violating the AVL property. There you might have to rotate every level, thus a logarithmic number of times.
picture: an AVL tree of "Fibonacci" type. Deleting the node marked "X" results in unbalance at $11$. Rebalancing at $11$ leads to a tree that is shorter than the original tree, and we get an new imbalance at the root. Another rotation will solve that problem. Note that again the resulting tree is shorter than the original one, so this would propagate to levels above.
As one complement for Hendrik Jan's answer (IMHO it may be bloated if put the following in the edit of that answer. It may be appropriate as one reference for someone who struggles to understand that answer), here I give one more detailed description of why "inserting an element at most one (single or double) rotation is needed" based on this tutorial.
After the leaf insertion, we need to move the target new middle node to the root. Each basic rotation in link3 can move such a node one level up.
Let's focus on one case (Here assume the insertion element is in the subtree T1):
T1, T2, T3 and T4 are subtrees.
z y
/ \ / \
y T4 Right Rotate (z) x z
/ \ - - - - - - - - -> / \ / \
x T3 T1 T2 T3 T4
/ \
T1 T2
At the first glance, someone may innocently think the following is possible: depth(T1)=d,depth(T2)=d+1,depth(T3)=d+3,depth(T4)=d+5, then the right tree is not one AVL. But that is impossible.
Assume after insertion depth(T4)=d, then since each insertion at most add one to the depth, depth(y)=d+2 after the increment one. Then due to the increment, depth(x)=d+1 and depth(T1)=d. Since y are balanced after (exclude depth(T3)=d-1) and before (exclude depth(T3)=d+1) the insertion, depth(T3)=d. Similarly, depth(T2)=d-1. So the corresponding depth map for the above tree (Notice here d+3 is the depth after insertion, so the original depth is d+2 which is same as the rotation):
d+2 d+3 d+2
/ \ / \ / \
d+1 d Insertion d+2 d Right Rotate (z) d+1 d+1
/ \ -----------> / \ - - - - - - - - -> / \ / \
d d d+1 d d d-1 d d
/ \ / \
d-1 d-1 d d-1
We can relate each level with the corresponding height as this course note says. More specifically, we can view the tree in one simplified form same as link3 by viewing d as the base similar to $\epsilon$ in Turing Machine. This connects with the reference answer in Hendrik Jan's answer.
2 3 2
/ / / \
1 Insertion 2 Right Rotate (z) 1 1
-----------> / - - - - - - - - ->
1
Then this corresponds to the Right Rotation in link3 where y,z corresponds to B,A there.
We can also transform the following into the familiar balance factor map where the corresponding balance factors of pseudo-leaves like T1 are not cared about:
-2 0
/ \ / \
-1 d Right Rotate (z) -1 0
/ \ - - - - - - - - -> / \ / \
-1 d d d-1 d d
/ \
d d-1
The other 3 cases are similar with the same depth structure but with a bit different layouts. Based where the middle node is, the action length to move that node up to the top level / the root location may be 1 or 2.