I'm trying to implement an AVL tree as a practice. For both insertion and deletion operations, my implementation does the normal BST insertion and deletion first, and then walks up the parent chain to check and fix any unbalanced subtrees. However, I'm not sure how to decide between the zig-zig and zig-zag case when the child of the unbalanced node has a balance factor of 0. I thought I could choose either one in this case, but this doesn't work for deletion.
Suppose the tree looks like this and I want to remove 78,
The rebalancing method would walk up to 70 (the root), find it unbalanced, then here comes the problem: because 44 has a balance factor of 0, should I perform the Zig-zig case on 70-44-33, or the Zig-zag case on 70-44-48 ?
The latter would fail to balance the tree. Left Rotation around 44 followed by Right Rotation around 70 would produce the following result:
On the other hand, the zig-zig case (Right Rotation around 70) is the right way to go: