Check on case 1 and 3
- Assume we have a tree such that key[x] < key[y]< key[z];
- This makes it case 1, but case 3 will work the same way.
-
- The tree was ok before we added the new node.
- WLOG assume that the new node was added to T1
- It had to be T1 or T2 because x is the
ancestor of the node that was added.
- It could be either
- We know that the trees at x and y are ok so.
- let h(t) be the height of a tree t.
- h(T1) = h(T2)+1, otherwise
the tree didn't get any deeper, so there is no need to adjust the tree.
- h(T1) = h(T3), since the tree at y is
balanced, and T1 just got one node higher
- Again, if this were not the case, the tree would not be out of
balance at z.
- h(T3) = h(T4)+1, z was balanced and T3 and T4 did not change.
- h(y) = h(T4) + 2, since z is no longer balanced.
- After the rotation:
-
- h(T4) increased by 1.
- h(T3) remained the same.
- h(T2) decreased by 1.
- h(T1) decreased by 1.
- h(T3) = h(T4)
- h(T3) = h(T1)+1
- h(T1) = h(T2)+1
- So the AVL property is preserved.
- Note, h(y) is now the same as h(zold)