{"id":320,"date":"2018-07-19T05:43:57","date_gmt":"2018-07-19T05:43:57","guid":{"rendered":"http:\/\/csp01.epgpbooks.inflibnet.ac.in\/?post_type=chapter&#038;p=320"},"modified":"2018-12-12T10:34:18","modified_gmt":"2018-12-12T10:34:18","slug":"balanced-binary-search-trees-and-avl-trees","status":"publish","type":"chapter","link":"https:\/\/ebooks.inflibnet.ac.in\/csp01\/chapter\/balanced-binary-search-trees-and-avl-trees\/","title":{"rendered":"Balanced Binary Search Trees and AVL Trees"},"content":{"raw":"<div><span style=\"float: right\"><a href=\"https:\/\/youtu.be\/sCV5g9IoPK0\" target=\"_blank\" rel=\"noopener\"><img src=\"http:\/\/epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/2018\/11\/download.png\" alt=\"epgp books\" width=\"75px\" height=\"75px;\" \/><\/a>\r\n<\/span><\/div>\r\n<div>\r\n\r\n&nbsp;\r\n\r\n&nbsp;\r\n\r\n&nbsp;\r\n<p style=\"text-align: justify\">Welcome to the e-PG Pathshala Lecture Series on Data Structures. We have understood the basic concept of an important ADT \u2013 the Tree ADT and also the special type of tree the binary search tree. In this module we will discuss one very important type of binary search tree that is the balanced binary search tree and one example of balanced binary search tree namely the AVL trees.<\/p>\r\n&nbsp;\r\n\r\n<strong>Learning Objectives<\/strong>\r\n\r\n&nbsp;\r\n\r\nThe learning objectives of the module are as follows:\r\n\r\n&nbsp;\r\n\r\n\u2022\u00a0 To understand the concept of Balanced Binary Search Trees\r\n\r\n\u2022\u00a0 To discuss the properties of AVL Trees\r\n\r\n\u2022\u00a0 To describe\u00a0 Single Rotation of AVL Trees\r\n\r\n\u2022\u00a0 To discuss the Double Rotation of AVL Trees\r\n\r\n&nbsp;\r\n\r\n<strong>23.1\u00a0 Introduction to Balanced Binary Search Trees<\/strong>\r\n\r\n&nbsp;\r\n<p style=\"text-align: justify\">As a tree becomes more unbalanced, running time of the search operation increases from O(log n) to O(n) because as the tree gets more unbalanced the shape starts becoming a list. Binary Search Trees and important data structure used in many applications needing the search operation are fast if they\u2019re shallow or in other words the tree is a complete tree. The disadvantage of an unbalanced binary search tree is that its height can be as large as N-1. This essentially means that when one branch is much longer than the other, the time needed to perform insertion, deletion and search operations is much more.Now we need to define the concept of a \u201csort of\u201d complete tree where by \u201csort of\u201d we mean a binary search tree that is as balanced as possible. In other we want to keep the binary search tree balanced as nodes are added\/removed, so searching\/insertion remain efficient.<\/p>\r\n&nbsp;\r\n<p style=\"text-align: justify\">A class of binary search trees is said to be <strong>balanced<\/strong>, if each of the three dictionary operations searching, inserting and deleting of keys for a tree with <em>n<\/em> keys can always (in the worst case) be carried out in <em>O<\/em>(log <em>n<\/em>) steps.<\/p>\r\n&nbsp;\r\n\r\n<strong>23.1.1 Approaches to Balancing Trees<\/strong>\r\n\r\n&nbsp;\r\n<p style=\"text-align: justify\">There are many approaches that can be adopted for balancing trees. The first and simplest method is we do not do balancing. However in this case we may end up\u00a0<span style=\"font-size: 1em;text-align: initial\">with some nodes being very deep. The other extreme of the simple policy is to main a strict balance that is the tree must always be balanced perfectly. The next approach is what is called pretty good balance where we allow a little out of balance. The fourth and final approach is adjusting the balance during access \u2013 the so called self-adjusting trees.<\/span><\/p>\r\n&nbsp;\r\n<p style=\"text-align: justify\"><strong style=\"text-align: initial;font-size: 1em\">23.2 Balanced Binary Search Trees<\/strong><\/p>\r\n&nbsp;\r\n<p style=\"text-align: justify\"><span style=\"font-size: 1em\">There are two basic kinds of balanced binary search trees. <\/span><strong style=\"font-size: 1em\">Height balancedtrees<\/strong><span style=\"font-size: 1em\"> wherewe ensure that the height of siblings are \u201capproximately the same\u201d. The other type is the <\/span><strong style=\"font-size: 1em\">Weight-balancedtrees<\/strong><span style=\"font-size: 1em\"> where we ensure that the number of descendants of sibling nodes are \u201capproximately the same\u201d. \u201cTree rotations\u201d are used to maintain balance on insert\/delete<\/span><\/p>\r\n\r\n<\/div>\r\n<div>\r\n\r\n&nbsp;\r\n<p style=\"text-align: justify\">Now let us define the concept of balance - A <em>binary tree<\/em> is <strong><em>balanced<\/em><\/strong>(here unless otherwise specified balanced implies height balanced) if the difference in height between any node\u2019s left and right subtree is \u00a3 1.<\/p>\r\n&nbsp;\r\n<p style=\"text-align: center\"><strong>balance = height(left subtree) - height(right subtree)<\/strong><\/p>\r\n&nbsp;\r\n<p style=\"text-align: justify\">By convention the height of a \u201cnull\u201d subtree is assumed to be -1. If the balance is zero everywhere then the tree is said to be perfectly balanced. If the balance is small everywhere that is at each and every node then the tree is balanced enough to ensure operations will take time of the O<strong>(log n).<\/strong> This is because the maximum depth of the tree can be only 1.44 log n.<\/p>\r\n&nbsp;\r\n\r\n<img class=\"alignnone size-full wp-image-323 aligncenter\" src=\"http:\/\/csp01.epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/sites\/45\/2018\/07\/1-183.png\" alt=\"\" width=\"302\" height=\"227\" \/>\r\n<p style=\"text-align: justify\"><strong>Perfect Balance <\/strong>is said to occur if we want a complete tree after every operation. This means that the tree is full except possibly in the lower right. However maintaining perfect balance is expensive. For example (Figure 23.2), insert 2 in the tree on the left and then rebuild as a complete tree<\/p>\r\n&nbsp;\r\n\r\n<img class=\"alignnone size-full wp-image-324 aligncenter\" src=\"http:\/\/csp01.epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/sites\/45\/2018\/07\/1-184.png\" alt=\"\" width=\"576\" height=\"164\" \/>\r\n\r\n<strong style=\"text-align: initial;font-size: 1em\">23.2.1 Prevent the degeneration of the Binary Search Tree (BST) :<\/strong>\r\n\r\n<\/div>\r\n<div>\r\n\r\n&nbsp;\r\n<p style=\"text-align: justify\">A BST can be set up to maintain balance during every update operation typically insertions and removals. Our objective is to keep the height of a binary search tree O(log N) . This allows us to achieve a worst-case runtime of O(log n) for searching, inserting and deleting.<\/p>\r\n&nbsp;\r\n\r\nTypes of BST which maintain the optimal performance are the following:\r\n<ul>\r\n \t<li><strong>AVL trees<\/strong><\/li>\r\n \t<li><strong>Red-Black trees<\/strong><\/li>\r\n \t<li><strong>Splay trees<\/strong><\/li>\r\n<\/ul>\r\n&nbsp;\r\n<p style=\"text-align: justify\"><strong>AVL Trees<\/strong>are BSTs that maintain<strong> <em>height balance.<\/em> <\/strong>For for each node, the difference in height of its two subtrees is in the range -1 to 1<\/p>\r\n&nbsp;\r\n<p style=\"text-align: justify\"><strong>Red<\/strong>-<strong>black <\/strong>tree is a binary version of a 2-3-4 tree. Here the the nodes have a 'color' attribute: <strong>BLACK<\/strong> or <strong>RED. The tree<\/strong> maintains a balance measure called the <strong><em>BLACK<\/em><\/strong> <strong><em>height<\/em><\/strong><\/p>\r\n&nbsp;\r\n<p style=\"text-align: justify\">A <strong>splay tree<\/strong> are self-adjusting type of BST with the additional property that <strong>recently<\/strong> <strong>accessed elements are quick to be access again<\/strong>. The basic operations such as insertion, look-up and removal takes place in O(log n) amortized time. Insert\/find always rotates node <em>to the root<\/em>.<\/p>\r\n&nbsp;\r\n\r\n<strong>23.3AVL Tree<\/strong>\r\n\r\n&nbsp;\r\n<p style=\"text-align: justify\">We will first discuss an AVL tree which is essentially a binary search tree with a <em>balance <\/em>condition. AVL is named after its inventors: <strong>A<\/strong>del\u2019son-<strong>V<\/strong>el\u2019skii and <strong>L<\/strong>andis. The AVL tree <strong><em>approximates<\/em><\/strong> <strong>the ideal tree<\/strong> (completely balanced tree) since the AVL Tree maintains a height close to the minimum (Figure 23.3)However It is not a requirement that all leaves of an AVL tree be on the same or adjacent level.<\/p>\r\n&nbsp;\r\n\r\n<img class=\"alignnone size-full wp-image-325 aligncenter\" src=\"http:\/\/csp01.epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/sites\/45\/2018\/07\/1-185.png\" alt=\"\" width=\"617\" height=\"237\" \/>\r\n\r\n&nbsp;\r\n\r\n<strong>23.3.1 AVL - Good but not Perfect Balance<\/strong>\r\n\r\n<\/div>\r\n<div>\r\n\r\n&nbsp;\r\n<p style=\"text-align: justify\">As we have already discussed AVL tree is a binary search tree that has an additional <strong><em>height constraint<\/em><\/strong>:For each node x in the tree, Height(x.left) differs from Height(x.right) by at most 1. If this height constraint is satisfied then the height of the tree is O(log n).<\/p>\r\n&nbsp;\r\n<p style=\"text-align: justify\">As we already discussed the Balance factor of a node which is defined below:<\/p>\r\n&nbsp;\r\n\r\n<strong>height(left sub-tree) - height(right sub-tree)<\/strong>\r\n\r\n&nbsp;\r\n<p style=\"text-align: justify\">The AVL tree has balance factor calculated at every node, where for every node, the heights of the left and right sub-trees can differ by no more than 1. Each node storesthe current heights at each node. The height of an AVL tree storing n keys is O(log n).<\/p>\r\n&nbsp;\r\n<p style=\"text-align: justify\">The height of a node is now specified. The height of a leaf is 0. The height of a null tree is assumed to be -1. In general, the height of an internal node is the maximum height of its children plus 1 (Figure 23.4). The height of any node is the maximum of the heights of it\u2019s two sub-trees.Maximum of (H-1, H-2) + 1 = H-1+1 =H<\/p>\r\n&nbsp;\r\n\r\n<img class=\"alignnone size-full wp-image-326 aligncenter\" src=\"http:\/\/csp01.epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/sites\/45\/2018\/07\/1-186.png\" alt=\"\" width=\"580\" height=\"639\" \/>\r\n\r\n<\/div>\r\n<div>\r\n<p style=\"text-align: center\"><strong>Figure 23.5 Example of an AVL Tree with Heights shown<\/strong><\/p>\r\n&nbsp;\r\n<p style=\"text-align: justify\">An example of an AVL tree where the heights are shown next to the nodes is given in Figure 23.5. Now remember an AVL tree is a binary search tree. The height of the left and right sub-trees of the root differ by at most 1. Now each left and right sub-trees are themselves AVL trees. An AVL Tree T is a binary search tree such that for every internal node v of T, the heights of the children of v can differ by at most 1<em>.<\/em><\/p>\r\n&nbsp;\r\n\r\n<strong>To sum up :<\/strong>\r\n\r\n&nbsp;\r\n<p style=\"text-align: justify\">Height of empty node = -1, Height of leaf = 0 and Height of a node = Max (Height of left subtree, Height of right subtree) +1<\/p>\r\n&nbsp;\r\n\r\n<strong>23.3.2 Balance Factor of AVL Trees<\/strong>\r\n\r\n&nbsp;\r\n<p style=\"text-align: justify\">For each AVL tree node, the difference between the heights of its left and right sub-trees is either -1, 0 or +1 is called thebalance factor of a node. The balanceFactor = height(left sub-tree) \u2013 height(right sub-tree). If balanceFactor &gt; 1 or &lt; -1 then the tree is too unbalanced, and needs 'rearranging' to make it more balanced. When we have a positive balance factor the node is said to be \"heavy on the left\u201c that is the height of the left sub-tree is greater than the height of the right sub-tree. When we have negative balance factor, the node is \"heavy on the right\u201c. Figure 23.6 shows an example where the heights and balance factors are shown next to the nodes<\/p>\r\n&nbsp;\r\n\r\n<img class=\"alignnone size-full wp-image-327 aligncenter\" src=\"http:\/\/csp01.epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/sites\/45\/2018\/07\/1-187.png\" alt=\"\" width=\"453\" height=\"328\" \/>\r\n\r\n&nbsp;\r\n<p style=\"text-align: justify\">Now the balance factor of an AVL tree can be either 0, +1 or -1. Due insertions or deletions the balance factor can change from 0 to +1 or -1. In this case there is no imbalance. When the balance factor changes from +1\/-1 to +2\/-2 then comesthe need to rebalance so that the balance factor is back to 0, +1 or -1.<\/p>\r\n&nbsp;\r\n\r\n<strong>23.4 Properties of AVL Trees<\/strong>\r\n\r\n&nbsp;\r\n<p style=\"text-align: justify\">The depth of a typical node in an AVL tree approaches the optimal value possible which is <em>log N<\/em>.Consequently, all searching operations in an AVL tree have logarithmic worst-case bounds.An update (insert or remove) in an AVL tree could\u00a0<span style=\"font-size: 1em;text-align: initial\">destroy the balance. Therefore we go about rebalancing before before the operation can be considered complete. This time taken for rebalancing during update operation is the price we pay for making the search operation faster. It is to be noted that after\u00a0<\/span><span style=\"text-align: initial;font-size: 1em\">an insertion, only nodes that are on the path from the insertion point to the root can have their balances altered. This affects the way we carry out rebalancing.<\/span><\/p>\r\n\r\n<\/div>\r\n<div>\r\n\r\n&nbsp;\r\n\r\n<strong>23.5 Rotations<\/strong>\r\n\r\n&nbsp;\r\n<p style=\"text-align: justify\">Insertion or deletion operation involves adding or deleting only a single node at a time. This essentially means that the height of some sub-tree can change by at most 1.\u00a0 If the AVL tree property is violated at a node x, it means that the heights of left(x) and right(x) differ by exactly 2. Now rotations will have to be applied to x to restore the AVL tree property.The rotation is generally a O(1) operation.<\/p>\r\n&nbsp;\r\n<p style=\"text-align: justify\">In order to restore the balance of an unbalanced tree, three adjacent nodes are involved in the rotation. The deepest unbalanced node, is the node that requires rotation to rebalance the tree.This node is either the ancestor of a deleted node (in case of deletion) or of the inserted node (in case of insertion) andis the node whose balance factor has changed to -2 or +2. This is shown using an example of insertion in Figure 23.7. Here the element 35 is inserted. Therefore the three nodes that take part in the rotation are the deepest unbalanced node (here 45), the ancestor of the inserted node (40) and the inserted node (35).<\/p>\r\n&nbsp;\r\n\r\n<img class=\"alignnone size-full wp-image-328 aligncenter\" src=\"http:\/\/csp01.epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/sites\/45\/2018\/07\/1-188.png\" alt=\"\" width=\"621\" height=\"533\" \/>\r\n<div>\r\n\r\n&nbsp;\r\n\r\n<strong>23.5.1 BST Ordering Property after a Rotation<\/strong>\r\n\r\n&nbsp;\r\n<p style=\"text-align: justify\">A rotation does not affect the ordering property of a BST (binary search tree). This is explained in Figure 23.8. Let us assume a e \u03b1, b e \u03b2 and c eg. This implies that a \u2264 A \u2264 b \u2264 B \u2264 c according to the BST tree property. It can be seen that even after rotation the same property is maintained. We will explain the actual procedure of rotation in the next section.<\/p>\r\n&nbsp;\r\n\r\n<strong>23.5.2 Concept of Rotation and BST Property<\/strong>\r\n\r\n&nbsp;\r\n\r\n<img class=\"alignnone size-full wp-image-329 aligncenter\" src=\"http:\/\/csp01.epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/sites\/45\/2018\/07\/1-189.png\" alt=\"\" width=\"503\" height=\"177\" \/>\r\n\r\n&nbsp;\r\n<p style=\"text-align: justify\">Now let us discuss rotation in terms of subtrees. BST ordering property requirement means that on the left hand side tree T1 &lt; x &lt; y,x&lt; T2&lt; y andx &lt; y &lt; T3. Now we carry out a right rotation of x about y (Figure23.9). This means that the following changes are made:<\/p>\r\n\r\n<ul>\r\n \t<li style=\"text-align: justify\">y which was the parent with x as left child (x&lt;y) before rotation now becomes the right child of parent x retaining the x&lt;y property.<\/li>\r\n \t<li style=\"text-align: justify\">T2 which was the right sub-treeof x (x&lt;T2&lt;y) before rotation now becomes the left sub-tree of y still maintaining the same relation.<\/li>\r\n \t<li style=\"text-align: justify\">Therefore after rotation the same property T1 &lt; x &lt; y, x &lt; T2&lt; y and x &lt; y &lt; T3is maintained.<\/li>\r\n<\/ul>\r\nThough we have seen an example of right rotation, the same is true for left rotation.\r\n\r\n&nbsp;\r\n\r\n<strong>23.6\u00a0 Different Cases of Rotations<\/strong>\r\n\r\n&nbsp;\r\n<p style=\"text-align: justify\">There are basically two types of rotations based on whether we carry out single or double rotations. We also classify rotations based on where and on which sub-tree (left or right) the violation or the imbalance occurs. Here we also use the term outside grandchild that is the left grandchild of a left child (left-left) or the right grandchild of a right child (right-right). We have an inside grandchild when we have left grandchild of a right child (left-right) or the right grandchild of a left child (right-left). Therefore considering these aspects we have the following four cases of rotations:<\/p>\r\n&nbsp;\r\n\r\nViolation may occur during an insertion into\r\n\r\n<strong>Case1. <\/strong>left sub-tree of left child<strong> (Single Right Rotation)<\/strong>\r\n\r\n<strong>Case 2. <\/strong>right sub-tree of right child<strong> (Single Left Rotation)<\/strong>\r\n\r\n<strong>Case 3. <\/strong>right sub-tree of left child<strong> (Double Left \u2013 Right Rotation)<\/strong>\r\n\r\n<strong>Case 4. <\/strong>left sub-tree of right child<strong> (Double Right \u2013 Left Rotation)<\/strong>\r\n\r\n&nbsp;\r\n\r\n<strong>23.6.1 Single Rotation<\/strong>\r\n\r\n<\/div>\r\n<div>\r\n<p style=\"text-align: justify\">The simplest type of rotation is single rotation. Case 1 and case 2 are both cases of single rotation. Single rotation switches the roles of the parent and child while maintaining the search order. Here we rotate between a node and its child. While the child becomes parent, parent becomes right child in case 1 and left child in case 2. The result is a binary search tree that satisfies the AVL property. Now let us discuss in detail the above two cases.<\/p>\r\n&nbsp;\r\n\r\n<strong>23.6.2 <\/strong><strong>Single Right Rotation<\/strong>\r\n\r\n<\/div>\r\n<img class=\"alignnone size-full wp-image-330 aligncenter\" src=\"http:\/\/csp01.epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/sites\/45\/2018\/07\/1-190.png\" alt=\"\" width=\"599\" height=\"532\" \/>\r\n<div>\r\n\r\n<img class=\"alignnone size-full wp-image-331 aligncenter\" src=\"http:\/\/csp01.epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/sites\/45\/2018\/07\/1-191.png\" alt=\"\" width=\"600\" height=\"284\" \/>\r\n<p style=\"text-align: center\"><strong>(c )<\/strong>\r\n<strong>Figure 23.10 Single Right Rotation<\/strong><\/p>\r\n&nbsp;\r\n<p style=\"text-align: justify\"><span style=\"text-align: justify;font-size: 1em\">Figure 23.10 shows the details of case 1. In this case the left sub-tree of the left child of X violates the property. In Figure 23.10 (a) this means sub-tree A, the left sub-tree of the left child Y of X violates the property. We need to carry out single right rotation of X about Y where X now becomes the right child of Y, and the right sub-tree of Y becomes the left sub-tree of X.<\/span><\/p>\r\n\r\n<\/div>\r\n<div>\r\n\r\n&nbsp;\r\n\r\nFigure 23.10 (b) shows the three steps namely\r\n\r\n&nbsp;\r\n\r\n\u2022\u00a0 the left child x of a node y becomes y's parent.\r\n\r\n\u2022\u00a0 y becomes the right child of x.\r\n\r\n\u2022\u00a0 The right subtree T2 of x, if any, becomes the left child of y\r\n\r\n&nbsp;\r\n<p style=\"text-align: justify\">Here the notation ((T1+T2)+T3) indicates that the subtrees T1 and T2 are children of the same parent and that the combined subtree of (T1+T2) and subtree T3 are children of the root. Moreover the sub-trees change positions from ((T1+T2) + T3) before the rotation to positions (T1+(T2+T3)) after the rotation still maintaining the BST property.<\/p>\r\n&nbsp;\r\n\r\nFigure 23.10 (c) shows the steps more in detail.\r\n<ul>\r\n \t<li style=\"text-align: justify\">The node k2 is the node at which the imbalance occurs and the balance factor is violated since the height of subtree c has height difference of 2 with the subtree A.<\/li>\r\n \t<li>The left child of k2 is given by k1=k2.left.<\/li>\r\n \t<li>The new left sub-tree of k2 is now is k1\u2019s right sub-tree (B) that is k2.left=k1.right<\/li>\r\n \t<li>The new right sub-tree of k1 is k2 that is k1.right=k2<\/li>\r\n \t<li>\u00a0Now we return the root of the new balanced tree that is k1.<\/li>\r\n<\/ul>\r\nThe algorithm is given below in Figure 23.10 (d) (notations from Figure 23.10 (b))\r\n<table style=\"border-collapse: collapse;width: 100%\" border=\"1\">\r\n<tbody>\r\n<tr>\r\n<td style=\"width: 100%\">\r\n<div>\r\n\r\n<strong>private TreeNode rightRotate(TreeNode oldParent) {<\/strong>\r\n\r\n&nbsp;\r\n\r\n\/\/ 1. detach left child's (x\u2019s) right subtree (T2)\r\n\r\n&nbsp;\r\n\r\n<strong>TreeNode orphan= oldParent.left.right;<\/strong>\r\n\r\n&nbsp;\r\n\r\n\/\/ 2. consider left child (x) to be the new parent\r\n\r\n&nbsp;\r\n\r\n<strong>TreeNode newParent = oldParent.left;<\/strong>\r\n\r\n&nbsp;\r\n\r\n\/\/ 3. attach old parent (y) onto right of new parent (x)\r\n\r\n&nbsp;\r\n\r\n<strong>newParent.right = oldParent;<\/strong>\r\n\r\n&nbsp;\r\n\r\n\/\/\u00a0 4. attach new parent's (x\u2019s) right subtree (T2)as\r\n\r\n&nbsp;\r\n\r\n\/\/\u00a0\u00a0\u00a0\u00a0\u00a0 left subtree of old parent (y)\r\n\r\n&nbsp;\r\n\r\n<strong>oldParent.left = orphan;<\/strong>\r\n\r\n&nbsp;\r\n\r\n<strong>oldParent.height = height(oldParent); <\/strong>\/\/ update nodes'\r\n\r\n<strong>newParent.height = height(newParent); <\/strong>\/\/ height values\r\n\r\n<\/div>\r\n<div>\r\n\r\n<strong>return newParent;<\/strong>\r\n\r\n<strong>}<\/strong>\r\n\r\n<\/div><\/td>\r\n<\/tr>\r\n<\/tbody>\r\n<\/table>\r\n<\/div>\r\n<div>\r\n<p style=\"text-align: center\"><strong>Figure 23.10 (d) Algorithm for Single Right Rotation 23.6.3 Single Left Rotation<\/strong><\/p>\r\n&nbsp;\r\n\r\n<img class=\"alignnone size-full wp-image-332 aligncenter\" src=\"http:\/\/csp01.epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/sites\/45\/2018\/07\/1-192.png\" alt=\"\" width=\"635\" height=\"568\" \/>\r\n\r\n<img class=\"alignnone size-full wp-image-333 aligncenter\" src=\"http:\/\/csp01.epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/sites\/45\/2018\/07\/1-193.png\" alt=\"\" width=\"605\" height=\"315\" \/>\r\n\r\n<\/div>\r\n<div>\r\n<p style=\"text-align: center\"><strong>Figure 23.11 Single Left Rotation<\/strong><\/p>\r\n&nbsp;\r\n<p style=\"text-align: justify\">Figure 23.11 shows the details of case 2. In this case the right sub-tree of the right child of X violates the property.In Figure 23.11 (a) this means sub-tree A, the right sub-tree of the right child Y of X violates the property. We need to carry out single left rotation of Y about X where X now becomes the left child of Y, and the left sub-tree of Y becomes the right sub-tree of X.<\/p>\r\n&nbsp;\r\n\r\nFigure 23.11 (b) shows the three steps namely\r\n\r\n&nbsp;\r\n\r\n\u2022 The right child y of a node x becomes x's parent.\r\n\r\n\u2022 x becomes the left child of y.\r\n\r\n\u2022 The left child T2 of y, if any, becomes the right child of x\r\n<p style=\"text-align: justify\">Moreover the sub-trees change positions from (T1 + (T2 + T3)) before the rotation to positions ((T1 + T2) + T3)after the rotation still maintaining the BST property.<\/p>\r\n&nbsp;\r\n\r\nFigure 23.10 (c) shows the steps more in detail.\r\n<ul>\r\n \t<li style=\"text-align: justify\">The node k1 is the node at which the imbalance occurs and the balance factor is violated where the height of subtree c differs with the height of subtree A by more than one.<\/li>\r\n \t<li>The right child of k2 is given by k2=k1.right.<\/li>\r\n \t<li>The new right sub-tree of k1 is now is k2\u2019s left sub-tree (B) that is k1.right=k2.left (B)<\/li>\r\n \t<li>The new left sub-tree of k2 is k1 that is k2.left=k1<\/li>\r\n \t<li>Now we return the root of the new balanced tree that is k2.<\/li>\r\n \t<li>The algorithm is given below in Figure 23.11 (d) (notations from Figure 23.11 (b))<\/li>\r\n<\/ul>\r\n<table style=\"border-collapse: collapse;width: 100%\" border=\"1\">\r\n<tbody>\r\n<tr>\r\n<td style=\"width: 100%\"><strong>private TreeNode leftRotate(TreeNode oldParent) {<\/strong>\r\n\r\n&nbsp;\r\n\r\n\/\/ 1. detach right child's (y) left subtree (T2)\r\n\r\n&nbsp;\r\n\r\n<strong>TreeNode orphan = oldParent.right.left;<\/strong>\r\n\r\n&nbsp;\r\n\r\n\/\/ 2. consider right child (y) to be the new parent\r\n\r\n&nbsp;\r\n\r\n<strong>TreeNode newParent = oldParent.right;<\/strong>\r\n\r\n&nbsp;\r\n\r\n\/\/ 3. attach old parent (x) onto left of new parent (y)\r\n\r\n&nbsp;\r\n\r\n<strong>newParent.left = oldParent;<\/strong>\r\n\r\n&nbsp;\r\n\r\n\/\/\u00a0 4. attach new parent's old left subtree (T2) \/\/asright subtree of old parent (x)\r\n\r\n&nbsp;\r\n\r\n<strong>oldParent.right = orphan;<\/strong>\r\n\r\n&nbsp;\r\n\r\n<strong>oldParent.height = height(oldParent); \/\/ update nodes' newParent.height = height(newParent); \/\/ height values return newParent;<\/strong>\r\n\r\n&nbsp;\r\n\r\n<strong>}<\/strong><\/td>\r\n<\/tr>\r\n<\/tbody>\r\n<\/table>\r\n&nbsp;\r\n<p style=\"text-align: center\"><strong>Figure 23.11 (d) Algorithm for Single Left Rotation<\/strong><\/p>\r\n\r\n<\/div>\r\n<div>\r\n\r\n&nbsp;\r\n\r\n<strong>23.6.4 Double Rotations<\/strong>\r\n\r\n&nbsp;\r\n<p style=\"text-align: justify\">The other two cases of rotation are cases of two single rotations. When a new item is added to the sub-tree for an inside grandchild (left-right or right-left)<strong>,<\/strong> the imbalance is fixed with a double right or left rotation.<\/p>\r\n&nbsp;\r\n\r\n<strong>23.6.5 Left Right Double Rotation<\/strong>\r\n\r\n&nbsp;\r\n<p style=\"text-align: justify\">When we have the situation where we have a left inside grandchild (left-right) case we have left right double rotation. A left-right double rotation is equivalent to a sequence of two single rotations where the first rotation on the original tree is a <em>left<\/em> rotation between X\u2019s left-child and grandchild and the second rotation on this new tree is a <em>right<\/em> rotation between X and its new left child.<\/p>\r\n&nbsp;\r\n\r\n<img class=\"alignnone size-full wp-image-334 aligncenter\" src=\"http:\/\/csp01.epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/sites\/45\/2018\/07\/1-194.png\" alt=\"\" width=\"577\" height=\"538\" \/>\r\n\r\n&nbsp;\r\n\r\n<img class=\"alignnone size-full wp-image-335 aligncenter\" src=\"http:\/\/csp01.epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/sites\/45\/2018\/07\/1-195.png\" alt=\"\" width=\"569\" height=\"345\" \/>\r\n\r\n&nbsp;\r\n\r\n<span style=\"text-align: justify;font-size: 1em\">Figure 23.12 shows the details of case 3. In this case the right sub-tree of the left child of X violates the property. First we need to rotate the tree LEFT about X\u2019s left child (Y) and grandchild (Z) as shown in Figure 23.12 (a) similar to single left rotation. Now we need to carry out the single right rotation of z about x in the modified tree that is we rotate the tree RIGHT about X and its new left child (Z) (Figure 23.12.(b)).<\/span>\r\n\r\n<\/div>\r\n<\/div>\r\n<div>\r\n\r\n&nbsp;\r\n\r\nFigure 23.12 (c) shows the steps more in detail.\r\n<ul>\r\n \t<li style=\"text-align: justify\">The first pivot for the left rotation is the left child (v) of the deepest unbalanced node (x).<\/li>\r\n \t<li style=\"text-align: justify\">We carry out Single Left Rotation of w about the first pivot v<\/li>\r\n \t<li style=\"text-align: justify\">Moreover during this single left rotationthe sub-trees change positions from ((T1 + ((T2+T3) + T4) before the first rotation to positions (((T1 + T2)+T3) + T4) after the rotation still maintaining the BST property.<\/li>\r\n \t<li style=\"text-align: justify\">The second pivot for the right rotation is the deepest unbalanced node itself x.<\/li>\r\n \t<li style=\"text-align: justify\">We carry out Single Right Rotation of w about x<\/li>\r\n \t<li style=\"text-align: justify\">Moreover during this single right rotation the sub-trees change positions from (((T1 + T2)+T3) + T4) before the first rotation to positions ((T1 + T2)+ (T3+ T4)) after the rotation still maintaining the BST property.<\/li>\r\n<\/ul>\r\n&nbsp;\r\n\r\n<strong>23.6.6 Right-Left Double Rotation<\/strong>\r\n\r\n&nbsp;\r\n<p style=\"text-align: justify\">When we have the situation where we have a right inside grandchild (right-left) then we have right left double rotation. A right-left double rotation is equivalent to a sequence of two single rotations where the first rotation on the original tree is a right rotation between X\u2019s right-child and grandchild and the second rotation on this new tree is a <em>left<\/em> rotation between X and its new right child.<\/p>\r\n\r\n<\/div>\r\n<img class=\"alignnone size-full wp-image-336 aligncenter\" src=\"http:\/\/csp01.epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/sites\/45\/2018\/07\/1-196.png\" alt=\"\" width=\"582\" height=\"300\" \/>\r\n<div>\r\n<p style=\"text-align: justify\">Figure 23.13 shows the details of case 4. The left sub-tree of the right child of X violates the property. First we need to rotate the tree RIGHT about X\u2019s rightchild (Y) and grandchild (Z) as shown in Figure 23.13 (a) similar to single right rotation. Now we need to carry out the single left rotation of z about x in the modified tree that is we rotate the tree LEFT about X and its new rightchild (Z) (Figure 23.13 (b)).<\/p>\r\n&nbsp;\r\n\r\n<img class=\"alignnone size-full wp-image-337 aligncenter\" src=\"http:\/\/csp01.epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/sites\/45\/2018\/07\/1-197.png\" alt=\"\" width=\"572\" height=\"586\" \/>\r\n\r\n&nbsp;\r\n\r\n<\/div>\r\n&nbsp;\r\n\r\n<strong>Figure 23.13 Right Left Double Rotation <\/strong>Figure 23.13 (c) shows the steps more in detail.\r\n<ul>\r\n \t<li>The first pivot for the right rotation is right child (w) of the deepest unbalanced node (x)<\/li>\r\n \t<li>We carry out Single Right Rotation of v about the first pivot w<\/li>\r\n \t<li style=\"text-align: justify\">Moreover during this single left rotation the sub-trees change positions from (T1 + ((T2+T3) + T4)) before the first rotation to positions (T1 + (T2 (T3 + T4)) after the rotation still maintaining the BST property.<\/li>\r\n \t<li>The second pivot for the right rotation is the deepest unbalanced node itself x.<\/li>\r\n \t<li>We carry out Single Left Rotation of v about x<\/li>\r\n \t<li style=\"text-align: justify\">Moreover during this single right rotation the sub-trees change positions from (T1 + (T2 (T3 + T4)) before the first rotation to positions ((T1 + T2) + (T3 + T4)) after the rotation still maintaining the BST property.<\/li>\r\n<\/ul>\r\n<p style=\"text-align: justify\">The algorithms for the double rotations are sequential call to the two appropriate single rotations with the appropriate nodes. Therefore we have seen all the possible four cases of violation and the rebalancing through rotations.<\/p>\r\n&nbsp;\r\n\r\n<strong>Summary<\/strong>\r\n<ul>\r\n \t<li>Explained the concept of balancing and Balanced Binary Search Trees<\/li>\r\n \t<li>Discussed the properties of AVL Trees<\/li>\r\n \t<li>Described Single Rotation of AVL Trees<\/li>\r\n \t<li>Discussed Double Rotation of AVL Trees<\/li>\r\n<\/ul>\r\n<table>\r\n<tbody>\r\n<tr>\r\n<td><strong>you can view video on Balanced Binary Search Trees and AVL Trees<\/strong><\/td>\r\n<td><a href=\"https:\/\/youtu.be\/sCV5g9IoPK0\" target=\"_blank\" rel=\"noopener\"><img class=\"alignnone wp-image-120\" src=\"http:\/\/epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/2018\/11\/download.png\" alt=\"\" width=\"36\" height=\"36\" \/><\/a><\/td>\r\n<\/tr>\r\n<\/tbody>\r\n<\/table>\r\n\r\n<img class=\"size-full wp-image-338 aligncenter\" src=\"http:\/\/csp01.epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/sites\/45\/2018\/07\/1-198.png\" alt=\"\" width=\"637\" height=\"379\" \/>","rendered":"<div><span style=\"float: right\"><a href=\"https:\/\/youtu.be\/sCV5g9IoPK0\" target=\"_blank\" rel=\"noopener\"><img decoding=\"async\" src=\"http:\/\/epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/2018\/11\/download.png\" alt=\"epgp books\" width=\"75px\" height=\"75px;\" \/><\/a><br \/>\n<\/span><\/div>\n<div>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify\">Welcome to the e-PG Pathshala Lecture Series on Data Structures. We have understood the basic concept of an important ADT \u2013 the Tree ADT and also the special type of tree the binary search tree. In this module we will discuss one very important type of binary search tree that is the balanced binary search tree and one example of balanced binary search tree namely the AVL trees.<\/p>\n<p>&nbsp;<\/p>\n<p><strong>Learning Objectives<\/strong><\/p>\n<p>&nbsp;<\/p>\n<p>The learning objectives of the module are as follows:<\/p>\n<p>&nbsp;<\/p>\n<p>\u2022\u00a0 To understand the concept of Balanced Binary Search Trees<\/p>\n<p>\u2022\u00a0 To discuss the properties of AVL Trees<\/p>\n<p>\u2022\u00a0 To describe\u00a0 Single Rotation of AVL Trees<\/p>\n<p>\u2022\u00a0 To discuss the Double Rotation of AVL Trees<\/p>\n<p>&nbsp;<\/p>\n<p><strong>23.1\u00a0 Introduction to Balanced Binary Search Trees<\/strong><\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify\">As a tree becomes more unbalanced, running time of the search operation increases from O(log n) to O(n) because as the tree gets more unbalanced the shape starts becoming a list. Binary Search Trees and important data structure used in many applications needing the search operation are fast if they\u2019re shallow or in other words the tree is a complete tree. The disadvantage of an unbalanced binary search tree is that its height can be as large as N-1. This essentially means that when one branch is much longer than the other, the time needed to perform insertion, deletion and search operations is much more.Now we need to define the concept of a \u201csort of\u201d complete tree where by \u201csort of\u201d we mean a binary search tree that is as balanced as possible. In other we want to keep the binary search tree balanced as nodes are added\/removed, so searching\/insertion remain efficient.<\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify\">A class of binary search trees is said to be <strong>balanced<\/strong>, if each of the three dictionary operations searching, inserting and deleting of keys for a tree with <em>n<\/em> keys can always (in the worst case) be carried out in <em>O<\/em>(log <em>n<\/em>) steps.<\/p>\n<p>&nbsp;<\/p>\n<p><strong>23.1.1 Approaches to Balancing Trees<\/strong><\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify\">There are many approaches that can be adopted for balancing trees. The first and simplest method is we do not do balancing. However in this case we may end up\u00a0<span style=\"font-size: 1em;text-align: initial\">with some nodes being very deep. The other extreme of the simple policy is to main a strict balance that is the tree must always be balanced perfectly. The next approach is what is called pretty good balance where we allow a little out of balance. The fourth and final approach is adjusting the balance during access \u2013 the so called self-adjusting trees.<\/span><\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify\"><strong style=\"text-align: initial;font-size: 1em\">23.2 Balanced Binary Search Trees<\/strong><\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify\"><span style=\"font-size: 1em\">There are two basic kinds of balanced binary search trees. <\/span><strong style=\"font-size: 1em\">Height balancedtrees<\/strong><span style=\"font-size: 1em\"> wherewe ensure that the height of siblings are \u201capproximately the same\u201d. The other type is the <\/span><strong style=\"font-size: 1em\">Weight-balancedtrees<\/strong><span style=\"font-size: 1em\"> where we ensure that the number of descendants of sibling nodes are \u201capproximately the same\u201d. \u201cTree rotations\u201d are used to maintain balance on insert\/delete<\/span><\/p>\n<\/div>\n<div>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify\">Now let us define the concept of balance &#8211; A <em>binary tree<\/em> is <strong><em>balanced<\/em><\/strong>(here unless otherwise specified balanced implies height balanced) if the difference in height between any node\u2019s left and right subtree is \u00a3 1.<\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: center\"><strong>balance = height(left subtree) &#8211; height(right subtree)<\/strong><\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify\">By convention the height of a \u201cnull\u201d subtree is assumed to be -1. If the balance is zero everywhere then the tree is said to be perfectly balanced. If the balance is small everywhere that is at each and every node then the tree is balanced enough to ensure operations will take time of the O<strong>(log n).<\/strong> This is because the maximum depth of the tree can be only 1.44 log n.<\/p>\n<p>&nbsp;<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-323 aligncenter\" src=\"http:\/\/csp01.epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/sites\/45\/2018\/07\/1-183.png\" alt=\"\" width=\"302\" height=\"227\" srcset=\"https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/1-183.png 302w, https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/1-183-300x225.png 300w, https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/1-183-65x49.png 65w, https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/1-183-225x169.png 225w\" sizes=\"auto, (max-width: 302px) 100vw, 302px\" \/><\/p>\n<p style=\"text-align: justify\"><strong>Perfect Balance <\/strong>is said to occur if we want a complete tree after every operation. This means that the tree is full except possibly in the lower right. However maintaining perfect balance is expensive. For example (Figure 23.2), insert 2 in the tree on the left and then rebuild as a complete tree<\/p>\n<p>&nbsp;<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-324 aligncenter\" src=\"http:\/\/csp01.epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/sites\/45\/2018\/07\/1-184.png\" alt=\"\" width=\"576\" height=\"164\" srcset=\"https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/1-184.png 576w, https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/1-184-300x85.png 300w, https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/1-184-65x19.png 65w, https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/1-184-225x64.png 225w, https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/1-184-350x100.png 350w\" sizes=\"auto, (max-width: 576px) 100vw, 576px\" \/><\/p>\n<p><strong style=\"text-align: initial;font-size: 1em\">23.2.1 Prevent the degeneration of the Binary Search Tree (BST) :<\/strong><\/p>\n<\/div>\n<div>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify\">A BST can be set up to maintain balance during every update operation typically insertions and removals. Our objective is to keep the height of a binary search tree O(log N) . This allows us to achieve a worst-case runtime of O(log n) for searching, inserting and deleting.<\/p>\n<p>&nbsp;<\/p>\n<p>Types of BST which maintain the optimal performance are the following:<\/p>\n<ul>\n<li><strong>AVL trees<\/strong><\/li>\n<li><strong>Red-Black trees<\/strong><\/li>\n<li><strong>Splay trees<\/strong><\/li>\n<\/ul>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify\"><strong>AVL Trees<\/strong>are BSTs that maintain<strong> <em>height balance.<\/em> <\/strong>For for each node, the difference in height of its two subtrees is in the range -1 to 1<\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify\"><strong>Red<\/strong>&#8211;<strong>black <\/strong>tree is a binary version of a 2-3-4 tree. Here the the nodes have a &#8216;color&#8217; attribute: <strong>BLACK<\/strong> or <strong>RED. The tree<\/strong> maintains a balance measure called the <strong><em>BLACK<\/em><\/strong> <strong><em>height<\/em><\/strong><\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify\">A <strong>splay tree<\/strong> are self-adjusting type of BST with the additional property that <strong>recently<\/strong> <strong>accessed elements are quick to be access again<\/strong>. The basic operations such as insertion, look-up and removal takes place in O(log n) amortized time. Insert\/find always rotates node <em>to the root<\/em>.<\/p>\n<p>&nbsp;<\/p>\n<p><strong>23.3AVL Tree<\/strong><\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify\">We will first discuss an AVL tree which is essentially a binary search tree with a <em>balance <\/em>condition. AVL is named after its inventors: <strong>A<\/strong>del\u2019son-<strong>V<\/strong>el\u2019skii and <strong>L<\/strong>andis. The AVL tree <strong><em>approximates<\/em><\/strong> <strong>the ideal tree<\/strong> (completely balanced tree) since the AVL Tree maintains a height close to the minimum (Figure 23.3)However It is not a requirement that all leaves of an AVL tree be on the same or adjacent level.<\/p>\n<p>&nbsp;<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-325 aligncenter\" src=\"http:\/\/csp01.epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/sites\/45\/2018\/07\/1-185.png\" alt=\"\" width=\"617\" height=\"237\" srcset=\"https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/1-185.png 617w, https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/1-185-300x115.png 300w, https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/1-185-65x25.png 65w, https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/1-185-225x86.png 225w, https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/1-185-350x134.png 350w\" sizes=\"auto, (max-width: 617px) 100vw, 617px\" \/><\/p>\n<p>&nbsp;<\/p>\n<p><strong>23.3.1 AVL &#8211; Good but not Perfect Balance<\/strong><\/p>\n<\/div>\n<div>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify\">As we have already discussed AVL tree is a binary search tree that has an additional <strong><em>height constraint<\/em><\/strong>:For each node x in the tree, Height(x.left) differs from Height(x.right) by at most 1. If this height constraint is satisfied then the height of the tree is O(log n).<\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify\">As we already discussed the Balance factor of a node which is defined below:<\/p>\n<p>&nbsp;<\/p>\n<p><strong>height(left sub-tree) &#8211; height(right sub-tree)<\/strong><\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify\">The AVL tree has balance factor calculated at every node, where for every node, the heights of the left and right sub-trees can differ by no more than 1. Each node storesthe current heights at each node. The height of an AVL tree storing n keys is O(log n).<\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify\">The height of a node is now specified. The height of a leaf is 0. The height of a null tree is assumed to be -1. In general, the height of an internal node is the maximum height of its children plus 1 (Figure 23.4). The height of any node is the maximum of the heights of it\u2019s two sub-trees.Maximum of (H-1, H-2) + 1 = H-1+1 =H<\/p>\n<p>&nbsp;<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-326 aligncenter\" src=\"http:\/\/csp01.epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/sites\/45\/2018\/07\/1-186.png\" alt=\"\" width=\"580\" height=\"639\" srcset=\"https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/1-186.png 580w, https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/1-186-272x300.png 272w, https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/1-186-65x72.png 65w, https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/1-186-225x248.png 225w, https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/1-186-350x386.png 350w\" sizes=\"auto, (max-width: 580px) 100vw, 580px\" \/><\/p>\n<\/div>\n<div>\n<p style=\"text-align: center\"><strong>Figure 23.5 Example of an AVL Tree with Heights shown<\/strong><\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify\">An example of an AVL tree where the heights are shown next to the nodes is given in Figure 23.5. Now remember an AVL tree is a binary search tree. The height of the left and right sub-trees of the root differ by at most 1. Now each left and right sub-trees are themselves AVL trees. An AVL Tree T is a binary search tree such that for every internal node v of T, the heights of the children of v can differ by at most 1<em>.<\/em><\/p>\n<p>&nbsp;<\/p>\n<p><strong>To sum up :<\/strong><\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify\">Height of empty node = -1, Height of leaf = 0 and Height of a node = Max (Height of left subtree, Height of right subtree) +1<\/p>\n<p>&nbsp;<\/p>\n<p><strong>23.3.2 Balance Factor of AVL Trees<\/strong><\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify\">For each AVL tree node, the difference between the heights of its left and right sub-trees is either -1, 0 or +1 is called thebalance factor of a node. The balanceFactor = height(left sub-tree) \u2013 height(right sub-tree). If balanceFactor &gt; 1 or &lt; -1 then the tree is too unbalanced, and needs &#8216;rearranging&#8217; to make it more balanced. When we have a positive balance factor the node is said to be &#8220;heavy on the left\u201c that is the height of the left sub-tree is greater than the height of the right sub-tree. When we have negative balance factor, the node is &#8220;heavy on the right\u201c. Figure 23.6 shows an example where the heights and balance factors are shown next to the nodes<\/p>\n<p>&nbsp;<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-327 aligncenter\" src=\"http:\/\/csp01.epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/sites\/45\/2018\/07\/1-187.png\" alt=\"\" width=\"453\" height=\"328\" srcset=\"https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/1-187.png 453w, https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/1-187-300x217.png 300w, https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/1-187-65x47.png 65w, https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/1-187-225x163.png 225w, https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/1-187-350x253.png 350w\" sizes=\"auto, (max-width: 453px) 100vw, 453px\" \/><\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify\">Now the balance factor of an AVL tree can be either 0, +1 or -1. Due insertions or deletions the balance factor can change from 0 to +1 or -1. In this case there is no imbalance. When the balance factor changes from +1\/-1 to +2\/-2 then comesthe need to rebalance so that the balance factor is back to 0, +1 or -1.<\/p>\n<p>&nbsp;<\/p>\n<p><strong>23.4 Properties of AVL Trees<\/strong><\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify\">The depth of a typical node in an AVL tree approaches the optimal value possible which is <em>log N<\/em>.Consequently, all searching operations in an AVL tree have logarithmic worst-case bounds.An update (insert or remove) in an AVL tree could\u00a0<span style=\"font-size: 1em;text-align: initial\">destroy the balance. Therefore we go about rebalancing before before the operation can be considered complete. This time taken for rebalancing during update operation is the price we pay for making the search operation faster. It is to be noted that after\u00a0<\/span><span style=\"text-align: initial;font-size: 1em\">an insertion, only nodes that are on the path from the insertion point to the root can have their balances altered. This affects the way we carry out rebalancing.<\/span><\/p>\n<\/div>\n<div>\n<p>&nbsp;<\/p>\n<p><strong>23.5 Rotations<\/strong><\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify\">Insertion or deletion operation involves adding or deleting only a single node at a time. This essentially means that the height of some sub-tree can change by at most 1.\u00a0 If the AVL tree property is violated at a node x, it means that the heights of left(x) and right(x) differ by exactly 2. Now rotations will have to be applied to x to restore the AVL tree property.The rotation is generally a O(1) operation.<\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify\">In order to restore the balance of an unbalanced tree, three adjacent nodes are involved in the rotation. The deepest unbalanced node, is the node that requires rotation to rebalance the tree.This node is either the ancestor of a deleted node (in case of deletion) or of the inserted node (in case of insertion) andis the node whose balance factor has changed to -2 or +2. This is shown using an example of insertion in Figure 23.7. Here the element 35 is inserted. Therefore the three nodes that take part in the rotation are the deepest unbalanced node (here 45), the ancestor of the inserted node (40) and the inserted node (35).<\/p>\n<p>&nbsp;<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-328 aligncenter\" src=\"http:\/\/csp01.epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/sites\/45\/2018\/07\/1-188.png\" alt=\"\" width=\"621\" height=\"533\" srcset=\"https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/1-188.png 621w, https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/1-188-300x257.png 300w, https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/1-188-65x56.png 65w, https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/1-188-225x193.png 225w, https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/1-188-350x300.png 350w\" sizes=\"auto, (max-width: 621px) 100vw, 621px\" \/><\/p>\n<div>\n<p>&nbsp;<\/p>\n<p><strong>23.5.1 BST Ordering Property after a Rotation<\/strong><\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify\">A rotation does not affect the ordering property of a BST (binary search tree). This is explained in Figure 23.8. Let us assume a e \u03b1, b e \u03b2 and c eg. This implies that a \u2264 A \u2264 b \u2264 B \u2264 c according to the BST tree property. It can be seen that even after rotation the same property is maintained. We will explain the actual procedure of rotation in the next section.<\/p>\n<p>&nbsp;<\/p>\n<p><strong>23.5.2 Concept of Rotation and BST Property<\/strong><\/p>\n<p>&nbsp;<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-329 aligncenter\" src=\"http:\/\/csp01.epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/sites\/45\/2018\/07\/1-189.png\" alt=\"\" width=\"503\" height=\"177\" srcset=\"https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/1-189.png 503w, https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/1-189-300x106.png 300w, https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/1-189-65x23.png 65w, https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/1-189-225x79.png 225w, https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/1-189-350x123.png 350w\" sizes=\"auto, (max-width: 503px) 100vw, 503px\" \/><\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify\">Now let us discuss rotation in terms of subtrees. BST ordering property requirement means that on the left hand side tree T1 &lt; x &lt; y,x&lt; T2&lt; y andx &lt; y &lt; T3. Now we carry out a right rotation of x about y (Figure23.9). This means that the following changes are made:<\/p>\n<ul>\n<li style=\"text-align: justify\">y which was the parent with x as left child (x&lt;y) before rotation now becomes the right child of parent x retaining the x&lt;y property.<\/li>\n<li style=\"text-align: justify\">T2 which was the right sub-treeof x (x&lt;T2&lt;y) before rotation now becomes the left sub-tree of y still maintaining the same relation.<\/li>\n<li style=\"text-align: justify\">Therefore after rotation the same property T1 &lt; x &lt; y, x &lt; T2&lt; y and x &lt; y &lt; T3is maintained.<\/li>\n<\/ul>\n<p>Though we have seen an example of right rotation, the same is true for left rotation.<\/p>\n<p>&nbsp;<\/p>\n<p><strong>23.6\u00a0 Different Cases of Rotations<\/strong><\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify\">There are basically two types of rotations based on whether we carry out single or double rotations. We also classify rotations based on where and on which sub-tree (left or right) the violation or the imbalance occurs. Here we also use the term outside grandchild that is the left grandchild of a left child (left-left) or the right grandchild of a right child (right-right). We have an inside grandchild when we have left grandchild of a right child (left-right) or the right grandchild of a left child (right-left). Therefore considering these aspects we have the following four cases of rotations:<\/p>\n<p>&nbsp;<\/p>\n<p>Violation may occur during an insertion into<\/p>\n<p><strong>Case1. <\/strong>left sub-tree of left child<strong> (Single Right Rotation)<\/strong><\/p>\n<p><strong>Case 2. <\/strong>right sub-tree of right child<strong> (Single Left Rotation)<\/strong><\/p>\n<p><strong>Case 3. <\/strong>right sub-tree of left child<strong> (Double Left \u2013 Right Rotation)<\/strong><\/p>\n<p><strong>Case 4. <\/strong>left sub-tree of right child<strong> (Double Right \u2013 Left Rotation)<\/strong><\/p>\n<p>&nbsp;<\/p>\n<p><strong>23.6.1 Single Rotation<\/strong><\/p>\n<\/div>\n<div>\n<p style=\"text-align: justify\">The simplest type of rotation is single rotation. Case 1 and case 2 are both cases of single rotation. Single rotation switches the roles of the parent and child while maintaining the search order. Here we rotate between a node and its child. While the child becomes parent, parent becomes right child in case 1 and left child in case 2. The result is a binary search tree that satisfies the AVL property. Now let us discuss in detail the above two cases.<\/p>\n<p>&nbsp;<\/p>\n<p><strong>23.6.2 <\/strong><strong>Single Right Rotation<\/strong><\/p>\n<\/div>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-330 aligncenter\" src=\"http:\/\/csp01.epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/sites\/45\/2018\/07\/1-190.png\" alt=\"\" width=\"599\" height=\"532\" srcset=\"https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/1-190.png 599w, https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/1-190-300x266.png 300w, https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/1-190-65x58.png 65w, https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/1-190-225x200.png 225w, https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/1-190-350x311.png 350w\" sizes=\"auto, (max-width: 599px) 100vw, 599px\" \/><\/p>\n<div>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-331 aligncenter\" src=\"http:\/\/csp01.epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/sites\/45\/2018\/07\/1-191.png\" alt=\"\" width=\"600\" height=\"284\" srcset=\"https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/1-191.png 600w, https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/1-191-300x142.png 300w, https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/1-191-65x31.png 65w, https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/1-191-225x107.png 225w, https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/1-191-350x166.png 350w\" sizes=\"auto, (max-width: 600px) 100vw, 600px\" \/><\/p>\n<p style=\"text-align: center\"><strong>(c )<\/strong><br \/>\n<strong>Figure 23.10 Single Right Rotation<\/strong><\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify\"><span style=\"text-align: justify;font-size: 1em\">Figure 23.10 shows the details of case 1. In this case the left sub-tree of the left child of X violates the property. In Figure 23.10 (a) this means sub-tree A, the left sub-tree of the left child Y of X violates the property. We need to carry out single right rotation of X about Y where X now becomes the right child of Y, and the right sub-tree of Y becomes the left sub-tree of X.<\/span><\/p>\n<\/div>\n<div>\n<p>&nbsp;<\/p>\n<p>Figure 23.10 (b) shows the three steps namely<\/p>\n<p>&nbsp;<\/p>\n<p>\u2022\u00a0 the left child x of a node y becomes y&#8217;s parent.<\/p>\n<p>\u2022\u00a0 y becomes the right child of x.<\/p>\n<p>\u2022\u00a0 The right subtree T2 of x, if any, becomes the left child of y<\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify\">Here the notation ((T1+T2)+T3) indicates that the subtrees T1 and T2 are children of the same parent and that the combined subtree of (T1+T2) and subtree T3 are children of the root. Moreover the sub-trees change positions from ((T1+T2) + T3) before the rotation to positions (T1+(T2+T3)) after the rotation still maintaining the BST property.<\/p>\n<p>&nbsp;<\/p>\n<p>Figure 23.10 (c) shows the steps more in detail.<\/p>\n<ul>\n<li style=\"text-align: justify\">The node k2 is the node at which the imbalance occurs and the balance factor is violated since the height of subtree c has height difference of 2 with the subtree A.<\/li>\n<li>The left child of k2 is given by k1=k2.left.<\/li>\n<li>The new left sub-tree of k2 is now is k1\u2019s right sub-tree (B) that is k2.left=k1.right<\/li>\n<li>The new right sub-tree of k1 is k2 that is k1.right=k2<\/li>\n<li>\u00a0Now we return the root of the new balanced tree that is k1.<\/li>\n<\/ul>\n<p>The algorithm is given below in Figure 23.10 (d) (notations from Figure 23.10 (b))<\/p>\n<table style=\"border-collapse: collapse;width: 100%\">\n<tbody>\n<tr>\n<td style=\"width: 100%\">\n<div>\n<p><strong>private TreeNode rightRotate(TreeNode oldParent) {<\/strong><\/p>\n<p>&nbsp;<\/p>\n<p>\/\/ 1. detach left child&#8217;s (x\u2019s) right subtree (T2)<\/p>\n<p>&nbsp;<\/p>\n<p><strong>TreeNode orphan= oldParent.left.right;<\/strong><\/p>\n<p>&nbsp;<\/p>\n<p>\/\/ 2. consider left child (x) to be the new parent<\/p>\n<p>&nbsp;<\/p>\n<p><strong>TreeNode newParent = oldParent.left;<\/strong><\/p>\n<p>&nbsp;<\/p>\n<p>\/\/ 3. attach old parent (y) onto right of new parent (x)<\/p>\n<p>&nbsp;<\/p>\n<p><strong>newParent.right = oldParent;<\/strong><\/p>\n<p>&nbsp;<\/p>\n<p>\/\/\u00a0 4. attach new parent&#8217;s (x\u2019s) right subtree (T2)as<\/p>\n<p>&nbsp;<\/p>\n<p>\/\/\u00a0\u00a0\u00a0\u00a0\u00a0 left subtree of old parent (y)<\/p>\n<p>&nbsp;<\/p>\n<p><strong>oldParent.left = orphan;<\/strong><\/p>\n<p>&nbsp;<\/p>\n<p><strong>oldParent.height = height(oldParent); <\/strong>\/\/ update nodes&#8217;<\/p>\n<p><strong>newParent.height = height(newParent); <\/strong>\/\/ height values<\/p>\n<\/div>\n<div>\n<p><strong>return newParent;<\/strong><\/p>\n<p><strong>}<\/strong><\/p>\n<\/div>\n<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<\/div>\n<div>\n<p style=\"text-align: center\"><strong>Figure 23.10 (d) Algorithm for Single Right Rotation 23.6.3 Single Left Rotation<\/strong><\/p>\n<p>&nbsp;<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-332 aligncenter\" src=\"http:\/\/csp01.epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/sites\/45\/2018\/07\/1-192.png\" alt=\"\" width=\"635\" height=\"568\" srcset=\"https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/1-192.png 635w, https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/1-192-300x268.png 300w, https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/1-192-65x58.png 65w, https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/1-192-225x201.png 225w, https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/1-192-350x313.png 350w\" sizes=\"auto, (max-width: 635px) 100vw, 635px\" \/><\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-333 aligncenter\" src=\"http:\/\/csp01.epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/sites\/45\/2018\/07\/1-193.png\" alt=\"\" width=\"605\" height=\"315\" srcset=\"https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/1-193.png 605w, https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/1-193-300x156.png 300w, https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/1-193-65x34.png 65w, https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/1-193-225x117.png 225w, https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/1-193-350x182.png 350w\" sizes=\"auto, (max-width: 605px) 100vw, 605px\" \/><\/p>\n<\/div>\n<div>\n<p style=\"text-align: center\"><strong>Figure 23.11 Single Left Rotation<\/strong><\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify\">Figure 23.11 shows the details of case 2. In this case the right sub-tree of the right child of X violates the property.In Figure 23.11 (a) this means sub-tree A, the right sub-tree of the right child Y of X violates the property. We need to carry out single left rotation of Y about X where X now becomes the left child of Y, and the left sub-tree of Y becomes the right sub-tree of X.<\/p>\n<p>&nbsp;<\/p>\n<p>Figure 23.11 (b) shows the three steps namely<\/p>\n<p>&nbsp;<\/p>\n<p>\u2022 The right child y of a node x becomes x&#8217;s parent.<\/p>\n<p>\u2022 x becomes the left child of y.<\/p>\n<p>\u2022 The left child T2 of y, if any, becomes the right child of x<\/p>\n<p style=\"text-align: justify\">Moreover the sub-trees change positions from (T1 + (T2 + T3)) before the rotation to positions ((T1 + T2) + T3)after the rotation still maintaining the BST property.<\/p>\n<p>&nbsp;<\/p>\n<p>Figure 23.10 (c) shows the steps more in detail.<\/p>\n<ul>\n<li style=\"text-align: justify\">The node k1 is the node at which the imbalance occurs and the balance factor is violated where the height of subtree c differs with the height of subtree A by more than one.<\/li>\n<li>The right child of k2 is given by k2=k1.right.<\/li>\n<li>The new right sub-tree of k1 is now is k2\u2019s left sub-tree (B) that is k1.right=k2.left (B)<\/li>\n<li>The new left sub-tree of k2 is k1 that is k2.left=k1<\/li>\n<li>Now we return the root of the new balanced tree that is k2.<\/li>\n<li>The algorithm is given below in Figure 23.11 (d) (notations from Figure 23.11 (b))<\/li>\n<\/ul>\n<table style=\"border-collapse: collapse;width: 100%\">\n<tbody>\n<tr>\n<td style=\"width: 100%\"><strong>private TreeNode leftRotate(TreeNode oldParent) {<\/strong><\/p>\n<p>&nbsp;<\/p>\n<p>\/\/ 1. detach right child&#8217;s (y) left subtree (T2)<\/p>\n<p>&nbsp;<\/p>\n<p><strong>TreeNode orphan = oldParent.right.left;<\/strong><\/p>\n<p>&nbsp;<\/p>\n<p>\/\/ 2. consider right child (y) to be the new parent<\/p>\n<p>&nbsp;<\/p>\n<p><strong>TreeNode newParent = oldParent.right;<\/strong><\/p>\n<p>&nbsp;<\/p>\n<p>\/\/ 3. attach old parent (x) onto left of new parent (y)<\/p>\n<p>&nbsp;<\/p>\n<p><strong>newParent.left = oldParent;<\/strong><\/p>\n<p>&nbsp;<\/p>\n<p>\/\/\u00a0 4. attach new parent&#8217;s old left subtree (T2) \/\/asright subtree of old parent (x)<\/p>\n<p>&nbsp;<\/p>\n<p><strong>oldParent.right = orphan;<\/strong><\/p>\n<p>&nbsp;<\/p>\n<p><strong>oldParent.height = height(oldParent); \/\/ update nodes&#8217; newParent.height = height(newParent); \/\/ height values return newParent;<\/strong><\/p>\n<p>&nbsp;<\/p>\n<p><strong>}<\/strong><\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>&nbsp;<\/p>\n<p style=\"text-align: center\"><strong>Figure 23.11 (d) Algorithm for Single Left Rotation<\/strong><\/p>\n<\/div>\n<div>\n<p>&nbsp;<\/p>\n<p><strong>23.6.4 Double Rotations<\/strong><\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify\">The other two cases of rotation are cases of two single rotations. When a new item is added to the sub-tree for an inside grandchild (left-right or right-left)<strong>,<\/strong> the imbalance is fixed with a double right or left rotation.<\/p>\n<p>&nbsp;<\/p>\n<p><strong>23.6.5 Left Right Double Rotation<\/strong><\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify\">When we have the situation where we have a left inside grandchild (left-right) case we have left right double rotation. A left-right double rotation is equivalent to a sequence of two single rotations where the first rotation on the original tree is a <em>left<\/em> rotation between X\u2019s left-child and grandchild and the second rotation on this new tree is a <em>right<\/em> rotation between X and its new left child.<\/p>\n<p>&nbsp;<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-334 aligncenter\" src=\"http:\/\/csp01.epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/sites\/45\/2018\/07\/1-194.png\" alt=\"\" width=\"577\" height=\"538\" srcset=\"https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/1-194.png 577w, https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/1-194-300x280.png 300w, https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/1-194-65x61.png 65w, https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/1-194-225x210.png 225w, https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/1-194-350x326.png 350w\" sizes=\"auto, (max-width: 577px) 100vw, 577px\" \/><\/p>\n<p>&nbsp;<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-335 aligncenter\" src=\"http:\/\/csp01.epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/sites\/45\/2018\/07\/1-195.png\" alt=\"\" width=\"569\" height=\"345\" srcset=\"https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/1-195.png 569w, https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/1-195-300x182.png 300w, https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/1-195-65x39.png 65w, https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/1-195-225x136.png 225w, https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/1-195-350x212.png 350w\" sizes=\"auto, (max-width: 569px) 100vw, 569px\" \/><\/p>\n<p>&nbsp;<\/p>\n<p><span style=\"text-align: justify;font-size: 1em\">Figure 23.12 shows the details of case 3. In this case the right sub-tree of the left child of X violates the property. First we need to rotate the tree LEFT about X\u2019s left child (Y) and grandchild (Z) as shown in Figure 23.12 (a) similar to single left rotation. Now we need to carry out the single right rotation of z about x in the modified tree that is we rotate the tree RIGHT about X and its new left child (Z) (Figure 23.12.(b)).<\/span><\/p>\n<\/div>\n<\/div>\n<div>\n<p>&nbsp;<\/p>\n<p>Figure 23.12 (c) shows the steps more in detail.<\/p>\n<ul>\n<li style=\"text-align: justify\">The first pivot for the left rotation is the left child (v) of the deepest unbalanced node (x).<\/li>\n<li style=\"text-align: justify\">We carry out Single Left Rotation of w about the first pivot v<\/li>\n<li style=\"text-align: justify\">Moreover during this single left rotationthe sub-trees change positions from ((T1 + ((T2+T3) + T4) before the first rotation to positions (((T1 + T2)+T3) + T4) after the rotation still maintaining the BST property.<\/li>\n<li style=\"text-align: justify\">The second pivot for the right rotation is the deepest unbalanced node itself x.<\/li>\n<li style=\"text-align: justify\">We carry out Single Right Rotation of w about x<\/li>\n<li style=\"text-align: justify\">Moreover during this single right rotation the sub-trees change positions from (((T1 + T2)+T3) + T4) before the first rotation to positions ((T1 + T2)+ (T3+ T4)) after the rotation still maintaining the BST property.<\/li>\n<\/ul>\n<p>&nbsp;<\/p>\n<p><strong>23.6.6 Right-Left Double Rotation<\/strong><\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify\">When we have the situation where we have a right inside grandchild (right-left) then we have right left double rotation. A right-left double rotation is equivalent to a sequence of two single rotations where the first rotation on the original tree is a right rotation between X\u2019s right-child and grandchild and the second rotation on this new tree is a <em>left<\/em> rotation between X and its new right child.<\/p>\n<\/div>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-336 aligncenter\" src=\"http:\/\/csp01.epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/sites\/45\/2018\/07\/1-196.png\" alt=\"\" width=\"582\" height=\"300\" srcset=\"https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/1-196.png 582w, https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/1-196-300x155.png 300w, https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/1-196-65x34.png 65w, https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/1-196-225x116.png 225w, https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/1-196-350x180.png 350w\" sizes=\"auto, (max-width: 582px) 100vw, 582px\" \/><\/p>\n<div>\n<p style=\"text-align: justify\">Figure 23.13 shows the details of case 4. The left sub-tree of the right child of X violates the property. First we need to rotate the tree RIGHT about X\u2019s rightchild (Y) and grandchild (Z) as shown in Figure 23.13 (a) similar to single right rotation. Now we need to carry out the single left rotation of z about x in the modified tree that is we rotate the tree LEFT about X and its new rightchild (Z) (Figure 23.13 (b)).<\/p>\n<p>&nbsp;<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-337 aligncenter\" src=\"http:\/\/csp01.epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/sites\/45\/2018\/07\/1-197.png\" alt=\"\" width=\"572\" height=\"586\" srcset=\"https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/1-197.png 572w, https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/1-197-293x300.png 293w, https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/1-197-65x67.png 65w, https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/1-197-225x231.png 225w, https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/1-197-350x359.png 350w\" sizes=\"auto, (max-width: 572px) 100vw, 572px\" \/><\/p>\n<p>&nbsp;<\/p>\n<\/div>\n<p>&nbsp;<\/p>\n<p><strong>Figure 23.13 Right Left Double Rotation <\/strong>Figure 23.13 (c) shows the steps more in detail.<\/p>\n<ul>\n<li>The first pivot for the right rotation is right child (w) of the deepest unbalanced node (x)<\/li>\n<li>We carry out Single Right Rotation of v about the first pivot w<\/li>\n<li style=\"text-align: justify\">Moreover during this single left rotation the sub-trees change positions from (T1 + ((T2+T3) + T4)) before the first rotation to positions (T1 + (T2 (T3 + T4)) after the rotation still maintaining the BST property.<\/li>\n<li>The second pivot for the right rotation is the deepest unbalanced node itself x.<\/li>\n<li>We carry out Single Left Rotation of v about x<\/li>\n<li style=\"text-align: justify\">Moreover during this single right rotation the sub-trees change positions from (T1 + (T2 (T3 + T4)) before the first rotation to positions ((T1 + T2) + (T3 + T4)) after the rotation still maintaining the BST property.<\/li>\n<\/ul>\n<p style=\"text-align: justify\">The algorithms for the double rotations are sequential call to the two appropriate single rotations with the appropriate nodes. Therefore we have seen all the possible four cases of violation and the rebalancing through rotations.<\/p>\n<p>&nbsp;<\/p>\n<p><strong>Summary<\/strong><\/p>\n<ul>\n<li>Explained the concept of balancing and Balanced Binary Search Trees<\/li>\n<li>Discussed the properties of AVL Trees<\/li>\n<li>Described Single Rotation of AVL Trees<\/li>\n<li>Discussed Double Rotation of AVL Trees<\/li>\n<\/ul>\n<table>\n<tbody>\n<tr>\n<td><strong>you can view video on Balanced Binary Search Trees and AVL Trees<\/strong><\/td>\n<td><a href=\"https:\/\/youtu.be\/sCV5g9IoPK0\" target=\"_blank\" rel=\"noopener\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone wp-image-120\" src=\"http:\/\/epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/2018\/11\/download.png\" alt=\"\" width=\"36\" height=\"36\" \/><\/a><\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"size-full wp-image-338 aligncenter\" src=\"http:\/\/csp01.epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/sites\/45\/2018\/07\/1-198.png\" alt=\"\" width=\"637\" height=\"379\" srcset=\"https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/1-198.png 637w, https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/1-198-300x178.png 300w, https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/1-198-65x39.png 65w, https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/1-198-225x134.png 225w, https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/1-198-350x208.png 350w\" sizes=\"auto, (max-width: 637px) 100vw, 637px\" \/><\/p>\n","protected":false},"author":3,"menu_order":23,"template":"","meta":{"pb_show_title":"on","pb_short_title":"","pb_subtitle":"","pb_authors":["dr-t-v-geetha"],"pb_section_license":""},"chapter-type":[],"contributor":[59],"license":[],"class_list":["post-320","chapter","type-chapter","status-publish","hentry","contributor-dr-t-v-geetha"],"part":3,"_links":{"self":[{"href":"https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-json\/pressbooks\/v2\/chapters\/320","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-json\/pressbooks\/v2\/chapters"}],"about":[{"href":"https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-json\/wp\/v2\/types\/chapter"}],"author":[{"embeddable":true,"href":"https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-json\/wp\/v2\/users\/3"}],"version-history":[{"count":9,"href":"https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-json\/pressbooks\/v2\/chapters\/320\/revisions"}],"predecessor-version":[{"id":951,"href":"https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-json\/pressbooks\/v2\/chapters\/320\/revisions\/951"}],"part":[{"href":"https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-json\/pressbooks\/v2\/parts\/3"}],"metadata":[{"href":"https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-json\/pressbooks\/v2\/chapters\/320\/metadata\/"}],"wp:attachment":[{"href":"https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-json\/wp\/v2\/media?parent=320"}],"wp:term":[{"taxonomy":"chapter-type","embeddable":true,"href":"https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-json\/pressbooks\/v2\/chapter-type?post=320"},{"taxonomy":"contributor","embeddable":true,"href":"https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-json\/wp\/v2\/contributor?post=320"},{"taxonomy":"license","embeddable":true,"href":"https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-json\/wp\/v2\/license?post=320"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}