CS301-Lec22 handout - CS301 Data Structures Lecture No. 22...

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

View Full Document Right Arrow Icon
CS301 – Data Structures Lecture No. 22 ___________________________________________________________________ Data Structures Lecture No. 22 Reading Material Data Structures and Algorithm Analysis in C++ Chapter. 4 4.4.2 Summary Cases of rotations Left-right double rotation to fix case 2 Right-left double rotation to fix case 3 C++ Code for avlInsert method Cases of rotations In the previous lecture, we discussed how to make insertions in the AVL tree. It was seen that due to the insertion of a node, the tree has become unbalanced. Resultantly, it was difficult to fix it with the single rotation. We have analyzed the insertion method again and talked about the α node. The new node will be inserted at the left or right subtree of the α ’s left child or at the left or right subtree of the α ’s right child. Now the question arises whether the single rotation help us in balancing the tree or not. If the new node is inserted in the left subtree of the α ’s left child or in the right subtree of α ’s right child, the balance will be restored through single rotation. However, if the new node goes inside the tree, the single rotation is not going to be successful in balancing the tree. We face four scenarios in this case. We said that in the case-1 and case-4, single rotation is successful while in the case-2 and case-3 single rotation does not work. Let’s see the tree in the diagram given below. k 1 k 2 Z X Level n Level n-1 Level n-2 k 1 k 2 Z X α α new Y new Y Single right rotation fails to fix case 2.
Background image of page 1

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

View Full DocumentRight Arrow Icon
Lecture No. 22 ___________________________________________________________________ In the above tree, we have α node as k2 , which has a left child as k1 . Whereas X and Y are its left and right children. The node k2 has a right child Z . Here the newly inserted node works as the left or right child of node Y . Due to this insertion, one level is increased in the tree. We have applied single rotation on the link of k1 and k2 . The right side tree in the figure is the post-rotation tree. The node k1 is now at the top while k2 comes down and node Y changes its position. Now if you see the levels of the node, these are seen same. Have a look on the level of the α node i.e. k1 which reflects that the difference between the left and right side levels is still 2. So the single rotation does not work here. Let’s see how we can fix that problem. A fresh look on the following diagram will help us understand the problem. Here k2 is the root node while k1 and Z are the right and left children respectively. The new node is inserted under Y so we have shown Y in a big triangle. The new node is inserted in the right subtree of k1 , increasing its level by 1. Y is not empty as the new node was inserted in it. If Y is empty, the new node will be inserted under k1 . It means that Y has a shape of a tree having a root and possibly left and right subtrees. Now view the entire tree with four subtrees connected with 3 nodes. See the diagram
Background image of page 2
Image of page 3
This is the end of the preview. Sign up to access the rest of the document.

Page1 / 17

CS301-Lec22 handout - CS301 Data Structures Lecture No. 22...

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

View Full Document Right Arrow Icon
Ask a homework question - tutors are online