caption

2 3 Trees Deletion

what's good everybody its Asia here again and in this video I want to a cut I want to go over to three trees and deletion which I consider a pretty complicated task and in this tutorial I'm going to show you videos which cover you know the practices and visualizations of what to do and then at the end I'm going to go through hard core examples so as you see in this two three tree example I have up here I have not I have letters and I'll explain what the letters mean into individually in a second but first if you do not understand to three trees I encourage you to watch my to three trees insertion video or my two three three four trees video which I also have in the data structures playlist so as you can see I have this two three tree here and I have letters P stands for parent s stands for smaller and L stands for larger now of course that s s is larger than L and that's all we care about but this whole node is entirely less than P and D is the node we are going to delete here so what we do in deletion really is you want to try you're going to switch the parent node if there is one if the node you're deleting has a parent you're going to switch it with the parent and then simply delete it and what comes of that is kind of is the way you redistribute so what I'm going to do here is I'm going to switch the parent and the deletion node because what I want to do is even though this seems like a really easy tool eat where I can just delete this node hey why don't want to delete this node it's the tree has a can be unbalanced and what I want to do is I want to rebalance the tree here to make sure it's always balanced and what I want to do is kind of make it like this at the end so you know it has it still has both leaves the parent still has both leaves off it or spoke children so what I want to do here is now I'm going to switch P with D right here and now what I'm going to do is I'm going to delete D or can delete i'm going to delete the value in d and then i'm going to bring up this l right here so what by doing that what I'm going to do is I am swapping this element the node that I'm on that I might be to delete with the in order successor and if you don't remember what in order in order is you can look at my tutorial in our in order traversal but it's essentially the going left and then the item or the element and then trying to go right from the parent so if I went left if I want left here I couldn't go left anymore so I hit this L and that would be my next in order accessor so what I can do here is I can bring up this L and as you can see here then I have that little image I showed you before well I've got the I've kind of got a more perfect tree where I've got L is now my new parent asses to the left and P is to the right which is great because I did know and then this does make sense because remember how I had SNL here in the beginning and since it was to the right of P I know that P is bigger than L so it is satisfactory by the rules that P can still be to the right of L so that is a simple deletion that is known as the remove redistribute deletion and the next deletion we're going to be working on is the remove redistribute solution actually this is another example of the remove redistribute example that I showed you guys up here it's just a little bit more complicated so d again is the node we want to delete and then I've got parent smaller and parent larger so again what I'm going to do is I'm going to delete is I'm going to swap the nodes with I'm going to swap D with its parent with the closest parent which is PS so I'm going to have PS and D and so then I'm going to do the inorder successor so it's it's on left element right where I can't go left because that's where I'm good that swear the note I'm deleting from so that's why I'm switching from so instead I'm going to go right and the next element in succession is SL so what I'm going to do here is I'm going to move our deletion and then I'm going to delete SL I'm going to sorry not SL just the s I'm going to bring up the s here and then when I bring up s here that's going to make everything nice and cozy so then I just have one note here of an L and now the tree is perfect again I didn't delete any child's or nodes or parents anything and I successfully deleted that node D now on to the next things okay here's another example a tad bit more complicated and one more step involved so again I want to delete the node D so what I'm going to do is I'm simply going to swap D with its closest payment which is the parent of these smaller PS s we like to go on PS wood D and then what I'm going to do is I'm going to delete D and bring up its inorder successor ex and I'm going to put it right where the D was so I'm going to swap those out but now as you can see there's nothing there and I don't to leave the ex there because there's a duplicate node so what I'm going to do here this is the extra step that I'm talking about or the more straightforward example is I've got to bring down this PL to make sure that there's still that I don't remove this child node and that it still satisfies the acquirement so I'm going to get I'm going to put the pl in this node and now I need I first three children I need two elements to satis to be able to have different sets and different paths to go so what I need to do is I need to bring up this s or the next in order element so I'm bringing up that s because I still need an element to fit all the requirements and I'm bringing up that s and then this was that L that was bigger than that s and as you can see this still fits the requirements because in the beginning the X was between was was less than PL and greater than PS which it still is and then in the beginning s was greater than PL but less than larger which still fits the requirements so that's a way to properly we'll move another out kind of again a more complicated example okay guys here's another exam now this look may look very similar to the other ones but let me trust you you actually do a different oil move this is known as the remove and merge method so again I'm going to delete D I'm going to swap it with its closest parent which is s and so I'm going to have s here and I'm going to have D here once I've swapped them I can finally remove it so once I do that I have successfully removed D but now what I do but now what I've got here is I've got an L and a and a B and I can't like if I moved all these over so if I move the a over here but the L down and bought the B up I would I would have an empty node here which is not what I want so that's not what I want to do what I actually want to do is let me put this all back when I've got the B and I've got the L here all I want to do is I want to simply merge these two nodes together I want to merge s and a together so that everything is satisfactory yet again so that's why this is called the remove and merge method so then I've got s and a and L only has two children would still satisfies the rules so that is the remove and merge that is one at one remove and merge merge example okay everybody now we're going to do another example in this example I've got again my basic parent nodes but essentially the s and L and D this time are not Leafs just imagine they have other trees they have other elements below them and that you have to consider reorganizing those paths or those branches when you delete a node so again I'm going to delete D and what I want to do again is switch P with D again I'm going to do P I'm going to leet P now I'm going to switch them over now and then I'm going to delete D and I'm going to bring up L now as you see here what's going to happen is I'm going to bring up L here and since I brought up L now this becomes s becomes a one node or has one element in so it doesn't have support it cannot support this branch or um this this branch of elements the T 3 branch because or every element you can for every element you have in each node you can have n for every n elements you have an eternal you can have n plus 1 branches so what I need to do is I need to take this T 3 over here and attach it to its sibling and make an eye sense and make tea for right here and put it over here with T 3 and that way it still fits the requirements and that everything is solidified because T 3 was the biggest tree here and we know everything in T 3 is going to be bigger than s but less than P so that's why it's still ok that it goes there and we know and it's okay that D extends to T 4 because beforehand it didn't it beforehand it was the D it was bigger but since we switched with the parent we still know it's greater than we still know it's greater than the parent because the parent used to be a P remember and we got rid of that deleted note another simple case is let's say for instance this is the root node in my other examples the this is this is my top node was not the woot node it was a case that it was a child of something but if you have a woot node and that's all you want to delete and let's say I've got trees or I've got roots below of a B and C to delete the root node is actually really simple all you do is you delete the road the root node you don't need to refactor anything or do anything else and then this is the new root is this SP right here so that's really cool and probably the easiest one to remember okay guys now time for an actual example in this example you're going to be deleting 65 the node I'm we drawing down there and for 65 what we're going to do here is we're going to first we gotta find 65 so we're going to find 65 and then we are going to remove its closest parent once in this case could be 70 or 90 I'm going to choose 90 it doesn't matter which one you will move so what I'm going to do is now I'm going to swap them so I'm going to do I'm going to now make 65 into this D node now I'm going to do it with 70 and what I do what I'm going to do here is I'm going to actually delete is I'm going to actually delete 70 I would delete the Dino Dileep the 65 d I just made and what I'm going to do here is now since there was a void here I'm going to have to move up the 70 node because I need to fill the void so then I move up to 70 node and this just becomes a 1 node of 80 so pretty easy example the next thing I'm going to do is I'm going to delete the 70 as you see so when I now I'm going to delete 70 and in 70 what I want to do is I want to find 70 again this right here and what I want to do is I want to find the next inorder successor which is going to be 80 and then with 80 what I want to do is I want to swap them so ice I want to swap 70 and 80 80 and 70 and since I've done that I can now safely delete 70 but it does that does something that I do not want and what that does is it leaves a blank element in our tree and we don't want that so what I want to do here is since I have this blank 70 is I want to there's two things on I can either do this about two ways I have to bring down an element of 80 or 90 and then merge those two nodes with the with the next one in sequence to it so it makes sense so for instance I could move get pull down 80 and then make this node into a 80 60 node make this into two elements oops make this into a 60 80 node and attach it or I could bring down 90 and make this into a 90 100 node and then it and then done put it on the right side and for my sequence for my niceness I will do a 80 60 node and that allows me to delete this 80 and make the tree format it again as you can see let me draw a nice and bubble because I already know I'm pretty bad at doing this and I need one line here going oh man so there you go then I just have my 90 and there you go boom and we've successfully deleted 70 next up I'm going to delete 100 and simply all I do is I find 100 and then I know that's going to be my new deletion node so I like to put it look nice and D around it and I'm going to swap it with a parent so I'm going to swap 90 and D because that is almost what I always need to do I'm going to swap 9 D and D and I now I can't just delete this node obviously because I can't just delete this node so what I'm going to do is I'm going to bring up the next in order successor with H with on multiple elements which is going to be 80 and so I'm going to bring up 80 here and I'm going to put 80 right down here and that's how I'm going to delete 100 to satisfy all the requirements and 60 is going to go back this node is going to go back to having one element in it okay so now I'm going to delete 80 which is actually going to get pretty hairy so again I'm going to find 80 I just write it here things get pretty here when you decide to relate a parent so I'm going to delete 80 and I'm going to put a D there and then I'm going to swap it with the inorder successor which will be 90 or it could be 60 but that's okay we will do I need to stay so I'm going to put 80 right I'm going to put 80 right here and basically what I'm going to do here now is sorry I'm going to put in d node here and I'm going to swap it with 90 so then I could successfully delete the D node or 80 but I can't redistribute that so I need to merge the nodes so simply what I do here is I put the elements together and what we get here is kind of a weird example and probably the hardest thing to do in the tree so I'm going to have to I'm going to move that denote up and I'm going to put down 60 and 90 because I couldn't redistribute the note I can't redistribute them so I merged the 60 and 90 but I can't do anything else and this is an invalid tree so I have to recursively go up the tree here and basically what I do is I start looking upwards I start looking at this 50 here and then I see would like do to redistribute or merge the notes here so I see this 50 and I say hmm what can I do with this 52 possibly um get rid of this denote and make actually make a tree structure so 60 and 90 could live together because as far as I know 60 was 80 so obviously 60 and 90 need to be on a different par so what I can do here is I see that I can put I could merge 30 and 50 and make and make a 3 branch node that simply brings them all together so was that 40 yes it was 40 and now as you can see here I have reclassified this tree here with this node and now I can successfully get rid of the D here and just draw one line from the 60 or 93 and as you can see that actually works out because 60 90 is greater than 50 40 s between 30 and 50 and 10 and 12 is less than those two so as you can see the leading and E is pretty different and now we have a new root of 30 and 50 instead of just the 50 node that is something to remember so getting 50 50 deleting things like that can be very complicated because you may need to redistribute everything and you may get an invalid tree at one point so there you go those are the basic examples and pretty complicated steps of removing in a tree if you have any more questions you can feel free to comment and ask me but also I'm going to include a link in the description below if you want to look at anything and a really great PowerPoint that explains how to do any trees work it's something I went to study up on this tutorial but I hope you guys have a great day um visit my Facebook and Twitter I hope to tweet more and give me any suggestions if you like if you don't like my handwriting will you own any extra software I could use or just anything the great thing about the Internet is interaction I hope you guys have a goochy goochy day

As found on YouTube

Add a Comment

Your email address will not be published. Required fields are marked *