CS301-Lec24 handout

# CS301-Lec24 handout - CS301 Data Structures Lecture No. 24...

This preview shows pages 1–3. Sign up to view the full content.

CS301 – Data Structures Lecture No. 24 ___________________________________________________________________ Data Structures Lecture No. 24 Reading Material Data Structures and Algorithm Analysis in C++ Chapter. 4 4.4 Summary Deletion in AVL Tree Other Uses of Binary Trees Deletion in AVL Tree At the end of last lecture, we were discussing about deleting a node from an AVL tree. There are five cases to consider while deleting a node of an AVL tree. When a node is deleted, the tree can become unbalanced. We calculate the balance factor of each node and perform rotation for unbalanced nodes. But this rotation can prolong to the root node. In case of insertion, only one node’s balance was adjusted as we saw in previous lectures but in case of deletion, this process of rotation may expand to the root node. However, there may also be cases when we delete a node and perform no or one rotation only. Now, we will see the five cases of deletion. A side note is that we are not going to implement these cases in C++ in this lecture, you can do it yourself as an exercise with the help of the code given inside your text book. In this lecture, the emphasis will be on the deletion process and what necessary actions we take when a node is required to be deleted from an AVL tree. Actually, there are two kinds of actions taken here, one is deletion and the other one is the rotation of the nodes. Case 1a : The parent of the deleted node had a balance of 0 and a node was deleted in the parent’s left subtree. Page 1 of 11 Delete on this side Fig 24.1

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
CS301 – Data Structures Lecture No. 24 ___________________________________________________________________ In the left tree in the Fig 24.1 , the horizontal line inside the tree node indicates that the balance is 0, the right and left subtrees of the node are of equal levels. Now, when a node is deleted from the left subtree of this node, this may reduce by one level and cause the balance of the right subtree of the node to increase by 1 relatively. The balance of the node in favor of the right subtree is shown by a triangular knob tilted towards right. Now, the action required in this case to make the tree balanced again is: Change the balance of the parent node and stop. There is no further effect on balance of any higher node. In this case, the balance of the tree is changed from 0 to –1, which is within the defined limits of AVL tree, therefore, no rotation is performed in this case. Below is a tree in which the height of the left subtree does not change after deleting one node from it. 4 2 6 5 7 3 1 2 1 0 Fig 24.2 The node 4 is the root node, nodes 2 and 6 are on level 1 and nodes 1, 3, 5, 7 are shown on level 2. Now, if we delete the node 1, the balance of the node 2 is tilted towards right, it is –1. The balance of the root node 4 is unchanged as there is no change in the number of levels within right and left subtrees of it. Similarly, there is no change in the balances of other nodes. So we don’t need to perform any rotation operation in this case.
This is the end of the preview. Sign up to access the rest of the document.

## This note was uploaded on 05/21/2010 for the course CS CS301 taught by Professor Dr.naveed malik during the Spring '10 term at Virtual University of Pakistan.

### Page1 / 11

CS301-Lec24 handout - CS301 Data Structures Lecture No. 24...

This preview shows document pages 1 - 3. Sign up to view the full document.

View Full Document
Ask a homework question - tutors are online