# DAA 18 Oct

- **Course:** 5101 - Design & Analysis of Algo
- **Recorded:** 2026-10-18 00:00
- **Transcript:** 461 segments, 8335 words
- **Made by:** deepdml/faster-whisper-large-v3-turbo-ct2

---
So, let us start. So, in the last lecture, we have discussed about the binary search tree or binary sorted tree that is called BST which holds the property that all the entry or all the info part in the left side should be less than the root and which is further less than the right entry. So, this is an important property of binary search tree to maintain searching time order of log of n. And we have also discussed that in BST, insertion and deletion is deletions required search first. okay so we will first search whether that element is exist or not so of course in deletion that is mandate that we have to first search but in in case of insertion also we have to first search whether that element exist or not why because we know that BST does not allow allow duplicate element BST does not allow duplicate element and this is important property that yesterday we have also discussed that due to this property we can easily remove

the redundant entries from the list of an element or database or anywhere so you can say that bst helps in avoiding or in removing redundant entries binary search tree help in removing redundant entries. Now, we have further understood the problem of skewness in BST. Why? Because we are maintaining left should be less than root which is further less than right. So, we may encounter element sorted in increasing or decreasing order. So, So, we may encounter element sorted in increasing or decreasing order. In that case, we will either get left skewed or right skewed tree. So, now this problem is critical because when we have these two skewness, our searching time will reach to order of linear search that is order of n and the whole exercise of creating binary search tree from the list and then applying searching algorithm that imposes the computational complexity of order of n will so better to go with linear search so in this

situation, a condition arises if the height of left and right size is comparable, then the computational complexity will be in the bound of log of n. So it is said that if the height of left subpart is comparable with right subpart, then we will reach the computational complexity of original scope of log of n. So in that way, we have understood the tree that is called avial tree that is further known as balance binary search tree. So how it balances, it uses a term okay that is called balance factor balance factor which is difference of of left height minus right height and in avl the allowed balance factor is 0 1 or minus 1. so if the difference between left height and if the difference between left height and right height height is these three then it is avl avl tree okay and condition of course it will be a VST first. So, this is the thing that avial tree solves the problem of skewedness in binary search tree and avoid the situation of log of n computational complexity. This is equivalent

to the linear one. So, this arises the need for maintaining binary search tree to be balanced. So, that was the problem and then we understand that avial tree the balance factor should be and here we are using a modular sign. So, balance factor could be either 0 or 1 but in case of AVL, this is a special thing that it should be 0, 1 or minus 1. So, there we are not using the modular. So, here this is height of tree left minus height of tree right. So, this is just a rotational purpose but in AVL, this is AVL tree balance factor nodes can be the 0, 1 or minus 1 that you have to remember the height difference. Okay, so we consider one example that is ACGD. Okay, here this is a tree. So, balance factor of leaf node is left height 0, right height 0, balance factor this one. Here if you see left height 0, right height 0, balance factor is 0. For G, left height is 1, right height is 0, so it become 1. For C, left height is 1,

right height is 2 so it is minus 1. So balance vector could be either 0, 1 or minus 1 that is there. Now one thing is that if we do the search or traversal we are not changing the structure of the tree and therefore whatever the technique we have studied in BST searching will remain intact for AVL tree as well. Okay. So, if I say one more thing, the search and traversal operation in AVL tree do not violate the balance factor property. Okay. Thus, we can use bst search and traversal okay but if you see when we do any insertion or deletion it will disturb the balance factor so likewise if you see here one example we have seen while adding the e so insertion will follow the bst first it will go to c then c say okay you are e so you can go to my right but it go to g g say that you can go to my left then d say that you can go to my right okay so this is important this story you should remember k whenever a new node come it will

first reach to the root okay it will first go to the root and then it will first go to the root and then identify the path of movement okay and then identify the path of movement so first it go to c then then it will go to g g say you can be placed on my left and then it will go to the left that is d then after d there is no availability of e then it will be of course insert okay so this is the phenomenon now if you see so for a this is a leaf node no left height no right height, 0. For E, no left height, right height, it is 0. For D, no left height, but right height is 1. So, 0 minus 1, of course, it will be minus 1. Now, for G, left height is 2 and right height is 0. So, it becomes 2. And for C, if you see, left height is 1 and right height is 1, 2, 3. So, that is 1 minus 3. So, it becomes minus 2. So, here what we have said that in case of avial tree, the balance factor allowed is 0, 1 or minus 1. But here we are getting what?

Minus 2 and 2. So, how to restore this property? That is important. So, to rotate, So, we perform different rotations to identify the specific node A whose balance factor is neither 0, 1 or minus 1. That is the case, that is the horizon. One more important thing is that we do some rotations to restore the balance factor property. Now, one more thing is that the rotation will be performed on the first node from the point of insertion whose balance factor is violated. What this means if I insert E here and we can see there are two nodes having different balance factor minus 2 and 2. So the rotation will be performed along the first node from the point of insertion. The first node from the point of insertion is what? G that is violating the balance factor property. So we should not do the rotation on the C. We will first perform the rotation on the G. And if G rotation does not balance your tree, then we will further perform

other rotation. But initially, we will perform the rotation along the G. So, what are the rotation and when and why? Which one is needed? We will now explore. So, there are primarily four types of rotations that are being performed in the avial tree. Okay. So, it depends upon where we are placing the node. Okay. So, let me show you something. If let us assume this is your tree. Now, there are four rotations that is LL, RR, LR and RL. One by one. So, if we insert into the left of left sub tree, then what we will perform? Left of left sub tree. So, this is called left of left subtree. Then what we will do? We will perform LL rotation. We will understand what is LL rotation. Similarly, we insert node Y here. So, for X. So, we have to insert, where we have inserted? Right of right subtree. So, we will perform what? RR rotation. this is right of right of right sub tree. So, this we will check when there is a violation of

balance factor property. Otherwise, no need to rotate. Okay. Otherwise, no need to rotate. LR will be inserting node into the right of left sub tree. This is inserting node into the right of left subtree. So, what I am inserting, I am inserting some g here. So, right of left subtree about the g. So, here we will study from left of left subtree. So, we will study from right to left. So, these symbols are from right. So, inserting right of left subtree. That is LR. And similarly, left of right subtree. So, if I insert something here, let us say A. So, that is inserting left of right subtree. So, for A. So, these are the four points. So, if we are inserting into the left of left subtree, right of right subtree, left of right subtree and right of left subtree, these are the four terms. So, do not confuse, you have to read from right to left. So, first one says that the point of insertion and second one says about the

in which subtree we are targeting, okay. And these rotations will only require these rotations will restore the property of AVL, okay. It may be the situation where rotations can be perform in multiple rounds. It may be the multiple round means that one rotation we have performed but the property does not restore then we will perform second rotation, third rotation and so on. So now and this is only applicable when your balance factor is not in the range of 0, 1 or minus 1. That is very important. You should remember your rotation should be 0, 1 or minus 1. Now, talking about the first rotation, so this is a representative example that we are witnessing now. So, here we have a node A, we have another node B that is in the left subtree, left. Now, let us assume the height is h here and here the height is h. Now, if we insert something here then what will happen this become h plus 1 then the balance

factor of b changes to what from 0 it will be h plus 1 minus h then it becomes 1. Similarly when you reach to the location a so h plus 1 plus 1 h plus 2 and the hair height is only h if you see here. So, what will happen? h plus 2 minus h. So, we will get a balance factor of 2. Now, we have inserted where? In the left of left subtree. So, the rotation will happen toward this direction. This is toward the right because we have added in the left. So, rotation will happen in the right. So, what rotation means? So, b go up, a becomes the right child of B. If you see, B go up, A become the right child of A. And this A and B are only the representative node. So, any node can be there. You can assign one is A, one is B. Now, the right child of B become the left child of A. This is the situation. When B go up, so what will happen b is already having two child and a become the right child of b so where this

br will go it becomes the left child of a this is the situation this is the ll rotation okay so just you have to see if you see here in the last example where we have added we have added if you consider this subtree in the right of left subtree okay so this we will see afterwards when when we will compile all the four rotation. But of course, I hope this is clear. Now let us consider this situation. So we have this tree 96, 85, 110, 64 and 90. So we calculate the balance factor of 64 it is 0 of 90 it is 0 because there is no left and right child. Similarly, for 85 it is 0, for 110 it is 0, for 85 it is 0 because 1 minus 1, for 100 there is no, for 96 the left height is 1, 2 and right height is 1, so 2 minus 1 that is 1, fine. Now, if I ask to insert 36 here, so 36 will first go to 96, 96 says go to my left, 85 says go to my left, 64 says go to my left. So, where we have added? Left of left sub 3. So, 36 is inserted. Now, let us calculate the

balance factor. So, balance factor is 0, 90, 0, 110, 0. 64, the balance factor is what? 1 minus 0, 1. 85, the balance factor is 1, 2, 1. So, 2 minus 1, that is 1. Now, for 96, the balance factor is 1, 2, 3 minus 1. So, 3 minus 1 will become 2. So, now assign the node, this become A and this become B and then what will happen 85 will go up 96 become the right child of 85 and 85 right child become the left child of A. So, three things are happening 85 goes up 96 become the right child of 85 and 85 right child become the left child of A. So, this is the situation. Now, after what if you calculate the balance factor. So, what we find that for 36 it is 0, for 90 it is 0, for 110 it is 0, 64 it is 1 minus 0, 1, 96 0, 1 minus 1, 0 and for 85 the balance factor is 0 because it is 2 minus 2. So, in this way we have restored the property of the avial tree when the insertion was happening into the left of left something. So, in the same example, if I insert 18, let

us check the situation. If I insert, just give me a moment. In the same example, if I wish to insert 18. So, the example is, again I am noting, so it is 85, 64, 96, 36, 90 and 110. So, we have estimated the balance factor for 36, it is 0, for 90 it is 0, for 110 it is 0, for 96 it is 1 minus 1 that is 0 for 64 it is 1 minus 0 that becomes 1 and for 85 it becomes 2 minus 2 that is 0 now what I say insert 18 so first it will go to 85 85 says go to my left 64 says go to my left 36 says go to my left so 18 will be inserted somewhere here so the resultant tree will be 85 64, 96, 36, 18, 90 and 110. Okay, now we have inserted 18 here. So, let us check the balance factor. For 18 it is 0, for 36 it is 1, for 64 it is 2 minus 0, 2. For 90 it is again 0, for 110 it is 0, for 96 it is 0 and for 85 it is 3 minus 2, 1. So, there is a 64 is the point where there is a violation. 64 is the point where there is a violation. So, here this 64 become A and 36

become the node B. So, the rotation will happen around what 64. So, what will be the resultant tree. So, B goes up. So, that is 36. 64 become the right child and right child of B become the left child of 64. So, there is no right child. So, nothing will happen and here we will get 18 and other structure of the tree remain intact. So, now if you calculate the balance factor, So, it is 0, 0, 0, 0, 1 minus 1, 0, 1 minus 1, 0. For 85, it is 2 minus 2, 0. Okay. So, this is the resultant tree after performing the LL rotation. Okay. So, let us check out the RR rotation as well. Then I will take your queries. So, now here we have RR rotation. So, if you see here, we are inserting where? right of right sub tree, right of right sub tree. We are doing insertion into the right of right sub tree. So, if you see here the height is h, under b the height is h. So, h minus h is 0. And here the balance factor is h minus h plus 1. So, it becomes minus 1. Now, if we have

inserted something here in the right of right subtree. So here it is inserted. Now the height become h plus 1. For VL the height is h. So of course the h minus h plus 1. So it becomes minus 1. And for here the height was h. So at A what will happen? h minus h plus 2. So it becomes minus 2. So, the now height of A is minus 2. So, now this is you can say reverse of LL rotation. So, what where we have inserted, we have inserted into the right of right sub tree. So, to restore the balance factor, we will do the rotation in the left direction. Same property, again b will come up a will become the left child of b and right left child of b become the right child of a so what is the three case a okay so in rr what we are doing b that is the right child of a So, right child of A becomes parent of A. Second thing is that A becomes left child of B. third situation, the left child of B becomes right child of A. So, these are three steps.

Similarly, in LL, opposite thing was happening. And if you see the example, So, here we have 34, 26, 44, 40, 56. Balance factor is 0, 0. Then balance factor is 1 minus 1, that is 0. For this 26, 0 minus 0, 0. For 34, the balance factor is 1 minus 2, that is minus 1. Now, we have inserted 65. So, 65 first go to 34. 34 say you could go to my right. 44 say you couldn't go to my right. 56 says go to my right. So basically we are inserting into the right of right subtree. So if we have inserted 65, the resultant tree is here. And if you calculate the balance factor, so it is now 65, 0. 56 is 0 minus 1 that is minus 1. 40 it is 0. 44 it is 1 minus 2, that is minus 1. Now, for 26, it is 0. For 34, it is 1 minus 1, 2, 3. So, that is minus 2. Now, the violation of avial tree property happens. Now, since we have inserted into the right of right subtree, so what we will do, we will do the rotation. So, 44 become the parent

of 34, left child of 44 become the right child of 34. So, here if you see 44 is now up, 40 that was the left child of 44, it becomes the right child of 34 and so on. So, now if you calculate the balance factor after this, so there will be a clear thing that the balance factor is either 0, 1 or minus 1. In the same tree, if you wish to insert another node that is 81. So, let us see how it is behaving. Just wait for a while. In the same tree, if I wish to insert 81. So, the tree was 44, 34, 56, 26, 40 and 65. And what I wish, I wish to insert 81. So, if I insert, what will happen? 44, 34, 56, 26, 40, 65 and 81. Now, let us check balance factor is 0. Here it is 0, it is 0, here it is minus 1, here it is minus 2 now. This is the violating point. This is 0 and for 44 it is 1, 2, 1, 2, 3. So 2 minus 3 that is minus 1. So the point of problem is 56 and we have inserted into the right of right subtree. So

rotation will happen along 56. So what will be the resultant tree that is 44, 34, 26, 40 and 65 will of course go up. There is no left child of 65. So, nothing to add. 56 and 81. So, if you see the balance factor is 0, 0 for this 1 minus 1 that is 0 and for this 0, 0, 1 minus 1, 0 and here if you see 1, 2, 1, 2. So, 2 minus 2 that is 0. So, now it is a avianity. One more important thing is that we will always maintain the property of the BST and on the top of it, if we add balance vector, then it becomes the AVL tree. So, all the AVL tree is basically a BST. So, we can say that AVL tree is a subset or a special case of binary search tree. So, we can never say that the property of BST is violating in AVLT, that is not allowed. So, we will see other rotation in the, okay. Let me check if anyone has any query. Okay, I have seen number of hand reach. So, what I will do, I will just stop for 3-4 minute, then come back again and take up your

queries. In the meanwhile, you can insert two things in this tree if it is visible little bit. 26, here it is 40, 56 and 81. So, insert 17 and insert 85. After break, we will resume and I will also try to solve. I am adding one extra slide here. So, to solve this issue, these two probably insert 17 and 85 in the same thing okay so just taking the five minute break and again come back okay uh so i hope some of you has inserted it so let me solve the example then i will take the query okay so now we have to insert two things that is 17 and 85 in the above tree okay so i will show you how the things are inserted and then we will take the query in this tree we have to insert 17 and 85. So let me draw the tree again. It is 44. Here we have 34. Then we have 65, 26 and 40. And here we have 56 and 81. Now we have to insert 17. Insert 17. 17 will primarily go 44 34 26 and here so if i insert 17 so 44 34 65 26 40 56 81 and i have

inserted 17 so let us check the balance factor it is 0 it is 1 and this become 2 okay these are the balance factor because 2, sorry, 2 minus 1 here it is. So, it is also 1. So, this is 1, not 2 minus 1. Now, for 65, it is 0. For 56, it is 0. For 81, it is 0. Now, coming to the 44. So, it is 1, 2, 3 and here 1, 2. So, what is the 3 minus 2? That is 1. So, if you see here, when we insert 17, there is no violation of the AVL tree property. So, we will not perform any rotation. Similarly, if I want to insert 85, so 44, 65, 81 and 85 will be inserted here so the resultant tree is 44 34 65 26 40 56 81 17 and 85 okay now Now so this balance factor is 0, balance factor is 1, here it is 0, for 40 it is 0, for 56 it is 0 because we know that the leaf node will hold the balance factor of 0. So for 34 it is 1, 2 and 1, so it is 1, for 81 it is what, 0 minus 1 that is minus 1, 65 it is 1 minus 2 that is minus 1 for 44 it is 1 2 3 1 2 3 it is 0 so nothing to rotate because

we already had the 3 with balance factor in the range of 0 1 or minus 1 now let us take your take up your queries uh yes parambir so uh please ask your query good morning sir audible sir good yes you are audible can you show the first ll rotation example uh where you had inserted 18. just i'm going to that previous yeah here here yeah yes sir so here you had after inserting the 18 can you explain this rotation uh where the balance has imbalance has happened at 64. so the rotation will around 64. so that is where 64 becomes uh you do a clockwise rotation of single step over there 36 moves up becomes the child and 36 becomes over here that is how this rotation is happening yes yes yes that is that is the same thing yeah okay so rotation will it doesn't have to happen right till the root it has to happen at the nearest node which has got imbalance in case yes yes 64 has got imbalance because the balance factor is two so the rotation will happen here it

will not happen 64 will go to 85 and the whole whole the tree will not rotate no no so we will perform the rotation around the first node from the point of insertion okay it will be localized to this particular sub tree only yes yes yes yes so if the if let's say 64 if in the previous example so we are going to the root because if you see the balance factor of 96 was disturbed so first node that is disturbing so we were moving there if it is happening happening locally then we will perform locally we will not go to root because tree may be very large in this case it is very small we are yes that's all sir this is about my doubt no uh yes ganesh you can ask so i have enabled your mic you can ask i don't know what's the issue uh next one is lalith uh yes lalith you can ask okay good morning good morning so my question is so every time we do an insertion so we have to check for make it balance every day well yeah then then we will insert new mode so this is the important thing

that keep on inserting but before insertion of the new check the balance factor if rotation required rotate it then insert new node so it does not mean that we will insert all the node then we start balancing it every time we insert we have to balance okay in i mean the example which we have seen we have inserted at the either and the correct position we have already already inside at that end right but now if we are suppose inserting in the middle there also we will check the balancing of the whole tree and then so for that ll and rl rotations are there that i will show just after this oh okay thank you sir yes yes yes so just hold uh yes ganesh please yeah yeah sir actually uh in some cases right we are moving the like in a little rotation what we that side of a particular node that that is becoming we are moving it to the right right pair right shape yeah yeah yeah yes sir like uh how to when to do what so that there is

so that is simple because every insertion will be starting from the root and from the root you you will only know that we are now moving toward the right direction in the right subtree we are inserting something so that you have to take care of okay and because we know that in case of BST the information about node also persists but also this information hold key where the highest or lowest nodes are inserted okay so accordingly because it is a BST so we know that the properties are already holding and if we are inserting in the extreme ends okay so we know that is ll or rl so so so while in session you have to determine in which direction you are moving okay and then only we can perform some rotations so the rotation will happen generally when there is a violation so there is a violation if it is not then then no worry okay that can become like whatever you you answer previously that may happen at uh in the locally or that may happen in the

globally we have to first check which is the first node from the point of insertion which is violating the balance factor property if let's say there are 10 20 nodes which are violating the property of avl but we will first pick up the node that is closest to the point of insertion yeah thanks thanks sir thank you very much uh yes anuja you can ask uh good morning sir good one sir thank you so much for the clear explanation just one quick very actually i'm a bit confused in terms of balance factor itself like uh where we started the session prevention just simple thing let's say i want to calculate the balance factor of 85. okay so in both the sides in left and right side identify the identify the path with number of nodes so here if you see how many nodes we are having one two and here in this path maximum node is what one two so that's why two minus two is zero similarly for 85 if you see here so in this left path how many nodes we have one two three and right path maximum nodes are one two

so that that is coming getting so so we should be taking the uh last leaf also as a note This 18 is also a node. 18 is also a node. So the leaf will also become... So just if you pick up any node, check its left side and right side and identify the maximum number of node in any path in left side and any path in right side. So identify maximum number of nodes in any path. on left and right. Just this is the height. Including the leaf. Including the leaf and other aspect. Then just we can subtract it and get the balance factor. Okay. And in which case, sir, we do the clockwise rotation and anticlockwise rotation? So, clockwise rotation, if we have inserted into the left. So, how we can balance? If we rotate towards the right or if we rotate clockwise. so if i inserted here then i will rotate this side if i insert here then i will research this side if i insert here so what is happening so to encounter the left insertion what we can do

right rotate to encounter the right insertion we can do what left rotate okay i mean i'm a bit confused but yeah i will just try to do so no no so just simply see um if if i will insert below 26 so what this will do this will increase the left height so how you can decrease the left height by rotating towards the right so the main agenda is to decrease okay if i insert on the right then how we can balance the tree we can rotate toward the left this simple thing you'll see okay okay so so in a nutshell sir our main agenda will would be like uh have to balance the root uh so our main agenda of avl3 is to balance the high left and right height or to ensure that the left and right height are comparable so if right height increases then we will go towards left if left height increases then we will go toward right this is a rotation thing that we are targeting okay okay okay so just one last thing if it takes any time uh we we we occur that uh this is uh like i'm having like uh n number of

calls which are happening within the tree tree ds2 to us that is uh some somewhere we have kept around the data section of the tree at that moment uh the same way we are just judging here that insertion should be happening in the left node or right node like the you you mentioned that this will be happening within the ram actually so does the ram also possess the similar algorithm which we're just talking right now or it has a different uh architecture altogether okay so um uh in the current scenario uh whether this uh bst or avial tree is happening because now we have shifted toward b3 b plus t and other stuff and only maintaining the indexes into the ram but uh previously this and this these things were um was basically used so this is you can say a background for the next trees and that we will see later on okay okay okay thank you thank you so much Anupam is asking something. Yes, Anupam. Anupam, you are not audible.

Can you hear me now? I will be louder, a little louder. Okay. So, Anupam, we will just check up your problem with this mic and then we will come up. Now, we have also witnessed that while in insertion, it is not every time we are doing the rotation. So in some time we cannot, we will witness that there will be no violation in the balance factor. So at that time nothing to rotate. And now we have witnessed two things. We are inserting on the left of left or we are inserting on the right of right. Now what is the situation when we will insert on the right of left subtree or left of right subtree okay so see the situation and one important thing is that the balancing methodology of lr and rl rotation are similar to nature but a mirror image of each other it as we have seen rr and ll are mirror image here the rotation was this and here the rotation was this and you can say if you put a mirror so we will find similar so

b was going up right child was going up her left child was going up similarly lr and rr lr and rl rotations are similar in nature but they are mirror image okay amongst the rotation ll and RR rotation is called as the single rotation. But LR and RL are called as the double rotation since LR can be accomplished by RR followed by LL. So, this means that if we know how to perform RR and LL, we can simply perform LR by its combination. One time I will perform RR, then I will perform LL. Then it become what? It become LR. So, that is why it is also called as LR and RL are also known as double rotations. But there are direct methods for doing LR. So, if you do not wish to understand or if you do not have let us say the code available for LR, then you can use RR followed by LL to perform LR. Similarly, if you want to perform RL, So, you can use LL plus RR. So, what this means? LR means that we are inserting into the right of

left subtree. So, we can perform what? RR followed by LL. Similarly, if we have RL rotation, then what we can perform? LL followed by RR. LL followed by RR. Then these are the, you can say double method or there are direct method as well for this. Directly we can do. So, that is up to you because you want to wish to perform two rotation or you can perform a single step that we will see in the next slide. So, now here we have a tree. So, this is a little bit complicated. So, here this is a left of B and on the right of B we are having C and it is a subtree that is having the height h minus 1. This is having height h minus 1. Now, and here it is R if you see height is h, here it is h. So, of course, 1 plus node then the balance factor will be 1. For this the balance factor is 0 because this is h and this is h minus 1 plus 1 h. So, 0, ar is having this thing. Now, we have inserted a node tl, c on the left. So, basically what we have inserted? So, we have inserted on

the right of left subtree. So, this subtree is what? Left and we have inserted on the below this node C. So, this is right of left subtree. We have inserted on the right. This is right portion of left subtree. Now, so how we are performing the rotation? So, we have A, B and C. Now, if you see the balance factor is now disturbed and the first node that is disturbing is now A. Now it is A. So, we will identify A, B and C. Why C? C is the point where that is close to the insertion. So, if you see here how what is happening? C goes up, A become the left child of C and now it has two child left and right. Left become the right child of b and right child become the left child of a so what is happening c is going up a is coming down that is done then it has two child so one become the child of b that is left and one become the child of a so this is two-way rotation okay this is two-way rotation or in one sort we are

trying to perform. Okay. Now, one thing. Okay. Now, I had told that LR is nothing but RR plus LL. So, C goes up. That is RR rotation. C's go up. B come down. Okay. That is RR. Then C will replace A and A become the right side. So, that is how we are doing RR plus LL rotation or simply you can perform this rotation one important thing is that because this rotations are little bit tricky and you people are from very diverse background so in the exam i will ask only from ll and rr okay i'm not going to ask where is from lr plus rl though i am teaching because you people should know there is a possibility but this is a little bit tricky okay so So because in RR we know the C come up and in LL we know from C this so this is RR plus LL so it become LR. Now let us take an exam. Here we have 44, 30, 76, 16, 39 and we have inserted this is a root this is left subtree and this 39 is the right portion. So on this here we have inserted 37.

So, if you see the balance factor, so I will show both the way. One, this is using the formulation and other I will show via the RR plus LL part. So, what we have? 37 is having 0, 39 is having plus 1, 16, 0, 30 is 1 minus 2, that is minus 1, 76, 0 for 44, if you see, 1, 2, here it is 1, 2, 3. So, maximum I will take 3 and here it is 1. So 3 minus 1 that is become 2 and we have inserted on the left of right subtree. Sorry, on the right of left subtree. Okay, so this node become C, B, A and what it is basically 39 will go up, 44 will come down, 39 left child become the right child of B and if there is a right child exist for 39, it become the left child of A. but there was no right child so a will as it is and here is the result and if you see it is fine okay now what i will do i will perform rr plus ll to achieve lr okay this is direct lr where c is going up a is coming down a c is left child become the right child of b and c is right child become

the left child of A. Now, let us do the rotation with the help of Rr followed by Lm. So, here we have 44, 30, 76, 16, 39 and 37. So, we already know that the 37 insertion changes the balance factor plus 2 now we will perform first rr rotation so rr will be performed on the first point from the insertion so here we have so insertion is happening on on the 39 okay but one important thing is that there is no violation exists at 30 but we are performing okay so 39 will go up 30 will come down and 37 will be it's what what it be it will be so left child of 39 become the right child of 30 that is the r rotation so basically just consider this portion okay don't check out the entire tree so rri if i perform so 39 will go up 30 will come down left child of 39 become the right child of 30. And then again in draw the entire thing. Now I will perform LL. So LL will be again performed with respect to the 39 44. Two nodes,

this become B and this become A. And a simple, you can say initially it was A and it was B. Now this is b this is a so 39 will go up 44 will come down of course 76 will be as it is and here 30 will 30 will be there and 16 and 37 will be there so we are getting the same result that is in lr via using rr plus ll or we can simply use the direct mechanism okay that is totally up to you Now RL I am not covering, it is just a replicated case of LR. So let us see how we can do the insertion or in an empty list and we try to form a AVLT. So all the rotation will come into one go. Let me insert one new slide, then we will check whether we are correct or not. So, we have to insert 64, 1, 14, 26. So, we have to form an aviary. So, of course, it will be a BST plus balance factor. 13, 110, 98 and 85. Now, let us start the insertion. 64, single node, balance factor 0, no worries. Then I insert 1, 64, 1 will become the left child

because it is smaller, its balance factor is 0 and 64 balance factor will be plus 1. Then 14, here we have inserted 1 then 14 so 14 will go on the 1 and 1 right will become the 14 so its balance factor is 0 okay and this balance factor will be minus 1 and now here if you see left height is 2 right height is 0 so it become 2 so there is a violation and what we have did We have inserted on the right of left subtree, right of left, this was left subtree and we have inserted. So, what we will do? We will perform LR rotation. So, LR means that 14 will go up, 1 will remain at its plates and 64 will be inserted. And since there is no left and right child available for 14, so it will remain blank. Now, we want, we have insert 26 so here we are inserting 26 so 26 14 right but 64 left so 14 1 64 and 14 and 26 will be here so if you see the balance factor is 0 the balance factor is 0 the balance factor is 1

and here if you go so 1 minus 2 now it become minus 1 so there is no violation so we will keep it. Now we will insert 13 here. So 13 will first go to 14, 14 say go to my left, 1 say go to my right. So 14, 1, 64, 26 and 13 will be inserted on the right of 1. So if you see 0 0, minus 1, 0. For 64 it is plus 1 and on the above 14 is 0. So no violation, nothing to worry. 110 we want to insert. So 14, right, 64, right. Now the next node that we want to insert is 98. Okay, 98 we want to insert and if you see here again there is no violation. 0 0 or 13 it is 0 for 1 it is minus 1 so 64 now it becomes 0 and for 14 it is now again 0 so 98 will 14 right 64 right 110 left so what will happen 14 1 64 26 110 and 98 will be inserted on the left. So let us calculate the balance factor. 0 for this one is 1, for this one is 0, for 64 what is the balance factor? 1 minus 2 that becomes minus 1, for 13 it is 0, for 1 it

is minus 1 and for 14 it is 1, 2 and 1, 2, 3. So 2 minus 3 that become minus 1. So still there is no problem when we are inserting 98. So we have just performed one rotation so far. Now we have to insert 85 in this. So I will add one more slide. Now we have to insert 85 in the above tree. So, let me draw the tree again. 14, 1, 13, and we have 64, then we have 26, 110, and 98. Now, we are inserting what? We are inserting 85. So, 85 will of course go to 14, 14 right, 64 right, 110 left, 98 left, that is 85. Now, if you see here, so we have inserted where? In the left of left subtree, if I consider only this subtree, because we know that all the subtree are tree itself. And if you calculate the balance factor, it is now 0, this is 1, this is 2, this is 0, this is minus 1, this is 0, this is 1 and 1, 2, 3. Now 64 balance factor is how much? Minus 2 and 14 balance factor is what? Minus 2. So number of nodes are violating

the balance factor. But first we will consider this one and if you consider this one, so we have inserted on the left of left. So what rotation we will perform? That is LL. So LL says 98 will go up, 85 will come with left child and 110 become the right child. So let us check the resultant tree that is 14, 1, 13, here we have 64, 26, 98, 110 will be placed here and 85 will be placed here. So let us check the balance factor. If it is okay, then we will not do rotation. Otherwise, we will have to do rotation. So, it is 0, 0, now 0, 1 minus 1, 0. This is 0, 64 is 1 minus 2 will be minus 1. Here it is 0, minus 1. And if you see here of 440, 1, 2. And here it is 1, 2, 3. So, 2 minus 3, that is minus 1. So, we need not to perform any other rotation. Now, this tree is balanced. So, this is how we can perform multiple insertion operations as and when required. Now, next thing is the deletion because we know the deletion will also hamper the three properties. So, there are three

primary rotations in the deletion. So, that is R0, R1 and R minus 1. So, we have to identify your node B. If its balance factor is 0, then we will perform R0. If its balance factor is 1, then we will perform r1. And if its balance factor is minus 1, then we will perform r minus 1 rotation. So, this is, these are the three things we could consider while performing the deletion in the ABL tree. So, let us check r0. So, now if we have this example, now we will see this example. okay so we have a b a a r and the height is h here height is also h and we have deleted from deleted node x from right of right subtree okay this was x if you remember this was x and we have deleted the node okay now what will happen the tree balance factor disturbs tree balance factor disturbs okay just just a moment i will uh is the class finished hello

