{"id":340,"date":"2018-07-19T06:16:52","date_gmt":"2018-07-19T06:16:52","guid":{"rendered":"http:\/\/csp01.epgpbooks.inflibnet.ac.in\/?post_type=chapter&#038;p=340"},"modified":"2018-12-12T10:36:18","modified_gmt":"2018-12-12T10:36:18","slug":"insertion-and-deletion-avl-trees","status":"publish","type":"chapter","link":"https:\/\/ebooks.inflibnet.ac.in\/csp01\/chapter\/insertion-and-deletion-avl-trees\/","title":{"rendered":"Insertion and Deletion &#8211; AVL Trees"},"content":{"raw":"<div><span style=\"float: right\"><a href=\"https:\/\/youtu.be\/zPtm3V3eVPY\" 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 AVL, a balanced binary search tree. In this module we will discuss the insertion and deletion operations of AVL trees and the rebalancing carried out for re-establishing the balance while maintaining the binary search property.<\/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 To understand the Insertion operations of AVL Trees\r\n\r\n\u2022 To discuss Deletion from AVL Trees\r\n\r\n\u2022 To Outline the Pros and Cons of AVL Trees\r\n\r\n&nbsp;\r\n\r\n<strong>24.1 Insertion into AVL Trees<\/strong>\r\n\r\n&nbsp;\r\n<p style=\"text-align: justify\">The first step of insertionof a node into the AVL tree is a normal insertion using a BST insertion algorithm. However in addition we have to rebalance the tree if an imbalance occurs. As we have discussed in the last module, an imbalance occurs if a node's balance factor changes from -1 to -2 or from+1 to +2.Rebalancing is done at the deepest or lowest unbalanced ancestor of the inserted node.<\/p>\r\n&nbsp;\r\n\r\nThere are three cases that can happen during insertion:\r\n\r\n&nbsp;\r\n<p style=\"text-align: justify\">1. Insertion that does not cause an imbalance.<\/p>\r\n<p style=\"text-align: justify\">2.Same side (left-left or right-right) insertion that causes an imbalance. This type of insertion requires a single rotation to rebalance.<\/p>\r\n<p style=\"text-align: justify\">3.Opposite side (left-right or right-left) insertion that causes an imbalance. This type of insertion requires a double rotation to rebalance.<\/p>\r\n&nbsp;\r\n\r\n<strong>24.2 Insertion Algorithm<\/strong>\r\n\r\n&nbsp;\r\n<p style=\"text-align: justify\">The first step of the insertion algorithm for AVL trees is the same as insertion into a binary search tree. Here we first find a place for the value to be inserted, and then insert it. Now comes the next step of insertion into the AVL tree which is searching back from the inserted node looking for imbalance. If there is an imbalancewhen a new element is added as the outside grandchild (that is the left grandchild of a left\u00a0<span style=\"font-size: 1em;text-align: initial\">child (left-left) or the right grandchild of a right child (right-right))(Figure 24.1 (a)) we perform single rotation and exit. When a new item is added as an inside grandchild (Figure 24.1 (b)), the imbalance is fixed with a double right or left rotation. As already discussed in the previous module an inside grandchild is when we have left grandchild of a right child (left-right) or the right grandchild of a left child (right-left)).<\/span><\/p>\r\n\r\n<\/div>\r\n<div>\r\n\r\n<img class=\"alignnone size-full wp-image-341 aligncenter\" src=\"http:\/\/csp01.epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/sites\/45\/2018\/07\/1-199.png\" alt=\"\" width=\"545\" height=\"253\" \/>\r\n\r\n&nbsp;\r\n<p style=\"text-align: justify\">As we have already discussed in the previous module the insert operation may cause balance factor to become +2 or \u20132 for some node. Now only nodes on the path from the insertion point to the root node have possibly changed in height. Therefore after insertion, we go back up to the root node by node, updating heights as we go. If a new balance factor (the difference hleft-hright) is +2 or \u20132, we need to adjust the balance of the tree by <em>rotation<\/em> around the node. Now let us consider a validAVL sub-tree (Figure 24.2). Before the insertion into the subtree M the tree was a valid AVL tree, but after the insertion the AVL property is violated at node j where the balance factor has become +2.<\/p>\r\n&nbsp;\r\n\r\n<img class=\"alignnone size-full wp-image-342 aligncenter\" src=\"http:\/\/csp01.epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/sites\/45\/2018\/07\/1-200.png\" alt=\"\" width=\"599\" height=\"282\" \/>\r\n\r\n<strong style=\"text-align: initial;font-size: 1em\">24.3 Rebalancing<\/strong>\r\n\r\n&nbsp;\r\n<p style=\"text-align: justify\"><span style=\"text-align: justify;font-size: 1em\">Rebalancing is done at the deepest unbalanced ancestor of the inserted node. Now let the node that needs rebalancing be denoted as X. The rebalancing is performed through four separate types of rotations as given below:<\/span><\/p>\r\n\r\n<\/div>\r\n<div>\r\n\r\n<strong>\u00a0 \u00a0 Outside Cases (require single rotation) :<\/strong>\r\n\r\n&nbsp;\r\n\r\n1.\u00a0 Insertion into left subtree of left child of X - <strong>(Single Right Rotation)<\/strong>\r\n\r\n2.\u00a0 Insertion into right subtree of right child of X \u2013 <strong>(Single Left Rotation)<\/strong>\r\n\r\n&nbsp;\r\n\r\n<strong>Inside Cases (require double rotation) :<\/strong>\r\n\r\n&nbsp;\r\n\r\n3.\u00a0 Insertion into right subtree of left child of X \u2013 <strong>(Left-Right Rotation)<\/strong>\r\n\r\n4.\u00a0 Insertion into left subtree of right child of X \u2013 <strong>(Right-Left Rotation)<\/strong>\r\n\r\n&nbsp;\r\n\r\n<strong>24.3.1 Insertion into left sub-tree of left child of X<\/strong>\r\n\r\n&nbsp;\r\n<p style=\"text-align: justify\">Let us consider the <strong>first case of insertion into left sub-tree of left child of X<\/strong> (Figure 24.3). Here we have inserted 7 as left sub-tree of left child (8) of X (9). It is at X that the balancing is violated with balance factor of X becoming +2. Now this node X is the pivot. This imbalance occurred when the new element (7) was added to the left sub-tree of the outside left grandchild that is we have the case of <strong>single right<\/strong> <strong>rotation.<\/strong><\/p>\r\n&nbsp;\r\n\r\n<img class=\"alignnone size-full wp-image-343 aligncenter\" src=\"http:\/\/csp01.epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/sites\/45\/2018\/07\/1-201.png\" alt=\"\" width=\"629\" height=\"327\" \/>\r\n\r\n&nbsp;\r\n<p style=\"text-align: justify\">Now we carry out single right rotation of 8 about 9. In this case 8 becomes the right child of the parent (6) of 9 while 9 becomes the right child of 8. The BST property of the AVL tree is now maintained.<\/p>\r\n&nbsp;\r\n\r\n<strong>24.3.2 Insertion into right sub-tree of right child of X<\/strong>\r\n\r\n&nbsp;\r\n<p style=\"text-align: justify\">Let us consider the <strong>second case of insertion into right sub-tree of right child of<\/strong> <strong>X <\/strong>(Figure 24.4). Here we have inserted 45 as right sub-tree of right child (40) of X\u00a0<span style=\"text-align: initial;font-size: 1em\">(35). It is at X that the balancing is violated with balance factor of X becoming -2. Now the node X is the pivot. This imbalance occurred when the new element (45) was added to the right sub-tree of the outside right grandchild that is we have the case of <\/span><strong style=\"text-align: initial;font-size: 1em\">single left rotation.<\/strong><span style=\"text-align: initial;font-size: 1em\">Now we carry out single left rotation of 40 about 35. In this case 40 becomes the right child of the parent (30) of 35 while 35 becomes the left child of 40. The BST property of the AVL tree is now maintained.<\/span><\/p>\r\n\r\n<\/div>\r\n<div>\r\n\r\n&nbsp;\r\n\r\n<img class=\"alignnone size-full wp-image-344 aligncenter\" src=\"http:\/\/csp01.epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/sites\/45\/2018\/07\/1-202.png\" alt=\"\" width=\"601\" height=\"580\" \/>\r\n\r\n&nbsp;\r\n\r\n&nbsp;\r\n<p style=\"text-align: justify\">Let us consider the <strong>third case of insertion into left sub-tree of right child of X<\/strong> (Figure 24.5). Here we have inserted 34 as left sub-tree of right child (40) of X (30). Balance is violated at X balance factor becoming -2. <strong>Now we carry out two<\/strong> <strong>rotations that is a right rotation followed by a left rotation<\/strong>:<\/p>\r\n\r\n<ul>\r\n \t<li style=\"text-align: justify\">The first is a <strong>single right rotate<\/strong> of 35 about the first pivot (40). Here 35 becomes the new right child of X while 40 becomes the right child of 35.<\/li>\r\n \t<li style=\"text-align: justify\">Next we carry out a <strong>single left rotate<\/strong> of 35 about second pivot node X (30). In this case 35 becomes the left child of the parent (20) of 30 while 30 becomes the new left child of 35. The BST property of the AVL tree is now maintained.<\/li>\r\n<\/ul>\r\n&nbsp;\r\n\r\n<strong>24.3.4 Insertion into right sub-tree of left child of X<\/strong>\r\n\r\n<\/div>\r\n<div>\r\n<p style=\"text-align: justify\">Let us consider the <strong>fourth case of insertion into right sub-tree of left child of X<\/strong> (Figure 24.6). Here we have inserted 7 as right sub-tree of left child (5) of X (10). Balance is violated at X balance factor becoming +2. When a new item (7) is added to the sub-tree oftheinside grandchild, the imbalance is fixed with a double rotation. Now we carry out two rotations that is a <strong>left rotation followed by a right rotation:<\/strong><\/p>\r\n&nbsp;\r\n\r\n&nbsp;\r\n\r\n<img class=\"alignnone size-full wp-image-345 aligncenter\" src=\"http:\/\/csp01.epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/sites\/45\/2018\/07\/1-203.png\" alt=\"\" width=\"569\" height=\"295\" \/>\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\nAvlTree <strong>Insert<\/strong>( ElementType X, AvlTree T )<strong>{<\/strong> if( T == NULL ){\r\n\r\n&nbsp;\r\n\r\n<strong>\/* Create and return a one-node tree *\/<\/strong>\r\n\r\n&nbsp;\r\n\r\nT = malloc( sizeof( struct AvlNode ) );\r\n\r\n&nbsp;\r\n\r\nif( T == NULL )\r\n\r\n&nbsp;\r\n\r\nFatalError( \"Out of space!!!\" );\r\n\r\n&nbsp;\r\n\r\nelse {\r\n\r\n&nbsp;\r\n\r\nT-&gt;Element = X; T-&gt;Height = 0;\r\n\r\n&nbsp;\r\n\r\nT-&gt;Left = T-&gt;Right = NULL;\r\n\r\n&nbsp;\r\n\r\n}\r\n\r\n&nbsp;\r\n\r\n}\r\n\r\n&nbsp;\r\n\r\nelse if( X &lt; T-&gt;Element ){\r\n\r\n&nbsp;\r\n\r\nT-&gt;Left = Insert( X, T-&gt;Left );<strong>\/* Insertion at the left*\/<\/strong> if( Height( T-&gt;Left ) - Height( T-&gt;Right ) == 2 ) if( X &lt; T-&gt;Left-&gt;Element )\r\n\r\n&nbsp;\r\n\r\nT = SingleRotateWithLeft( T ); <strong>\/* LL *\/<\/strong>\r\n\r\n&nbsp;\r\n\r\nelse\r\n\r\n&nbsp;\r\n\r\nT = DoubleRotateWithLeft( T ); <strong>\/* LR *\/<\/strong> } else if( X &gt; T-&gt;Element ){\r\n\r\n&nbsp;\r\n\r\nT-&gt;Right = Insert( X, T-&gt;Right ); <strong>\/* Insertion at the right*\/<\/strong> if( Height( T-&gt;Right ) - Height( T-&gt;Left ) == 2 ) if( X &gt; T-&gt;Right-&gt;Element )\r\n\r\n&nbsp;\r\n\r\nT = SingleRotateWithRight( T ); <strong>\/* RR *\/<\/strong>\r\n\r\n&nbsp;\r\n\r\nelse\r\n\r\n&nbsp;\r\n\r\nT = DoubleRotateWithRight( T ); <strong>\/* RL *\/<\/strong>\r\n\r\n<\/div>\r\n<strong>\u00a0<\/strong>\r\n<div>\r\n\r\n} <strong>\/* Else X is in the tree already; we'll do nothing *\/<\/strong> T-&gt;Height=Max(Height(T-&gt;Left),Height(T-&gt;Right))+1;\r\n\r\n&nbsp;\r\n\r\nreturn T;\r\n\r\n&nbsp;\r\n\r\n<strong>}<\/strong>\r\n\r\n<\/div><\/td>\r\n<\/tr>\r\n<\/tbody>\r\n<\/table>\r\n&nbsp;\r\n\r\n<\/div>\r\n<div>\r\n<p style=\"text-align: center\"><strong>Figure 24.7 The Insertion Algorithm<\/strong><\/p>\r\n&nbsp;\r\n<ul>\r\n \t<li style=\"text-align: justify\">The first is a <strong>single left rotate<\/strong> of 6 about the first pivot (5). Here 6 becomes the new left child of the parent (10)of 5 while 5 becomes the left child of 6.<\/li>\r\n \t<li style=\"text-align: justify\">Next we carry out a <strong>single right rotate<\/strong> of 6 about the second pivot node X (10). In this case 6 becomes the left child of parent (13) of X (10) while 10 becomes the new right child of 6. The BST property of the AVL tree is now maintained<\/li>\r\n<\/ul>\r\nThe detailed algorithm for insertion is given in Figure 24.7.\r\n\r\n&nbsp;\r\n\r\n<strong>24.4 Deletion<\/strong>\r\n\r\n&nbsp;\r\n<p style=\"text-align: justify\">The first step in deletion is deleting the node to be deleted X as in an ordinary binary search tree. Note that whatever may be the cases of deletion from the binary search tree, the last node deleted is a leaf.The deletion may result in an imbalance. Now we follow the path from the new leaf towards the root.For each node X encountered, we need to check if heights of left(X) and right(X) differ by at most 1. If yes, proceed to the parent(X) which now becomes the new X. If not, that is the heights differ by more than 1 then we need to perform an appropriate rotation at X. There are 4 cases as in the case of insertion. In the case of deletion, after we perform a rotation at X, we may have to perform a rotation at some ancestor of X. Thus, we must continue to trace the path until we reach the root. Now we will discuss the case of deletion where no balancing is needed and the cases where rebalancing of the tree is needed when an imbalance occurs after deletion.<\/p>\r\n<img class=\"alignnone size-full wp-image-346 aligncenter\" src=\"http:\/\/csp01.epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/sites\/45\/2018\/07\/1-204.png\" alt=\"\" width=\"557\" height=\"160\" \/>\r\n\r\n&nbsp;\r\n<p style=\"text-align: center\"><strong>Figure 24.8Deletion with no Imbalance with node P having balance factor=0<\/strong><\/p>\r\n&nbsp;\r\n\r\nWe can consider three cases for deletion:\r\n\r\n&nbsp;\r\n\r\n1. Deletion that does not cause an imbalance.\r\n\r\n2.Deletion that requires a single rotation to rebalance.\r\n\r\n3.Deletion that requires two or more rotations to rebalance.\r\n\r\n&nbsp;\r\n\r\n<strong>24.4.1 Deletion with no Imbalance<\/strong>\r\n\r\n<\/div>\r\n<div>\r\n\r\n&nbsp;\r\n<p style=\"text-align: justify\">Let us consider the simplest case of deletion that is deletion which causes no imbalance. We first consider the current node <em>p<\/em>that has sub-trees T1 and T2 with equal heights and so the <strong>balance factor of pis zero<\/strong> (Figure 24.8).When deletion occurs say in the left sub-tree T1, it\u2019s height is decreased but the height of p remains unchanged. The balance factor of <em>p<\/em>becomes (-1). This is allowed so there is no imbalance. This is illustrated using the example given in Figure 24.9. The deletion of 14 causes the balance factor of 15 to change from 0 to -1 but it\u2019s height remains the same as before deletion and hence no rotation is needed.<\/p>\r\n&nbsp;\r\n\r\n<img class=\"alignnone size-full wp-image-347 aligncenter\" src=\"http:\/\/csp01.epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/sites\/45\/2018\/07\/1-205.png\" alt=\"\" width=\"596\" height=\"203\" \/>\r\n\r\n&nbsp;\r\n<p style=\"text-align: justify\">We next consider the current node <em>p<\/em> whose balance factor is not 0. Let us see the case where balance factor of <em>p<\/em>is +1 (Figure 24.10) and the taller subtree (here T1) was shortened. Now the balance factor of P becomes 0, the height of the tree is reduced but there is no imbalance and hence there is no need for rotations.<\/p>\r\n&nbsp;\r\n\r\n<img class=\"alignnone size-full wp-image-348 aligncenter\" src=\"http:\/\/csp01.epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/sites\/45\/2018\/07\/1-206.png\" alt=\"\" width=\"621\" height=\"218\" \/>\r\n\r\n&nbsp;\r\n\r\n<strong>24.4.2 Deletion with Single Rotation<\/strong>\r\n\r\n&nbsp;\r\n<p style=\"text-align: justify\">Now let us consider the case shown in Figure 24.11. <strong>The balance factor of<\/strong> <strong><em>q<\/em><\/strong> <strong>is<\/strong> <strong>0.<\/strong>Before deletion the left sub-tree of p had height h and the right sub-tree of p had height=h+1. Now we delete a node from left sub-tree such that it\u2019s height become h- 1.\u00a0\u00a0 Now the balance factor at p changes from -1 to -2 and the AVL condition is violated. Then we need to carry out rebalancing. In this case we need to carry out a single right rotation of q about p where p becomes the left child of q and q\u2019s left child becomes p\u2019s right child.<\/p>\r\n\r\n<\/div>\r\n<img class=\"alignnone size-full wp-image-349 aligncenter\" src=\"http:\/\/csp01.epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/sites\/45\/2018\/07\/1-207.png\" alt=\"\" width=\"638\" height=\"249\" \/>\r\n<div>\r\n\r\n&nbsp;\r\n<p style=\"text-align: justify\">Now let us consider the case shown in Figure 24.12. <strong>The balance factor of<\/strong> <strong><em>q<\/em><\/strong><strong>is<\/strong> <strong>equal to that of p.<\/strong>Before deletion left sub-tree of p from T1, the balance factor of p becomes (h-1) \u2013 (h+1)= -2 and hence there is an imbalance. Now we do a single right rotation of q about P where P becomes left sub-tree of q and T2 becomes right sub-tree of P. The overall height of the tree is reduced.<\/p>\r\n&nbsp;\r\n\r\n&nbsp;\r\n\r\n<img class=\"size-full wp-image-350 aligncenter\" src=\"http:\/\/csp01.epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/sites\/45\/2018\/07\/1-208.png\" alt=\"\" width=\"620\" height=\"230\" \/>\r\n\r\n&nbsp;\r\n\r\n<strong>24.4.2.1 Deletion with Single Right Rotation<\/strong>\r\n\r\n&nbsp;\r\n<p style=\"text-align: justify\">Now let us consider the example of deletion which causes an imbalance and leads to single right rotation (Figure 24.13). Here we assume that 40 is deleted (From T3 of Figure 24.12). This causes an imbalance at node 35 (q). Now to rebalance we carry out single right rotation of 32 (T2) with 35 (q) as the pivot. Now 32 becomes the right child of the parent (30) of 35 while 35 becomes the right child of 32.<\/p>\r\n\r\n<\/div>\r\n&nbsp;\r\n<div>\r\n\r\n<img class=\"alignnone size-full wp-image-351 aligncenter\" src=\"http:\/\/csp01.epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/sites\/45\/2018\/07\/1-209.png\" alt=\"\" width=\"622\" height=\"296\" \/>\r\n\r\n<strong>24.4.2.2 Deletion with Single Left Rotation<\/strong>\r\n\r\n&nbsp;\r\n<p style=\"text-align: justify\">Now let us consider the example of deletion which causes an imbalance and leads to single left rotation (Figure 24.14). Here we assume that 32 is deleted. This causes an imbalance at the root node 44. Now to rebalance we carry out single left rotation of 62 with 44 as the pivot. Now 62 becomes the new root node while 44 becomes the right child of 62. The right sub-tree of 62 rooted at 50 now becomes the right sub-tree of 44.<\/p>\r\n&nbsp;\r\n\r\n<img class=\"alignnone size-full wp-image-352 aligncenter\" src=\"http:\/\/csp01.epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/sites\/45\/2018\/07\/1-210.png\" alt=\"\" width=\"627\" height=\"313\" \/>\r\n\r\n&nbsp;\r\n\r\n<strong> 24.4.3 Deletion with Double Rotation<\/strong>\r\n\r\n&nbsp;\r\n<p style=\"text-align: justify\">In case the balance factors of <em>p<\/em> and <em>q<\/em> are opposite then we need to apply a double rotation (Figure 24.15). The balance factor at q will be 0 or 1. The balance factor of node p will be 0 or -1.Now let us assume p\u2019s left child has height h before deletion. Let us assume that the right child q of p has left sub-tree rooted at r to be of height h-1+1=h or h-2 +1=h-1.The left sub-tree of q has height h-1. Now let us assume that a node is deleted from the left sub-tree of p making it\u2019s height h-1. We first right rotate r about q and then left rotate r about p. Then we set the balance factors of the new root r to be 0.<\/p>\r\n\r\n<\/div>\r\n&nbsp;\r\n<div>\r\n\r\n<img class=\"alignnone size-full wp-image-353 aligncenter\" src=\"http:\/\/csp01.epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/sites\/45\/2018\/07\/1-211.png\" alt=\"\" width=\"621\" height=\"230\" \/>\r\n\r\n&nbsp;\r\n<p style=\"text-align: justify\">For the case of deletion with two or more rotations let us consider the example given in Figure 24.16. The deletion of 40 first causes an imbalance to occur at 35. Now we carry out a right rotate of 32 with 35 as pivot. This causes 32 to become right child of parent (30) of 35 and 35 itself becomes right child of 32. However this rotation causes an imbalance at the root node 30. This requires us to carry out another right rotation of 20 about the pivot 30. This causes 20 to become the new root and the right sub-tree of 20 becomes the right sub-tree of 30. Now the tree is balanced. Please note that in the case of deletion we need to check for imbalance and carry on rebalancing until the tree is balanced. This may cause more than two rotations in some situations.<\/p>\r\n&nbsp;\r\n\r\n<img class=\"alignnone size-full wp-image-354 aligncenter\" src=\"http:\/\/csp01.epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/sites\/45\/2018\/07\/1-212.png\" alt=\"\" width=\"641\" height=\"302\" \/>\r\n\r\n<\/div>\r\n<table style=\"border-collapse: collapse;width: 100%;height: 14px\" border=\"1\">\r\n<tbody>\r\n<tr style=\"height: 14px\">\r\n<td style=\"width: 100%;height: 14px\">\r\n<div>\r\n\r\nAvlTree Delete( ElementType X, AvlTree T ){ if( T == NULL ) Error(\"Item not Found);\r\n\r\n&nbsp;\r\n\r\nelse if( X &lt; T-&gt;Element ){\r\n\r\nT-&gt;Left = Delete( X, T-&gt;Left );\r\n\r\n&nbsp;\r\n\r\nif( Height( T-&gt;Left ) - Height( T-&gt;Right ) == -2 )<strong>\/*Imbalance due to insertion*\/<\/strong> if( Height(T-&gt;Right-&gt;Right) &gt; Height(T-&gt;Right-&gt;Left) )\r\n\r\n&nbsp;\r\n\r\nT = SingleRotateWithRight( T );<strong>\/* RR *\/<\/strong>\r\n\r\nelse\r\n\r\nT = DoubleRotateWithRight( T );\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 <strong>\/* RL *\/<\/strong>\r\n\r\n}\r\n\r\nelse if( X &gt; T-&gt;Element ){\r\n\r\nT-&gt;Right = Delete( X, T-&gt;Right );\r\n\r\n<\/div>\r\n&nbsp;\r\n<div>\r\n\r\nif( Height( T-&gt;Right ) - Height( T-&gt;Left ) == -2 )\r\n\r\nif( Height(T-&gt;Left-&gt;Left) &gt; Height(T-&gt;Left-&gt;Right) )\r\n\r\nT = SingleRotateWithLeft( T );<strong>\/* LL *\/<\/strong>\r\n\r\nelse\r\n\r\nT = DoubleRotateWithLeft( T );\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 <strong>\/* LR *\/<\/strong>\r\n\r\nelse if( T-&gt;Left &amp;&amp; T-&gt;Right ){\u00a0 \/* Found with two children *\/\r\n\r\n\/* Replace with smallest in right subtree *\/\r\n\r\nTmpCell = FindMin( T-&gt;Right );\r\n\r\nT-&gt;Element = TmpCell-&gt;Element;\r\n\r\n&nbsp;\r\n\r\nT-&gt;Right = Delete( T-&gt;Element, T-&gt;Right ); if( Height( T-&gt;Right ) - Height( T-&gt;Left ) == -2 )\r\n\r\nif( Height(T-&gt;Left-&gt;Left) &gt; Height(T-&gt;Left-&gt;Right) )\/*LL*\/\r\n\r\n&nbsp;\r\n\r\nT = SingleRotateWithLeft( T );\r\n\r\n&nbsp;\r\n\r\nelse\r\n\r\nT = DoubleRotateWithLeft( T );\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 \/*LR*\/\r\n\r\n}\r\n\r\nelse {<strong>\/* Found with one or zero child *\/<\/strong>\r\n\r\nTmpCell = T;\r\n\r\n&nbsp;\r\n\r\nT\u00a0 = T-&gt;Left ? T-&gt;Left : T-&gt;Right;<strong>\/* Also handles 0 child *\/<\/strong> free( TmpCell );\r\n\r\n&nbsp;\r\n\r\n}\r\n\r\nif( T!= NULL )\r\n\r\nT-&gt;Height=Max(Height(T-&gt;Left), Height(T-&gt;Right))+1;\u00a0 return T;\r\n\r\n}\r\n\r\n<\/div><\/td>\r\n<\/tr>\r\n<\/tbody>\r\n<\/table>\r\n<div>\r\n\r\n&nbsp;\r\n\r\n<\/div>\r\n<div>\r\n<p style=\"text-align: center\"><strong>Figure 24.17 The Deletion Algorithm <\/strong><\/p>\r\n&nbsp;\r\n\r\nThe details of the deletion algorithm is given in Figure 24.17.\r\n\r\n&nbsp;\r\n\r\n<strong>24.5 Pros and Cons of AVL Trees<\/strong>\r\n\r\n&nbsp;\r\n\r\n<strong>24.5.1 Arguments for AVL trees<\/strong>\r\n\r\n&nbsp;\r\n<p style=\"text-align: justify\">The main advantage of the AVL tree is that it has been so designed that the search time for a node is of the O(log N) for a tree with N nodes since AVL trees are <strong>always<\/strong> <strong>balanced. <\/strong>Insertions and deletions are also of the O(logn). The height balancing needs for rebalancing during insertion and deletion increases the time by only a constant factor.<\/p>\r\n&nbsp;\r\n\r\n<strong>24.5.2 Arguments against using AVL trees<\/strong>\r\n\r\n&nbsp;\r\n<p style=\"text-align: justify\">In general AVL trees are difficult to program and debug and moreover additional space is required to store the balance factor. Even though asymptotically the speed of the operations is faster, rebalancing does cost time.Most large searches are done in database systems on disk and use other structures such as B-trees and not AVL trees. It may sometimes be alright to have O(N) for a single operation in case the total run time for many consecutive operations is fast which is the basis of Splay trees. Both B-trees and Splay trees will be discussed in subsequent modules.<\/p>\r\n&nbsp;\r\n\r\n<strong>Summary<\/strong>\r\n\r\n&nbsp;\r\n\r\n\u2022 Explained the Insertion operations of AVL Trees with illustrative examples\r\n\r\n\u2022 Discussed Deletion from AVL Trees with illustrative examples\r\n\r\n<span style=\"font-size: 1em\">\u2022 Outlined the Pros and Cons of AVL Trees<\/span>\r\n\r\n<\/div>\r\n<table>\r\n<tbody>\r\n<tr>\r\n<td><strong>you can view video on Insertion and Deletion \u2013 AVL Trees<\/strong><\/td>\r\n<td><a href=\"https:\/\/youtu.be\/zPtm3V3eVPY\" 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-355 aligncenter\" src=\"http:\/\/csp01.epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/sites\/45\/2018\/07\/1-213.png\" alt=\"\" width=\"631\" height=\"454\" \/>","rendered":"<div><span style=\"float: right\"><a href=\"https:\/\/youtu.be\/zPtm3V3eVPY\" 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 AVL, a balanced binary search tree. In this module we will discuss the insertion and deletion operations of AVL trees and the rebalancing carried out for re-establishing the balance while maintaining the binary search property.<\/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 To understand the Insertion operations of AVL Trees<\/p>\n<p>\u2022 To discuss Deletion from AVL Trees<\/p>\n<p>\u2022 To Outline the Pros and Cons of AVL Trees<\/p>\n<p>&nbsp;<\/p>\n<p><strong>24.1 Insertion into AVL Trees<\/strong><\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify\">The first step of insertionof a node into the AVL tree is a normal insertion using a BST insertion algorithm. However in addition we have to rebalance the tree if an imbalance occurs. As we have discussed in the last module, an imbalance occurs if a node&#8217;s balance factor changes from -1 to -2 or from+1 to +2.Rebalancing is done at the deepest or lowest unbalanced ancestor of the inserted node.<\/p>\n<p>&nbsp;<\/p>\n<p>There are three cases that can happen during insertion:<\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify\">1. Insertion that does not cause an imbalance.<\/p>\n<p style=\"text-align: justify\">2.Same side (left-left or right-right) insertion that causes an imbalance. This type of insertion requires a single rotation to rebalance.<\/p>\n<p style=\"text-align: justify\">3.Opposite side (left-right or right-left) insertion that causes an imbalance. This type of insertion requires a double rotation to rebalance.<\/p>\n<p>&nbsp;<\/p>\n<p><strong>24.2 Insertion Algorithm<\/strong><\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify\">The first step of the insertion algorithm for AVL trees is the same as insertion into a binary search tree. Here we first find a place for the value to be inserted, and then insert it. Now comes the next step of insertion into the AVL tree which is searching back from the inserted node looking for imbalance. If there is an imbalancewhen a new element is added as the outside grandchild (that is the left grandchild of a left\u00a0<span style=\"font-size: 1em;text-align: initial\">child (left-left) or the right grandchild of a right child (right-right))(Figure 24.1 (a)) we perform single rotation and exit. When a new item is added as an inside grandchild (Figure 24.1 (b)), the imbalance is fixed with a double right or left rotation. As already discussed in the previous module an inside grandchild is when we have left grandchild of a right child (left-right) or the right grandchild of a left child (right-left)).<\/span><\/p>\n<\/div>\n<div>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-341 aligncenter\" src=\"http:\/\/csp01.epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/sites\/45\/2018\/07\/1-199.png\" alt=\"\" width=\"545\" height=\"253\" srcset=\"https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/1-199.png 545w, https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/1-199-300x139.png 300w, https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/1-199-65x30.png 65w, https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/1-199-225x104.png 225w, https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/1-199-350x162.png 350w\" sizes=\"auto, (max-width: 545px) 100vw, 545px\" \/><\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify\">As we have already discussed in the previous module the insert operation may cause balance factor to become +2 or \u20132 for some node. Now only nodes on the path from the insertion point to the root node have possibly changed in height. Therefore after insertion, we go back up to the root node by node, updating heights as we go. If a new balance factor (the difference hleft-hright) is +2 or \u20132, we need to adjust the balance of the tree by <em>rotation<\/em> around the node. Now let us consider a validAVL sub-tree (Figure 24.2). Before the insertion into the subtree M the tree was a valid AVL tree, but after the insertion the AVL property is violated at node j where the balance factor has become +2.<\/p>\n<p>&nbsp;<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-342 aligncenter\" src=\"http:\/\/csp01.epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/sites\/45\/2018\/07\/1-200.png\" alt=\"\" width=\"599\" height=\"282\" srcset=\"https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/1-200.png 599w, https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/1-200-300x141.png 300w, https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/1-200-65x31.png 65w, https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/1-200-225x106.png 225w, https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/1-200-350x165.png 350w\" sizes=\"auto, (max-width: 599px) 100vw, 599px\" \/><\/p>\n<p><strong style=\"text-align: initial;font-size: 1em\">24.3 Rebalancing<\/strong><\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify\"><span style=\"text-align: justify;font-size: 1em\">Rebalancing is done at the deepest unbalanced ancestor of the inserted node. Now let the node that needs rebalancing be denoted as X. The rebalancing is performed through four separate types of rotations as given below:<\/span><\/p>\n<\/div>\n<div>\n<p><strong>\u00a0 \u00a0 Outside Cases (require single rotation) :<\/strong><\/p>\n<p>&nbsp;<\/p>\n<p>1.\u00a0 Insertion into left subtree of left child of X &#8211; <strong>(Single Right Rotation)<\/strong><\/p>\n<p>2.\u00a0 Insertion into right subtree of right child of X \u2013 <strong>(Single Left Rotation)<\/strong><\/p>\n<p>&nbsp;<\/p>\n<p><strong>Inside Cases (require double rotation) :<\/strong><\/p>\n<p>&nbsp;<\/p>\n<p>3.\u00a0 Insertion into right subtree of left child of X \u2013 <strong>(Left-Right Rotation)<\/strong><\/p>\n<p>4.\u00a0 Insertion into left subtree of right child of X \u2013 <strong>(Right-Left Rotation)<\/strong><\/p>\n<p>&nbsp;<\/p>\n<p><strong>24.3.1 Insertion into left sub-tree of left child of X<\/strong><\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify\">Let us consider the <strong>first case of insertion into left sub-tree of left child of X<\/strong> (Figure 24.3). Here we have inserted 7 as left sub-tree of left child (8) of X (9). It is at X that the balancing is violated with balance factor of X becoming +2. Now this node X is the pivot. This imbalance occurred when the new element (7) was added to the left sub-tree of the outside left grandchild that is we have the case of <strong>single right<\/strong> <strong>rotation.<\/strong><\/p>\n<p>&nbsp;<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-343 aligncenter\" src=\"http:\/\/csp01.epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/sites\/45\/2018\/07\/1-201.png\" alt=\"\" width=\"629\" height=\"327\" srcset=\"https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/1-201.png 629w, https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/1-201-300x156.png 300w, https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/1-201-65x34.png 65w, https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/1-201-225x117.png 225w, https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/1-201-350x182.png 350w\" sizes=\"auto, (max-width: 629px) 100vw, 629px\" \/><\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify\">Now we carry out single right rotation of 8 about 9. In this case 8 becomes the right child of the parent (6) of 9 while 9 becomes the right child of 8. The BST property of the AVL tree is now maintained.<\/p>\n<p>&nbsp;<\/p>\n<p><strong>24.3.2 Insertion into right sub-tree of right child of X<\/strong><\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify\">Let us consider the <strong>second case of insertion into right sub-tree of right child of<\/strong> <strong>X <\/strong>(Figure 24.4). Here we have inserted 45 as right sub-tree of right child (40) of X\u00a0<span style=\"text-align: initial;font-size: 1em\">(35). It is at X that the balancing is violated with balance factor of X becoming -2. Now the node X is the pivot. This imbalance occurred when the new element (45) was added to the right sub-tree of the outside right grandchild that is we have the case of <\/span><strong style=\"text-align: initial;font-size: 1em\">single left rotation.<\/strong><span style=\"text-align: initial;font-size: 1em\">Now we carry out single left rotation of 40 about 35. In this case 40 becomes the right child of the parent (30) of 35 while 35 becomes the left child of 40. The BST property of the AVL tree is now maintained.<\/span><\/p>\n<\/div>\n<div>\n<p>&nbsp;<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-344 aligncenter\" src=\"http:\/\/csp01.epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/sites\/45\/2018\/07\/1-202.png\" alt=\"\" width=\"601\" height=\"580\" srcset=\"https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/1-202.png 601w, https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/1-202-300x290.png 300w, https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/1-202-65x63.png 65w, https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/1-202-225x217.png 225w, https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/1-202-350x338.png 350w\" sizes=\"auto, (max-width: 601px) 100vw, 601px\" \/><\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify\">Let us consider the <strong>third case of insertion into left sub-tree of right child of X<\/strong> (Figure 24.5). Here we have inserted 34 as left sub-tree of right child (40) of X (30). Balance is violated at X balance factor becoming -2. <strong>Now we carry out two<\/strong> <strong>rotations that is a right rotation followed by a left rotation<\/strong>:<\/p>\n<ul>\n<li style=\"text-align: justify\">The first is a <strong>single right rotate<\/strong> of 35 about the first pivot (40). Here 35 becomes the new right child of X while 40 becomes the right child of 35.<\/li>\n<li style=\"text-align: justify\">Next we carry out a <strong>single left rotate<\/strong> of 35 about second pivot node X (30). In this case 35 becomes the left child of the parent (20) of 30 while 30 becomes the new left child of 35. The BST property of the AVL tree is now maintained.<\/li>\n<\/ul>\n<p>&nbsp;<\/p>\n<p><strong>24.3.4 Insertion into right sub-tree of left child of X<\/strong><\/p>\n<\/div>\n<div>\n<p style=\"text-align: justify\">Let us consider the <strong>fourth case of insertion into right sub-tree of left child of X<\/strong> (Figure 24.6). Here we have inserted 7 as right sub-tree of left child (5) of X (10). Balance is violated at X balance factor becoming +2. When a new item (7) is added to the sub-tree oftheinside grandchild, the imbalance is fixed with a double rotation. Now we carry out two rotations that is a <strong>left rotation followed by a right rotation:<\/strong><\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-345 aligncenter\" src=\"http:\/\/csp01.epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/sites\/45\/2018\/07\/1-203.png\" alt=\"\" width=\"569\" height=\"295\" srcset=\"https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/1-203.png 569w, https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/1-203-300x156.png 300w, https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/1-203-65x34.png 65w, https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/1-203-225x117.png 225w, https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/1-203-350x181.png 350w\" sizes=\"auto, (max-width: 569px) 100vw, 569px\" \/><\/p>\n<table style=\"border-collapse: collapse;width: 100%\">\n<tbody>\n<tr>\n<td style=\"width: 100%\">\n<div>\n<p>AvlTree <strong>Insert<\/strong>( ElementType X, AvlTree T )<strong>{<\/strong> if( T == NULL ){<\/p>\n<p>&nbsp;<\/p>\n<p><strong>\/* Create and return a one-node tree *\/<\/strong><\/p>\n<p>&nbsp;<\/p>\n<p>T = malloc( sizeof( struct AvlNode ) );<\/p>\n<p>&nbsp;<\/p>\n<p>if( T == NULL )<\/p>\n<p>&nbsp;<\/p>\n<p>FatalError( &#8220;Out of space!!!&#8221; );<\/p>\n<p>&nbsp;<\/p>\n<p>else {<\/p>\n<p>&nbsp;<\/p>\n<p>T-&gt;Element = X; T-&gt;Height = 0;<\/p>\n<p>&nbsp;<\/p>\n<p>T-&gt;Left = T-&gt;Right = NULL;<\/p>\n<p>&nbsp;<\/p>\n<p>}<\/p>\n<p>&nbsp;<\/p>\n<p>}<\/p>\n<p>&nbsp;<\/p>\n<p>else if( X &lt; T-&gt;Element ){<\/p>\n<p>&nbsp;<\/p>\n<p>T-&gt;Left = Insert( X, T-&gt;Left );<strong>\/* Insertion at the left*\/<\/strong> if( Height( T-&gt;Left ) &#8211; Height( T-&gt;Right ) == 2 ) if( X &lt; T-&gt;Left-&gt;Element )<\/p>\n<p>&nbsp;<\/p>\n<p>T = SingleRotateWithLeft( T ); <strong>\/* LL *\/<\/strong><\/p>\n<p>&nbsp;<\/p>\n<p>else<\/p>\n<p>&nbsp;<\/p>\n<p>T = DoubleRotateWithLeft( T ); <strong>\/* LR *\/<\/strong> } else if( X &gt; T-&gt;Element ){<\/p>\n<p>&nbsp;<\/p>\n<p>T-&gt;Right = Insert( X, T-&gt;Right ); <strong>\/* Insertion at the right*\/<\/strong> if( Height( T-&gt;Right ) &#8211; Height( T-&gt;Left ) == 2 ) if( X &gt; T-&gt;Right-&gt;Element )<\/p>\n<p>&nbsp;<\/p>\n<p>T = SingleRotateWithRight( T ); <strong>\/* RR *\/<\/strong><\/p>\n<p>&nbsp;<\/p>\n<p>else<\/p>\n<p>&nbsp;<\/p>\n<p>T = DoubleRotateWithRight( T ); <strong>\/* RL *\/<\/strong><\/p>\n<\/div>\n<p><strong>\u00a0<\/strong><\/p>\n<div>\n<p>} <strong>\/* Else X is in the tree already; we&#8217;ll do nothing *\/<\/strong> T-&gt;Height=Max(Height(T-&gt;Left),Height(T-&gt;Right))+1;<\/p>\n<p>&nbsp;<\/p>\n<p>return T;<\/p>\n<p>&nbsp;<\/p>\n<p><strong>}<\/strong><\/p>\n<\/div>\n<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>&nbsp;<\/p>\n<\/div>\n<div>\n<p style=\"text-align: center\"><strong>Figure 24.7 The Insertion Algorithm<\/strong><\/p>\n<p>&nbsp;<\/p>\n<ul>\n<li style=\"text-align: justify\">The first is a <strong>single left rotate<\/strong> of 6 about the first pivot (5). Here 6 becomes the new left child of the parent (10)of 5 while 5 becomes the left child of 6.<\/li>\n<li style=\"text-align: justify\">Next we carry out a <strong>single right rotate<\/strong> of 6 about the second pivot node X (10). In this case 6 becomes the left child of parent (13) of X (10) while 10 becomes the new right child of 6. The BST property of the AVL tree is now maintained<\/li>\n<\/ul>\n<p>The detailed algorithm for insertion is given in Figure 24.7.<\/p>\n<p>&nbsp;<\/p>\n<p><strong>24.4 Deletion<\/strong><\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify\">The first step in deletion is deleting the node to be deleted X as in an ordinary binary search tree. Note that whatever may be the cases of deletion from the binary search tree, the last node deleted is a leaf.The deletion may result in an imbalance. Now we follow the path from the new leaf towards the root.For each node X encountered, we need to check if heights of left(X) and right(X) differ by at most 1. If yes, proceed to the parent(X) which now becomes the new X. If not, that is the heights differ by more than 1 then we need to perform an appropriate rotation at X. There are 4 cases as in the case of insertion. In the case of deletion, after we perform a rotation at X, we may have to perform a rotation at some ancestor of X. Thus, we must continue to trace the path until we reach the root. Now we will discuss the case of deletion where no balancing is needed and the cases where rebalancing of the tree is needed when an imbalance occurs after deletion.<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-346 aligncenter\" src=\"http:\/\/csp01.epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/sites\/45\/2018\/07\/1-204.png\" alt=\"\" width=\"557\" height=\"160\" srcset=\"https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/1-204.png 557w, https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/1-204-300x86.png 300w, https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/1-204-65x19.png 65w, https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/1-204-225x65.png 225w, https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/1-204-350x101.png 350w\" sizes=\"auto, (max-width: 557px) 100vw, 557px\" \/><\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: center\"><strong>Figure 24.8Deletion with no Imbalance with node P having balance factor=0<\/strong><\/p>\n<p>&nbsp;<\/p>\n<p>We can consider three cases for deletion:<\/p>\n<p>&nbsp;<\/p>\n<p>1. Deletion that does not cause an imbalance.<\/p>\n<p>2.Deletion that requires a single rotation to rebalance.<\/p>\n<p>3.Deletion that requires two or more rotations to rebalance.<\/p>\n<p>&nbsp;<\/p>\n<p><strong>24.4.1 Deletion with no Imbalance<\/strong><\/p>\n<\/div>\n<div>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify\">Let us consider the simplest case of deletion that is deletion which causes no imbalance. We first consider the current node <em>p<\/em>that has sub-trees T1 and T2 with equal heights and so the <strong>balance factor of pis zero<\/strong> (Figure 24.8).When deletion occurs say in the left sub-tree T1, it\u2019s height is decreased but the height of p remains unchanged. The balance factor of <em>p<\/em>becomes (-1). This is allowed so there is no imbalance. This is illustrated using the example given in Figure 24.9. The deletion of 14 causes the balance factor of 15 to change from 0 to -1 but it\u2019s height remains the same as before deletion and hence no rotation is needed.<\/p>\n<p>&nbsp;<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-347 aligncenter\" src=\"http:\/\/csp01.epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/sites\/45\/2018\/07\/1-205.png\" alt=\"\" width=\"596\" height=\"203\" srcset=\"https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/1-205.png 596w, https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/1-205-300x102.png 300w, https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/1-205-65x22.png 65w, https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/1-205-225x77.png 225w, https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/1-205-350x119.png 350w\" sizes=\"auto, (max-width: 596px) 100vw, 596px\" \/><\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify\">We next consider the current node <em>p<\/em> whose balance factor is not 0. Let us see the case where balance factor of <em>p<\/em>is +1 (Figure 24.10) and the taller subtree (here T1) was shortened. Now the balance factor of P becomes 0, the height of the tree is reduced but there is no imbalance and hence there is no need for rotations.<\/p>\n<p>&nbsp;<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-348 aligncenter\" src=\"http:\/\/csp01.epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/sites\/45\/2018\/07\/1-206.png\" alt=\"\" width=\"621\" height=\"218\" srcset=\"https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/1-206.png 621w, https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/1-206-300x105.png 300w, https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/1-206-65x23.png 65w, https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/1-206-225x79.png 225w, https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/1-206-350x123.png 350w\" sizes=\"auto, (max-width: 621px) 100vw, 621px\" \/><\/p>\n<p>&nbsp;<\/p>\n<p><strong>24.4.2 Deletion with Single Rotation<\/strong><\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify\">Now let us consider the case shown in Figure 24.11. <strong>The balance factor of<\/strong> <strong><em>q<\/em><\/strong> <strong>is<\/strong> <strong>0.<\/strong>Before deletion the left sub-tree of p had height h and the right sub-tree of p had height=h+1. Now we delete a node from left sub-tree such that it\u2019s height become h- 1.\u00a0\u00a0 Now the balance factor at p changes from -1 to -2 and the AVL condition is violated. Then we need to carry out rebalancing. In this case we need to carry out a single right rotation of q about p where p becomes the left child of q and q\u2019s left child becomes p\u2019s right child.<\/p>\n<\/div>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-349 aligncenter\" src=\"http:\/\/csp01.epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/sites\/45\/2018\/07\/1-207.png\" alt=\"\" width=\"638\" height=\"249\" srcset=\"https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/1-207.png 638w, https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/1-207-300x117.png 300w, https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/1-207-65x25.png 65w, https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/1-207-225x88.png 225w, https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/1-207-350x137.png 350w\" sizes=\"auto, (max-width: 638px) 100vw, 638px\" \/><\/p>\n<div>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify\">Now let us consider the case shown in Figure 24.12. <strong>The balance factor of<\/strong> <strong><em>q<\/em><\/strong><strong>is<\/strong> <strong>equal to that of p.<\/strong>Before deletion left sub-tree of p from T1, the balance factor of p becomes (h-1) \u2013 (h+1)= -2 and hence there is an imbalance. Now we do a single right rotation of q about P where P becomes left sub-tree of q and T2 becomes right sub-tree of P. The overall height of the tree is reduced.<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"size-full wp-image-350 aligncenter\" src=\"http:\/\/csp01.epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/sites\/45\/2018\/07\/1-208.png\" alt=\"\" width=\"620\" height=\"230\" srcset=\"https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/1-208.png 620w, https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/1-208-300x111.png 300w, https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/1-208-65x24.png 65w, https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/1-208-225x83.png 225w, https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/1-208-350x130.png 350w\" sizes=\"auto, (max-width: 620px) 100vw, 620px\" \/><\/p>\n<p>&nbsp;<\/p>\n<p><strong>24.4.2.1 Deletion with Single Right Rotation<\/strong><\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify\">Now let us consider the example of deletion which causes an imbalance and leads to single right rotation (Figure 24.13). Here we assume that 40 is deleted (From T3 of Figure 24.12). This causes an imbalance at node 35 (q). Now to rebalance we carry out single right rotation of 32 (T2) with 35 (q) as the pivot. Now 32 becomes the right child of the parent (30) of 35 while 35 becomes the right child of 32.<\/p>\n<\/div>\n<p>&nbsp;<\/p>\n<div>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-351 aligncenter\" src=\"http:\/\/csp01.epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/sites\/45\/2018\/07\/1-209.png\" alt=\"\" width=\"622\" height=\"296\" srcset=\"https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/1-209.png 622w, https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/1-209-300x143.png 300w, https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/1-209-65x31.png 65w, https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/1-209-225x107.png 225w, https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/1-209-350x167.png 350w\" sizes=\"auto, (max-width: 622px) 100vw, 622px\" \/><\/p>\n<p><strong>24.4.2.2 Deletion with Single Left Rotation<\/strong><\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify\">Now let us consider the example of deletion which causes an imbalance and leads to single left rotation (Figure 24.14). Here we assume that 32 is deleted. This causes an imbalance at the root node 44. Now to rebalance we carry out single left rotation of 62 with 44 as the pivot. Now 62 becomes the new root node while 44 becomes the right child of 62. The right sub-tree of 62 rooted at 50 now becomes the right sub-tree of 44.<\/p>\n<p>&nbsp;<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-352 aligncenter\" src=\"http:\/\/csp01.epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/sites\/45\/2018\/07\/1-210.png\" alt=\"\" width=\"627\" height=\"313\" srcset=\"https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/1-210.png 627w, https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/1-210-300x150.png 300w, https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/1-210-65x32.png 65w, https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/1-210-225x112.png 225w, https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/1-210-350x175.png 350w\" sizes=\"auto, (max-width: 627px) 100vw, 627px\" \/><\/p>\n<p>&nbsp;<\/p>\n<p><strong> 24.4.3 Deletion with Double Rotation<\/strong><\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify\">In case the balance factors of <em>p<\/em> and <em>q<\/em> are opposite then we need to apply a double rotation (Figure 24.15). The balance factor at q will be 0 or 1. The balance factor of node p will be 0 or -1.Now let us assume p\u2019s left child has height h before deletion. Let us assume that the right child q of p has left sub-tree rooted at r to be of height h-1+1=h or h-2 +1=h-1.The left sub-tree of q has height h-1. Now let us assume that a node is deleted from the left sub-tree of p making it\u2019s height h-1. We first right rotate r about q and then left rotate r about p. Then we set the balance factors of the new root r to be 0.<\/p>\n<\/div>\n<p>&nbsp;<\/p>\n<div>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-353 aligncenter\" src=\"http:\/\/csp01.epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/sites\/45\/2018\/07\/1-211.png\" alt=\"\" width=\"621\" height=\"230\" srcset=\"https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/1-211.png 621w, https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/1-211-300x111.png 300w, https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/1-211-65x24.png 65w, https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/1-211-225x83.png 225w, https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/1-211-350x130.png 350w\" sizes=\"auto, (max-width: 621px) 100vw, 621px\" \/><\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify\">For the case of deletion with two or more rotations let us consider the example given in Figure 24.16. The deletion of 40 first causes an imbalance to occur at 35. Now we carry out a right rotate of 32 with 35 as pivot. This causes 32 to become right child of parent (30) of 35 and 35 itself becomes right child of 32. However this rotation causes an imbalance at the root node 30. This requires us to carry out another right rotation of 20 about the pivot 30. This causes 20 to become the new root and the right sub-tree of 20 becomes the right sub-tree of 30. Now the tree is balanced. Please note that in the case of deletion we need to check for imbalance and carry on rebalancing until the tree is balanced. This may cause more than two rotations in some situations.<\/p>\n<p>&nbsp;<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-354 aligncenter\" src=\"http:\/\/csp01.epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/sites\/45\/2018\/07\/1-212.png\" alt=\"\" width=\"641\" height=\"302\" srcset=\"https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/1-212.png 641w, https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/1-212-300x141.png 300w, https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/1-212-65x31.png 65w, https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/1-212-225x106.png 225w, https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/1-212-350x165.png 350w\" sizes=\"auto, (max-width: 641px) 100vw, 641px\" \/><\/p>\n<\/div>\n<table style=\"border-collapse: collapse;width: 100%;height: 14px\">\n<tbody>\n<tr style=\"height: 14px\">\n<td style=\"width: 100%;height: 14px\">\n<div>\n<p>AvlTree Delete( ElementType X, AvlTree T ){ if( T == NULL ) Error(&#8220;Item not Found);<\/p>\n<p>&nbsp;<\/p>\n<p>else if( X &lt; T-&gt;Element ){<\/p>\n<p>T-&gt;Left = Delete( X, T-&gt;Left );<\/p>\n<p>&nbsp;<\/p>\n<p>if( Height( T-&gt;Left ) &#8211; Height( T-&gt;Right ) == -2 )<strong>\/*Imbalance due to insertion*\/<\/strong> if( Height(T-&gt;Right-&gt;Right) &gt; Height(T-&gt;Right-&gt;Left) )<\/p>\n<p>&nbsp;<\/p>\n<p>T = SingleRotateWithRight( T );<strong>\/* RR *\/<\/strong><\/p>\n<p>else<\/p>\n<p>T = DoubleRotateWithRight( T );\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 <strong>\/* RL *\/<\/strong><\/p>\n<p>}<\/p>\n<p>else if( X &gt; T-&gt;Element ){<\/p>\n<p>T-&gt;Right = Delete( X, T-&gt;Right );<\/p>\n<\/div>\n<p>&nbsp;<\/p>\n<div>\n<p>if( Height( T-&gt;Right ) &#8211; Height( T-&gt;Left ) == -2 )<\/p>\n<p>if( Height(T-&gt;Left-&gt;Left) &gt; Height(T-&gt;Left-&gt;Right) )<\/p>\n<p>T = SingleRotateWithLeft( T );<strong>\/* LL *\/<\/strong><\/p>\n<p>else<\/p>\n<p>T = DoubleRotateWithLeft( T );\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 <strong>\/* LR *\/<\/strong><\/p>\n<p>else if( T-&gt;Left &amp;&amp; T-&gt;Right ){\u00a0 \/* Found with two children *\/<\/p>\n<p>\/* Replace with smallest in right subtree *\/<\/p>\n<p>TmpCell = FindMin( T-&gt;Right );<\/p>\n<p>T-&gt;Element = TmpCell-&gt;Element;<\/p>\n<p>&nbsp;<\/p>\n<p>T-&gt;Right = Delete( T-&gt;Element, T-&gt;Right ); if( Height( T-&gt;Right ) &#8211; Height( T-&gt;Left ) == -2 )<\/p>\n<p>if( Height(T-&gt;Left-&gt;Left) &gt; Height(T-&gt;Left-&gt;Right) )\/*LL*\/<\/p>\n<p>&nbsp;<\/p>\n<p>T = SingleRotateWithLeft( T );<\/p>\n<p>&nbsp;<\/p>\n<p>else<\/p>\n<p>T = DoubleRotateWithLeft( T );\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 \/*LR*\/<\/p>\n<p>}<\/p>\n<p>else {<strong>\/* Found with one or zero child *\/<\/strong><\/p>\n<p>TmpCell = T;<\/p>\n<p>&nbsp;<\/p>\n<p>T\u00a0 = T-&gt;Left ? T-&gt;Left : T-&gt;Right;<strong>\/* Also handles 0 child *\/<\/strong> free( TmpCell );<\/p>\n<p>&nbsp;<\/p>\n<p>}<\/p>\n<p>if( T!= NULL )<\/p>\n<p>T-&gt;Height=Max(Height(T-&gt;Left), Height(T-&gt;Right))+1;\u00a0 return T;<\/p>\n<p>}<\/p>\n<\/div>\n<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<div>\n<p>&nbsp;<\/p>\n<\/div>\n<div>\n<p style=\"text-align: center\"><strong>Figure 24.17 The Deletion Algorithm <\/strong><\/p>\n<p>&nbsp;<\/p>\n<p>The details of the deletion algorithm is given in Figure 24.17.<\/p>\n<p>&nbsp;<\/p>\n<p><strong>24.5 Pros and Cons of AVL Trees<\/strong><\/p>\n<p>&nbsp;<\/p>\n<p><strong>24.5.1 Arguments for AVL trees<\/strong><\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify\">The main advantage of the AVL tree is that it has been so designed that the search time for a node is of the O(log N) for a tree with N nodes since AVL trees are <strong>always<\/strong> <strong>balanced. <\/strong>Insertions and deletions are also of the O(logn). The height balancing needs for rebalancing during insertion and deletion increases the time by only a constant factor.<\/p>\n<p>&nbsp;<\/p>\n<p><strong>24.5.2 Arguments against using AVL trees<\/strong><\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify\">In general AVL trees are difficult to program and debug and moreover additional space is required to store the balance factor. Even though asymptotically the speed of the operations is faster, rebalancing does cost time.Most large searches are done in database systems on disk and use other structures such as B-trees and not AVL trees. It may sometimes be alright to have O(N) for a single operation in case the total run time for many consecutive operations is fast which is the basis of Splay trees. Both B-trees and Splay trees will be discussed in subsequent modules.<\/p>\n<p>&nbsp;<\/p>\n<p><strong>Summary<\/strong><\/p>\n<p>&nbsp;<\/p>\n<p>\u2022 Explained the Insertion operations of AVL Trees with illustrative examples<\/p>\n<p>\u2022 Discussed Deletion from AVL Trees with illustrative examples<\/p>\n<p><span style=\"font-size: 1em\">\u2022 Outlined the Pros and Cons of AVL Trees<\/span><\/p>\n<\/div>\n<table>\n<tbody>\n<tr>\n<td><strong>you can view video on Insertion and Deletion \u2013 AVL Trees<\/strong><\/td>\n<td><a href=\"https:\/\/youtu.be\/zPtm3V3eVPY\" 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-355 aligncenter\" src=\"http:\/\/csp01.epgpbooks.inflibnet.ac.in\/wp-content\/uploads\/sites\/45\/2018\/07\/1-213.png\" alt=\"\" width=\"631\" height=\"454\" srcset=\"https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/1-213.png 631w, https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/1-213-300x216.png 300w, https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/1-213-65x47.png 65w, https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/1-213-225x162.png 225w, https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-content\/uploads\/sites\/45\/2018\/07\/1-213-350x252.png 350w\" sizes=\"auto, (max-width: 631px) 100vw, 631px\" \/><\/p>\n","protected":false},"author":3,"menu_order":24,"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-340","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\/340","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":7,"href":"https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-json\/pressbooks\/v2\/chapters\/340\/revisions"}],"predecessor-version":[{"id":954,"href":"https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-json\/pressbooks\/v2\/chapters\/340\/revisions\/954"}],"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\/340\/metadata\/"}],"wp:attachment":[{"href":"https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-json\/wp\/v2\/media?parent=340"}],"wp:term":[{"taxonomy":"chapter-type","embeddable":true,"href":"https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-json\/pressbooks\/v2\/chapter-type?post=340"},{"taxonomy":"contributor","embeddable":true,"href":"https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-json\/wp\/v2\/contributor?post=340"},{"taxonomy":"license","embeddable":true,"href":"https:\/\/ebooks.inflibnet.ac.in\/csp01\/wp-json\/wp\/v2\/license?post=340"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}