Deletion in AVL TreeDeleting a node from an AVL tree is similar to that in a binary search tree. Deletion may disturb the balance factor of an AVL tree and therefore the tree needs to be rebalanced in order to maintain the AVLness. For this purpose, we need to perform rotations. The two types of rotations are L rotation and R rotation. Here, we will discuss R rotations. L rotations are the mirror images of them. If the node which is to be deleted is present in the left subtree of the critical node, then L rotation needs to be applied else if, the node which is to be deleted is present in the right subtree of the critical node, the R rotation will be applied. Let us consider that, A is the critical node and B is the root node of its left subtree. If node X, present in the right subtree of A, is to be deleted, then there can be three different situations: R0 rotation (Node B has balance factor 0 )If the node B has 0 balance factor, and the balance factor of node A disturbed upon deleting the node X, then the tree will be rebalanced by rotating tree using R0 rotation. The critical node A is moved to its right and the node B becomes the root of the tree with T1 as its left subtree. The subtrees T2 and T3 becomes the left and right subtree of the node A. the process involved in R0 rotation is shown in the following image. Example:Delete the node 30 from the AVL tree shown in the following image. SolutionIn this case, the node B has balance factor 0, therefore the tree will be rotated by using R0 rotation as shown in the following image. The node B(10) becomes the root, while the node A is moved to its right. The right child of node B will now become the left child of node A. R1 Rotation (Node B has balance factor 1)R1 Rotation is to be performed if the balance factor of Node B is 1. In R1 rotation, the critical node A is moved to its right having subtrees T2 and T3 as its left and right child respectively. T1 is to be placed as the left subtree of the node B. The process involved in R1 rotation is shown in the following image. ExampleDelete Node 55 from the AVL tree shown in the following image. Solution :Deleting 55 from the AVL Tree disturbs the balance factor of the node 50 i.e. node A which becomes the critical node. This is the condition of R1 rotation in which, the node A will be moved to its right (shown in the image below). The right of B is now become the left of A (i.e. 45). The process involved in the solution is shown in the following image. R1 Rotation (Node B has balance factor 1)R1 rotation is to be performed if the node B has balance factor 1. This case is treated in the same way as LR rotation. In this case, the node C, which is the right child of node B, becomes the root node of the tree with B and A as its left and right children respectively. The subtrees T1, T2 becomes the left and right subtrees of B whereas, T3, T4 become the left and right subtrees of A. The process involved in R1 rotation is shown in the following image. ExampleDelete the node 60 from the AVL tree shown in the following image. Solution:in this case, node B has balance factor 1. Deleting the node 60, disturbs the balance factor of the node 50 therefore, it needs to be R1 rotated. The node C i.e. 45 becomes the root of the tree with the node B(40) and A(50) as its left and right child.
Next TopicDoubly Linked List
