Insertion into AVL Trees

SaifAli Kheraj
A free video tutorial from SaifAli Kheraj
Instructor
4.1 instructor rating • 2 courses • 841 students

Learn more from the full course

Data Structures From Scratch

This course is designed to help you understand concepts of data structures in detail with applications

08:30:02 of on-demand video • Updated August 2016

  • apply data structure concepts in different applications of computer science
  • compare and contrast different data structure based on application
  • write their own algorithms and can analyse between different algorithm
  • implement various algorithms in programming language
  • develop planning skills required for datastructure
English [Auto] Move to the next structure and seeing the example of how to insert into the increase. So we have the example so I just encourage you to first try this example on your own and then see this. So and you have all four cases. OK so you can use that to solve this. So yes we do get into these arguments. So eat 1 4 5 6 answer. And this will be the simple cases. OK. So you're dealing the first two cases now and then one. So first we'll insert three. So initially we have we don't have any kind of on or so we insert three into the more and this is my dream. Now then insert. So as in the case of my research team what we do is we compare three cases compared to three to do is less than three so it will go with the left side of three who will read or hear then we have one right here. So up in knots balanced you can get it using the balancing factor. So we are done with this and then we insert once or one ballades with the left of this so when this lesson entry learned this lesson Dume it will go on the left side of this or one goes right here. So now you check the balance less. So it looks unbalanced and this looks something like the Lefler is. You can also check the balance effect so your my all the height of love suppresion the rights of others. I love subtrees one the height of recipe's your arm and enormous balancing factor is you. You're my balancing factor is one subtil here. It's fine to really fight because it could be at most one. And here if you see that the kind of left subtrees doing so. Was right. We're hearing is this queasy unbalances balancing factor is beating was due to my knees you know it was. So this is my last piece as I explain you right here is this. So this is my Lefler case and biphasic was so this is my left leg. So I have to take this part. So I'm using the tools you have to study so you can beat this. And this is so right on here that what you know you're on. But one right here. So I will essentially to take it easy. Lefler piece. Then the next thing I just insert the middle here and just you can also check the balancing factor. It's right here. The balancing factor is all this is right here on the balancing factor is against your right. So this is my balance sheet. The next thing you insert is four. So I just create this to you again. So my second step father is I'm right here. So this is my second step soon to right here that I have one right here. Right. OK. And I needn't serve four. So four is greater than two suited for this spot. Four is good in three suit with one of the spots for you right here. OK. So does it even balance less like a stack. So this is a balancing factor. You don't you got the balancing factor of all because of one. Sorry. So you're zero once as you remind us what is this one. And here I thought the height of the previous one. All right here's one minus two is minus one and again my balancing factor is zero. So you see right here that this does not one in the condition because it should be at most fun and we are having difference of one right here. So there's not even precondition. So this is the balance. This is the only balance you can see that this is balance with this balanced game. So the next thing we need to answer is so we are in so it four and the next thing we need to insert is 5. So I'm just laying right here. My name here. So thing that we do doing sort of quite right where you're just on the vicodin side. All right. Will you grab one right here for you right here. I have already told here and I just need to insert five. So this is sort of five right here to the binary search tree property because you can do this. Five years for. So I'll just insert it right. So let's check the and see. So you're at the balancing factor of zero because of left and right. There's one balancing factor right here as of this one because I of less of these you don't as you don't mind this one is minus one and you're at the height of lapse of your own and rights of jihadis do. So you're balancing factor is your minus two is minus 2. This is the one which was easy and balanced. So to remove the unbalanced last what should I do. If you look at this particular part right here this in the pot right here. So what what what is the key is which case is this. This is the. Right right. Yes. OK. This is my right. It is. It's a very simple case. So I'll perform again. Right. Great piece. And what really explained to you what I do in the right radius right here on the right track key is all my B-F balancing factor is equal so minus. And then perform the next station. So if you're performing activation to make it balance or you can also have been Dushan of this why I need to perform deprevation on this to make this balance. So it's self-explanatory. So now I'll balance this stuff so I get right with your game to remains as it is on day when you hear it was balance the situation unfold the left occasion. So this is my I should write this. This is like right. Right. Yes. This was my right. And so of all right well here. So what I'm just doing is just to make it balanced. What I'm doing is I am all moving down and forward. OK. So you can think in this and make it balanced. King should do that for them right here. And then three goes down. And then the five or seven you can check the balancing factor right here. So my balancing factor is you run and you're also balancing factor is your own your odds on the balance and because you don't because 1 minus one is equal to zero. And so my balancing factor is zero. And here also my balancing factor is one my is too high. That's a piece one the height of rights of free is two to one minus is equal to minus 1 and differing by minus 1. And this is one the property of even a queen. So this is my neatly balanced binary search string. The next thing I just want to do is to insert next element which is 6 0. I just answered 6 and this 3. So let me go ahead line here and this line right here. So I just call this again to one and you can do while I'm doing this I can really why the next thing I just want to answer the six against six people like you again soon. You can just check and see just didn't do it again for six years and finally gets it right. Right. OK. And one more thing to notice is that in the you sort of you will notice that the greatest element you will find in the binary search she is at the extreme right. So at the extreme right you. So the greenest government here is OK. So. So I've inserted six right here. OK so you can check the balance in fact right here some of it is infector is run by balancing In fact here is my this one. And you can work it out on your own learn balancing factor here is minus one by balancing factor. Here is the height of lapse of PS1 So one mile is the height of right used one minus the easy ones to minus two. And so this creates the unbalanced us again. So let us see how we can move the unbalances OK. So what is this. Which case is this. So you can see that most recently in so more lies along the street line on the right side. So and the extreme right. So you can record this as the right Brachis because you can sort it out and you do insertion of this. We created the unbends that's just the right brickies because you can that the extreme right. This is just really balance. So this is my right Brachis. OK so what we will do in this case swing right player is what we do is we perform the single lactation. So we perform the levitation game. So it's. So I'll just give you two minutes to perform condition in this case and you can also refer to the previous stuff. I explain you see to perform the left arrogation what it does do is say I should change the color so you can balance this. So I should follow the metrication right here. So folks that come right here and Pudong and so on. I have. Right here and two will go down so I'll just connect or two one and two is connected one with another and for those are connected to three Sulayman evil I can sell them. Let me explain this to you. So five and six right here. It's available to you. All right. OK. So you can check the diagram to explain to you this get them the right Perkis they explain to you where is the right way to do this. This is the case again. So if you if you see this particular stuff right here we're a different interpretation if you have this stuff remaining you just that right here. OK. So it was apt to be softer this dated. This gets connected. You can see this as it is unraveling. So this is my all right great case. So I hope this gets to your next thing. I just wanted to insert something again. So I'm just inserting 7 game so I can easily insert 7 in this case. So it's like this again. Sife All right here. I have to read with here. I have one right here at the United Way here. Game five right here. Six regular survival with the seven. So it's six and so seven been sorted. All right. 7 so that what we hear so you can see that let me check the balance in fact there is right. Well is the balancing factor is actually in your mind this one. So it's up to loads of Kim to the balancing factor here is your land is doing so you can see that this is the right is article on the left a patient on it. So I'll just for fun. And so my evil looks something like this. These were my simple cases I was dealing with six was right here and fight goes right here. And so is right here. So this is my balance. So let me be the more difficult cases the tort reform case is we should deal with. So that is the way that soldiers who work with the new page right here that's all the same stuff. This is so nice. All right we're here. All right. We're here. I've got one right here. But we're here. I'm just trying to see him get going. I've got six right here and Buddy getting five right here seven again. Now I'll ask you to answer these elements into the mind research tree. So you have to insert 16. Then you can serve 15 than 14 and 13 and fell in love and then then eight. So you have to insert these elements into the binary search now. So I just give you a few minutes to think on this and how you could do on your own before moving on to the next. So that they see how concerned you have to perform the foundation. So we're at four so that isn't so. So the first thing we need to answer is 16. So where will the 16 be. So 16 is greater than four. It sounds great and since you've been following this but 16 is getting 78 to of this whatso 16 balls right where you are. And then on for days so if you check the balance infector right here and it's still balanced according to the. So it's still balance. You can check that and then we have to insert 15. So very interesting. So 15 is in for it's going thinks it's greater than seven. It's not really then 16 so it's less than 16 so it will go right here. So we'll go right here. We'll be right here. And if you change the balancing factor in this situation so on balance in fact right here my balancing factor and here is Iran because right of left and right is wrong. So the balancing effect around here is one because we had the plus a PS1. So one might as you raise one and here my balancing factor will be. So here my height of love supreme zero and zero minus do so because the height of Redzepi stews you know manage to is minus like balancing factor here is my this do. So ingredient that ensnares the situation in this case. So I'll just show Marvis this stuff so I can market this stuff. So this part created the unbends that's OK. So this looks lovely. So I can just mark this part pre-TV and balance this game. So how to remove this and balance this one case should we apply to remove this imbalance. This is essentially my right left case it gets up to perform the right leg. So I just right over here the right that is the case or this is my right leg case. So I have to perform the this and the right leg is what I just do is again I just perform for the right patient. So I'm forced for example and destroy this again right here. So that humidistat you're in 716 of 15 right here. So this is my right leg. So trying to do it right. Dr. redating to the right. I have something like this. Seven fifteen because I'm doing the single right rotation and moving 15 right here and moving 16 right here. So 15 goes right we're here in 16 below that. So this is my single right tradition game that I have to perform. So this is my right brain. So this is very easy. So I'm 15 right over here. Then I have seven and then I have 16 right here again. And if he wanted to go directly to this or other people to do this is that you can just directly put 15 right here so if seen that up again and you move seven down and 16 is already here. So this is the great ok to move right into this game. So what do you do right here.