 Hello everyone. So the final thing we want to talk about with binary search trees is how to delete data out of them. Deletion is a little tricky because we need to preserve the binary search tree property that the left subtree is less than the current node and the right subtree is all greater than the current node. But we need to get stuff out of the tree. So we have to disturb this tree. What happens if we want to delete this node or the value of this node? What's going to happen? What's the procedure for doing that? So of course it's a little tricky but we have algorithms for that. Essentially what we will need to do is we're going to consider three different scenarios and deal with each one of them a little bit differently. So our delete operation is going to be fairly complex but once we break it down hopefully it won't be too bad but let's make sure we have a really good understanding of what we're doing to the tree. So we want to remove a node containing a target. So let me put a target in here somewhere. Simple. And we'll say that actually you know what, oh my goodness gracious. It doesn't like that at all. Let's try this a different way. Insert this. There we go. All right. Let's say our target is the value 4 and let me pretty it up a little. Only a little. Okay. All right. So here's my target. It's number 4. Okay. And I'll make him, let's just make him dark red so we can pull him out. All right. So we need to find first. Okay. We want to delete. We want to delete the node that contains the key for the item 4. Okay. So how we go about this process is going to depend on where in the tree that 4 is. Okay. So in any case what we will need to do is we will need to find the target node where it is and that targets parent. Okay. So let me just make some markers for the target, target node and the parent node. Okay. So all right. So let's say let's look at one scenario. So the first scenario is when the target has no children. In other words it is a leaf node. Okay. So like this guy down here. Okay. This is scenario one. The target is down there at the bottom. Let me just try and highlight these a little bit. Okay. So scenario one. All right. Now in this scenario the target is this node here. And the parent is this node here. Okay. So what we will have to do is we need an algorithm that finds both the target node and the parent node and gets a reference to them. Now fortunately for you I'm going to give you that code because it's a little tricky. It's basically though a variation of the search algorithm. But I'm going to use the iterative version of the search algorithm this guy and tweak it a little bit so that it keeps track of and returns both the target and the parent as well as whether or not it even found the target. Because it could be the case where you try and delete something that's not in the tree. Right. You're going to have to handle that differently in your code according to the specs. But anyway let's say for lives here and we want to delete it. Okay. Well just looking at this tree. How do you delete this guy. Well it's pretty easy. You just need to tell the parent. Hey forget about this child. Right. So just get rid of this link kind of like deleting something in the linked list. Right. You just forget about the link. And that's it. Right. Not too bad there. All right. So that's how you would get rid of in scenario one. The parent just deletes the child. They forget about it. Right. So obviously you have to know whether it's the left child or the right child. But that's a simple if statement that you can do on the parent. Okay. So that's how you chop out the case where the target is there. All right. Let's grab this and let's take number four and let's put them in scenario two. Scenario number two is where the target node has one child. Okay. So let's say that instead what we were deleting is this guy right here. Number four. Okay. So number four up now in this case. So number four here. We want to delete this node. This node has one child. It's got two descendants but only one child the right child. So this is our target. Now who's the parent of this node. The parent of this node is going to be this node above it. So just look at the scenario. If I need to delete this. What should I do? What do you think I should do? I have to maintain that binary search tree property. Well everybody to the left of this guy. So everyone in this subtree is less than whatever value is up here. So an easy thing to do here is going to be let's just take this guy here. Let's find the one child. This is the case where the target node has one child and what we want to do is basically adopt him. Oh Lord where to go. I made it very unhappy. There we go. We want to adopt him and forget about this link here. Okay. So the parents whoever the target is if it's the left child we say oh forget about your old left child adopt your grandchild. This is the case where this node only has one child. It's called adoption. Okay. So we need to know both the target and the parent for that and we do know this locate no methods going to give that for us. Okay. So that's the second scenario. The final scenario is when the target has two children and this is a little more complex. So let's kind of go to the most complex case here. Let's go to the case where we the target is in this node here. You know word wasn't really meant for this sort of thing. Then again we weren't meant to be learning online either. We were supposed to be in classes rooms going over this with whiteboards and handouts and be all sorts of fun. All right. So now scenario three. We're in the scenario where we want to delete a target that has two children. Okay. That's a little dark. So what are we going to do. We need to replace the data in this node with his right left most right descendant. In other words his in order successor. All right. Let's get a better example actually. We don't need to worry about let's say we're deleting the root here. We're actually deleting the root. So the parent in this case is going to be none. We want to replace the data. We're not going to actually delete a node in this case. We're going to replace his data with the from the in order successor which is the left most right descendant and then delete that successor. Okay. So who is the right the left most right descendant. So we will go right and then go as far left as we can. Okay. So in that case the left most right descendant would be this node here. Now what you will know what you can be guaranteed about this node here is that it has the next highest value in the tree. Okay. If you have any doubt of that let's kind of go back. Let's go back to this one. Say we were deleting this root. Okay. It says find the right the left most right descendant. Okay. If we go right and then find the left most person. It's Louisiana here. If you just glance at this tree you'll see that Louisiana is actually the next value in order after Kansas. Okay. Same thing over here. Eight find for deleting eight find in eight has two children find the left most right descendant. Well we go right. Well there's no more left here. Right. So we're going to replace eight with eleven and in fact eleven is the next highest value in this tree. So this replacing of the deleted value with the left most right descendant effectively puts the next highest value in that place where you want to delete. Okay. So this is the successor node right here. This guy is the successor. And what we're saying is replace this value. Get rid of it. Okay. Get rid of this value. Delete the four and replace it with the five. Then then delete the in order successor. So now delete this guy. Okay. Well how do you delete that guy. Okay. Well you will apply the algorithm for deleting either the target with one child or a target with no children. Okay. The left most right six left most right descendant is not going to have two children by definition. It's either going to have one child or two children. So then you say all right replace the value then delete this guy following either scenario one or scenario two. Okay. It's a lot. Okay. This code here which I give to you will find the in order successor of any given node. Okay. It's a lot to process but all of this is done with the intention of keeping that binary search tree property of making sure that left sub trees are less than the current node and right sub trees are greater than the current node. Okay. So let's go down here and let's just tap in this algorithm. Now I am not going to give you the code for it. Your job. I give you this code here in your tree map. You have to implement the delete function. Okay. So it's going to be your job to translate this concepts into actual code. All right. So if the target has two children you are going to find the nodes in order successor which is going to be the left most right descendant. And I give you the code for that and that successors parent. You need both the successor and its parent because you're going to delete this person. Okay. But I give you that code right here. Once you have found that person you're going to replace the value in the node to delete with the successors value. And then finally delete sessor node using the strategy below because successor node has at most one child. Okay. So what's the strategy for deleting when you've got no or just one child. Well you need to select the target nodes non empty child. Okay. If one exists then if the target is the root node we need to do something special then the root gets the non empty child. Else kind of everything up here on one page else replace the parents reference to target node with the child. This is adoption. Okay. All right. So again having this really strong visual is going to serve you well here. Okay. I'm providing you with these helper methods but it is going to be your job to fill in the delete item method. And it's not going to be a short method is probably going to be the most complicated method you've written but now you have the pseudo code for it you have the algorithm for it. If I can get that algorithm to show back up right. All right. So this is the idea of deletion talked a lot about binary search trees. I want you to be really comfortable with binary search tree operations. You're going to get practice implementing them in your tree map but more importantly you you very much need to be very comfortable with how deletion works how search works how traversals work. And how insertion works in a binary search tree along with all of the tree lingo because it's going to factor very heavily into your final exam especially at the conceptual level. All right. So good luck with everything. Please let me know if you have any questions and best of luck with the rest of the semester. Talk to you soon.