CS301-Lec23 handout - CS301 Data Structures Lecture No 23...

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. 23 ___________________________________________________________________ Data Structures Lecture No. 23 Reading Material Data Structures and Algorithm Analysis in C++ Chapter. 4 4.4.1, 4.4.2 Summary Single Right Rotation Single Left Rotation Double Right-Left Rotation Double Left- Right Rotation Deletion in AVL Tree Cases of Deletion in AVL Tree We demonstrated how to write the insert procedure for an AVL tree in the previous lecture. The single and double rotation call methods were also discussed at length. While inserting a node if we see that the tree is going to be unbalanced, it means that the AVL condition is being violated. We also carried out the balancing of the tree by calling single or double rotation. Now let’s see the code of these routines i.e. single and double rotation. Single Right Rotation At first, we will discuss the code of SingleRightRotation function. Its argument is TreeNode, which is given the name k2. The reason of using k2 is that it alongwtih k1 etc was used in the earlier discussion. After the rotation, this function will return a pointer, as tree will be re-organized. Resultantly, its root will be changed. Following is the code of this SingleRightRotation function. TreeNode<int>* singleRightRotation(TreeNode<int>* k2) { if( k2 == NULL ) return NULL; // k1 (first node in k2's left subtree) will be the new root TreeNode<int>* k1 = k2->getLeft(); // Y moves from k1's right to k2's left k2->setLeft( k1->getRight() ); k1->setRight(k2); // reassign heights. First k2 int h = Max(height(k2->getLeft()), height(k2->getRight())); k2->setHeight( h+1 );
Background image of page 1

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

View Full Document Right Arrow Icon
Lecture No. 23 ___________________________________________________________________ // k2 is now k1's right subtree h = Max( height(k1->getLeft()), k2->getHeight()); k1->setHeight( h+1 ); return k1; } In the function, at first, we check whether k2 is NULL. If it is NULL, the function is exited by returning NULL. If it is not NULL, the rotation process starts. The figure below depicts the single right rotation process. In this diagram, we see that k2 has been shown as the root node to this function. We see that k1 is the left subtree of k2. We are going to apply single right rotation on the link between k1 and k2. The node k1 will be the new root node after rotation. So we get its value by the following statement TreeNode <int>* k1 = k2 -> getLeft() ; Due to the single right rotation, k2 has come down, resulting in the upward movement of k1. The tree Y has to find its new place. The place of the trees X and Z remains intact. The change of place of Y is written in the code as below. k2->setLeft( k1->getRight() ); In the above statement, we get the right child of k1 (i.e. k1 -> getRight() ) i.e. Y and pass it to setLeft function of k2. Thus it becomes the left child of k2. By the statement k1 -> setRight (k2) ; We set the node k2 as right child of k1. Now after these three statements, the tree has been transformed into the following figure. k
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.

{[ snackBarMessage ]}

Page1 / 16

CS301-Lec23 handout - CS301 Data Structures Lecture No 23...

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