AVL Tree Deletion Deletion of nodes from an AVL tree is similar to that of Insertion. Insertion . But the main difference between AVL Insertion and AVL Deletion is that:
Imbalance caused in insertion can be corrected only with one rotation (single or double). In Insertion if imbalance occur then we need to find the first unbalanced node only from newly inserted node, by traversing toward Root node, and performing required rotation at that node will fix the whole AVL tree. But deletion of a node can imbalance multiple nodes in the tree (In ancestry line up-to Root node), so multiple rotations may be required and we need to traverse all the way back up-to the root node looking for imbalance nodes and performing required rotations.
AVL Tree deletion = Binary Search Tree delete (Search the node, and remove it) +
Perform required rotations if deletion cause imbalance.
We distinguish AVL tree deletion in three cases: The node to be deleted is a leaf node (external node (external node) The node to be deleted has exactly one child node. The node to be deleted has two children.
Attention Don’t confuse
if you found different case of AVL tree deletion (such as R0,
R1, R-1, L0, L1, and L-1). L-1). All All these six cases get automatically covered in above three case of AVL Tree deletion. deletion .
Case 1
The node to be deleted is a leaf node (i.e. node has no children) Deletion of even a single single leaf node from an AVL tree may imbalance multiple nodes at a time. So time. So traverse all the way back up-to Root node from parent of deleted leaf node in search of imbalance node, and perform required rotation if found any any imbalanced node.
BF =0- (2) = - 2
25
25 30 Delete 23
22
30
22
Left Rotation at node 25
30
25 40
28
40
28
23
22 35
35
50
40
28
35
50
50 Delete 28
BF =-1 - (1) = - 2
30
30
30 Delete 25
25
40
35
50
Left Rotation at node 30
40
30
35
Delete 22
50
25
40
35
50
22
40
35
50
Let us see another in which deletion of single leaf node imbalance multiple node at a time, hence multiple rotation required.
30
30 BF =1 - (-1) = 2
25
25
40
40
Delete 26
22
35
26
34
20
22
50
45
34
20
60
50
35
45
60
70
70
Right Rotation at node 25
BF =1 - (3) = - 2
40
30
30
35
22
20
50
25
34
45
Left Rotation at node 30
60
22
20 70
40
50
35
25
34
45
60
70
Still unbalanced at node 30 (BF = -2), Tree is left heavy, so perform Left rotation at node 30.
Case 2
The node to be deleted has one child node. node . Deletion of a node having one child child node may also imbalance multiple “AVL-ness” ss” property by performing required nodes at nodes at a time. So restore its “AVL-ne rotation, if get violated upon deletion.
Let us see an Example:
BF =0 - (2) = - 2
22
30
25
25 20
30
Left Rotation at node 25
30
Delete 22
20
28
27
40
28
40
27
35
40
25
20
35
28
35
27
Delete 40
30
28
30
25
20
Left-Right Rotation at node 30
27
35
35
25
20
28
27
Case 3
The node to be deleted has two children. Deletion of a node having two children may also imbalance multiple “AVL-ness” property, if get violated by nodes at a time. So restore its “AVLdeletion. Let us see an Example: