Sei sulla pagina 1di 7

AVL TREE DELETION

Deleting a node from an AVL tree is not as easy as inserting a node into an AVL tree. Not all deletions require the tree to be rebalanced since it can improve the height difference property than it was before the deletion. Figure 1 illustrates a deletion which improves the balance properties when the node colored in red is deleted (now the height difference property of the green node is 0).

Figure 1: (Left) AVL tree before deletion (Right) AVL tree after deletion

Not all trees are like the one in Figure 1 which means that deleting a node from an AVL tree could worsen the balance factor (eg:- changing from +1 to +2 and vice versa). Therefore local re-balancing is required to fix the imbalance. Figure 2 illustrates a tree where deleting the red node creates an imbalance at the yellow node which has to be fixed.

Figure 2: (Left) AVL tree before deletion (Right) non-AVL tree(requires fix to preserve the AVL tree property)

As in insertions, to fix the imbalance occurring as a result of a deletion rotations are performed. There are eight different cases of imbalance occurring as a result of deletions and we need to examine how to fix four of these cases since the other four are exact symmetric cases. Figure 3 - 6 illustrates these eight cases of imbalance. (The value within the triangle is the height of that subtree.) The imbalance occurs at the node colored in yellow in each figure. The deletion of an AVL tree node is done using the deletion by copying technique. The main reason

SCS 1006: Introduction to Data Structures & Algorithms

Page 1

behind the use of this technique is that it does not increase the overall height of the tree.

+2

P +1 Q Q 1

h1 h1

h1 h1

Figure 3: Case 1 and Case 5 imbalance

+2

P 0 Q Q 0

h1 h

h1 h

Figure 4: Case 2 and Case 6 imbalance

+2 Q 1

P 1 R Q +1 h1

h1

h1

h1

R +1

h1

h2

h2

h1

Figure 5: Case 3 and Case 7 imbalance

SCS 1006: Introduction to Data Structures & Algorithms

Page 2

+2 Q

P 1 Q +1 h1 h1

h1

h1 1 R

R +1

h1 h1

h2

h2

Figure 6: Case 4 and Case 8 imbalance

How to balance case 1 and 5 The balance state can be obtained by rotating Q about its parent P left or right depending on the formation of the imbalance. Figure 7 illustrate how a case 1 imbalance can be fixed using a single rotation.

+2

P +1 Q LeftRotate P 0

h1 h1

h h1

h1

Figure 7: Fix to Case 1 by Left Rotation

For the case 5 imbalance the right rotation is used. Thing to note is that after the fix the height difference property in both P and Q is 0 which is way better when compared with the initial state of the imbalanced AVL tree. How to balance case 2 and 6 These two cases are extremely similar to the cases 1 and 5. In fact the only difference is the at time of imbalance node Q's height difference is 0. The balance is resurrected using the same single rotation depending on the formation. After the balancing, unlike the earlier cases the height differences of P and Q is -1 and +1 for case 2 and vice versa. Figure 8 illustrates the fix to a Case 2 imbalance.

SCS 1006: Introduction to Data Structures & Algorithms

Page 3

+2

P 0 Q LeftRotate P +1

h1 h

h h

h1

Figure 8: Fix to Case 2 by Left Rotation

How to balance case 3 and 7 This is a complex imbalance which cannot be fixed using a single rotation. Therefore as in insertion we perform the double rotation. The rotations are in opposite direction that is if the initial rotation was a left then it is followed by a right rotation. To fix the imbalance in figure 9, R is initially rotated right around its parent Q and then R is rotated left around its new parent P which will result in a balanced tree.

+2

+2

h1 1

Q 1 R

h1 h1

R +1 Q +1

h1

h1
(a)

h2

h2
(b)

h1

Figure 9: (a) State before the first rotation of case 3 (b) State after the initial rotation

0 P 0 h1 R +1 Q

h1

h2

h1

Figure 10: Fix to Case 3 (after completion of both rotations in Figure 9 (a) & (b))

SCS 1006: Introduction to Data Structures & Algorithms

Page 4

How to balance case 4 and 8 The fix for this imbalance is exactly the same as it is for cases 3 and 7. The only difference is that the height difference property of R is exact opposite to that of Q (if Q is +1 then R is -1). Figure 11 & 12 illustrates this imbalance and the fix. Observe the height difference factors of P, Q and R and compare them to the same factors in the earlier balance (case 3 and 7).

+2

+2

h1

Q 1 +1 R

h1 h1

R +2 Q 0

h2

h2

h1
(a)

h1
(b)

h1

Figure 11: (a) State before the first rotation of case 4 (b) State after the initial rotation

0 P 1 h2 R 0 Q

h1

h1

h1

Figure 12: Fix to Case 4 (after completion of both rotations in Figure 11 (a) & (b))

Lets consider the AVL tree given in figure 13 as the example. Using this tree lets try to delete the nodes 13 and 25 in that order. 20 15 10 6 8
Figure 13: AVL Tree

30 17 25 19 32 40

13

SCS 1006: Introduction to Data Structures & Algorithms

Page 5

When we delete 13 from the tree in figure 13 an imbalance occurs at node 10 as shown in yellow in figure 14 (the height differences change from -1 to -2 as result of the delete). 20 15 10 6 8
Figure 14: AVL imbalance at node 10

30 17 19 25 32 40

This is a case 7 imbalance (requires double rotations). Therefore to fix this we need to left rotate 8 around its parent which is 6. Resulting tree is given in figure 15. 20 15 10 8 6
Figure 15: AVL tree after the left rotation of 8 around 6

30 17 19 25 32 40

Now we have reduced case 7 into a simpler case which is case 6. To fix case 6 we only need to right rotate 8 around its parent 10. The resulting tree is given in figure 16 with the AVL tree properties resurrected. Always keep note that the first rotation of the double rotation is performed to reduce the complex cases of 3,4,7 and 8 to the simpler cases of 1,2,5 and 6. So the second rotation is the standard balancing step where as the first rotation is the simplification step.

SCS 1006: Introduction to Data Structures & Algorithms

Page 6

Double rotation

First Rotation:- Reduce the complex case to a simpler case Second Rotation:- The balancing step (simpler cases only need this step thus called single rotation) 20 15 8 6 10 17 19 25 32 30 40

Figure 16: Final tree after the double rotation

Deleting the node 25 from the above three results in an imbalance at node 30 (height difference of the node 30 changes from +1 to +2). This is a case 3 imbalance which is an exact mirror image of the case 7. So we need double rotations to fix the imbalance as mentioned above. This time the first rotation is a right rotation followed by a left rotation to balance the tree. The final tree after deleting the node 25 is given in figure 17. 20 15 8 6 10 17 19 30 32 40

Figure 17: AVL tree after the deletion of node 25.

Read Drozdek pages 247 248 (edition 1) **********************************

SCS 1006: Introduction to Data Structures & Algorithms

Page 7

Potrebbero piacerti anche