# When to rebalance the AVL tree?

Consider we have an AVL tree.

Its left subtree A has disbalance coefficient has value +1 (meaning the right subtree has greater depth). Its right son has disbalance coefficient -1 (meaning left subtree has greater length).

If I understand correctly, we're looking for the most right vertex in the left subtree.

After deleting this specific vertex, what are possible cases regarding the tree rebalancing? When will it have to be rebalanced and when not?

Let $w$ be the node to be deleted

1) Perform standard BST delete for $w$.

2) Starting from $w$, travel up and find the first unbalanced node. Let $z$ be the first unbalanced node, $y$ be the larger height child of $z$, and $x$ be the larger height child of $y$. Note that the definitions of $x$ and $y$ are different from insertion here.

3) Re-balance the tree by performing appropriate rotations on the subtree rooted with $z$. There can be 4 possible cases that needs to be handled as $x$, $y$ and $z$ can be arranged in 4 ways. Following are the possible 4 arrangements:

a) $y$ is left child of $z$ and $x$ is left child of $y$ (Left Left Case)

b) $y$ is left child of $z$ and $x$ is right child of $y$ (Left Right Case)

c) $y$ is right child of $z$ and $x$ is right child of $y$ (Right Right Case)

d) $y$ is right child of $z$ and $x$ is left child of $y$ (Right Left Case)

Like insertion, following are the operations to be performed in above mentioned 4 cases. Note that, unlike insertion, fixing the node $z$ won’t fix the complete AVL tree. After fixing $z$, we may have to fix ancestors of $z$ as well (See this video if you have trouble visualizing the above mentioned cases).

Courtesy of Geeksforgeeks.

• Thank you so much. Could you please tell me also if I'm correct about this: Its left subtree A has disbalance coefficient has value +1 (meaning the right subtree has greater depth). Its right son has disbalance coefficient -1 (meaning left subtree has greater length). Does it mean that in this graph we're looking for the first left vertex that has the first right son? – Mirek Nov 13 '17 at 8:19
• If it helps, than an upvote and an accept is much appreciated. Please rephrase your comment more clearly so I can also answer that. – theSongbird Nov 13 '17 at 10:21
• Certainly, I plan on accepting it as final answer. Well, which vertex is exactly the one that we can find in the left subtree whose disbalance coeff is +1 in its right son (whose coeff is -1)? Does it even affect those cases? – Mirek Nov 13 '17 at 12:00
• @Mirek Let $T$ be you tree and $T_R$, $T_L$ be it's respective right and left subtrees. If depth($T_R$) $\gt$ depth($T_L$) and $T_L$ has a right subtree larger it's left subtree, than this falls in the $2^nd$ case mentioned in my answer: $Left-Right \ Case$. – theSongbird Nov 13 '17 at 15:44
• That's the situation that happens after deleting one vertex. I need that very situation (of course without those subtrees below it - so it's balanced) and then I delete X and I need to know to which one of these groups can it fall to then? – Mirek Nov 13 '17 at 19:04