 Okay, guys. This is a tutorial of binary search trees. Okay, binary search trees. I'm going to go in depth on binary search trees, and I hope you guys will follow along and understand what I'm talking about. Okay. So what is a binary search tree? I'm going to go slow. Okay. A binary search tree is basically trees like this where we have a left child and a right child for every node. Okay. Now you could have empty nodes, but that doesn't matter. But for now, let's say that we have a left child and a right child. We have left child, right child pointers. Okay. Anything on the left side is going to be less than the current node. Anything on the right side is going to be greater than. Okay. So on the left side, it's going to be less than everything on the right side is going to be greater than. Okay. That's pretty much what binary search trees are. All right. It's a tree structure. Anything on the left side is less than the node current node. Anything greater than is going to be on the right side of the node. Okay. Let's look at this tree. This is also a binary search tree. Why is that? Everything on the left side, even though it's empty, is less than the current node. Anything on the right side is greater than the current node. We have this, if you go through everything, any node in this tree, the same property holds here. Everything on the left side is less than five. Anything on the right side, seven is greater than five. Okay. That's what binary search tree is. All right. So what is a good property of binary search trees? Well, one of the good properties is that you could traverse through trees, these trees with in order. Wait, let's see. Yeah. Yeah. So you could traverse through these trees through in order, pre-order or post-order. Okay. So these are tree traversals. All right. In order lets you pretty much print out the tree in order. Pre-order prints out the tree, prints out the nodes, the children before it, and then post-order prints out the children's afterward. Okay. So here we're going to see that. So yeah. Okay. So I forgot to go over children. Okay. So here's a current node, right? The children are the left and right nodes below it. All right. Anything below it, that's a child. Okay. So five and seven are children of this value six. Okay. We normally call the top part the root. This is called the root because it's like the first node on the top. And we call that the root. Anything else below it are like it's children. So this is a child and that's a child. Okay. So yeah, that's pretty much it. So how do you do in order traversal? This is called in order traversal. Let's go over it. Okay. To go in order traversal, you normally just go to the left and then the node and then the right. Okay. You go to the left subtree, then you go to, then you print out your current node and you go to the right. And why is that? Well, when you ever you, if you look at this tree in order to print it in order, you have to go through the, you have to print out the left side first because it's less than then you print out your current, then you print out your right because the right side is greater than. Okay. So in this case, our inner in order traversal, which let me actually, I'm going to erase this. So I'm going to call this in order. Okay. What is it going to do? So first I'm at six. I'm going to go to my left subtree. Right. Which is this is my left subtree five right now. All right. Now I'm at my left subtree. I'm going to go to its left also. Okay. Now I'm at two. I'm going to go to its left. Well, there's nothing there. All right. So now I'm, I went all the way down to my left. Now I'm going to print my current node two. All right. And then I'm going to print my right node. Well, there's nothing there. So I'm done with that. I'm going to come back up here. Now I'm at up here. I'm going to print out my current node five. Then after that, I'm going to print out my right subtree. What's my right subtree five. So I'm going to print that out also. So this is what it's printing out. Okay. Now I'm done with my left subtree here. Now I have to print out my current node. The current node is six now because after my I'm printing out my left subtree. I'm going to be back up here. What's my current node six. I'm going to print that now at six this six I'm done. I'm going to print out my right subtree. What is my right subtree this side. So it's seven. Right. I'm going to go to my right subtree. All right. Now I'm at my right subtree. I'm going to go to the left again. Right. Left. Remember it's left then current node then right. So I'm going to go to the left. There's nothing there. All right. I'm going to print out my current node seven. Okay. I'm going to print out seven. Then I'm going to go to the right eight. Okay. At eight. What am I going to print out. I'm going to go to the it's left. Nothing. All right. I'm going to print out the current node eight. What am I going to write go to the right. Nothing. So then I'm done. So that's in order traversal. All right. This is the in order traversal. These are the nodes after the in order traversal. I'll go over it again. So at the beginning go to the left. Okay. Then after this I have to go to left again. Right. I got to go to left left left okay. Now I'm at that I'm done at the left. I'm going to go print out go to that's left. There's nothing print out the current node to write nothing. Okay. Just print out to all right. Now I'm done with the left subtree. Now I got to print out the current node five. I printed out that out that five. I'm going to print out the right side. Okay. Five. Print that out. And come back up here. Once I'm done with this got to print out the current node six. Then I got to go to its right. All right. So I'm at seven now got print out the left nothing print out the current node seven eight. Then after that go to the right eight nothing left nothing print out eight right nothing. Okay. And I'm done. Okay. So that's in order traversal. Let's go over preorder. Should you preorder traversal or to do preorder traversal you print the route before the values in either subtree. So it's going to be node left right. Okay. So what is that. So I'm going to print out my current node six. I'm going to print out left subtree then right subtree. Well okay. Now I'm at my left subtree print out the current node left five. Now I have to go to my left subtree then right subtree. Okay. Current print out the current node to print out its left nothing right nothing. Okay. Come back up here. So I just finished printing my current node right into my left. I have to go to the right. Okay. I'm at my right. I'm at the current node five and print out my left subtree nothing right nothing. Okay. Now I'm done with that. I'm done with left. Now because my preorder remember it's current node and then left right is the current node then left and right. Okay. So once I'm done with my current node I got to do so I did my left node already and then I have to do my right side. What's my right side. Okay. At seven do your print out your current node seven. What's my left nothing right right eight. So I go to my eight. That's my print out the current node eight left nothing right nothing. Okay. So that's preorder. All right. Post order prints the roots after the values in the subtree post order. So what does that mean I'm going to print out left subtree right subtree then my current node. All right. Left subtree right subtree. They print out your current node. All right. So what's my left subtree five. Okay. Now I got to go to my left subject again five. Okay. You're then left subject nothing red subtree nothing print out your current node two. So now I'm done with my left subtree here now I got to go to print out my right subtree. All right. Left right nothing so I'm going to print out five because I print out my current node. Now I'm done with my left right here. Now I just have to print out my current node five and then yeah I'm done with that. I'm done with my left subtree now I got to go to my right subtree. This is my right subtree. Now I got to print out my go to the left subtree of this seven. What's my left nothing. What's my right go eight. Okay. I'm at eight. What's my left nothing right nothing. I'm going to print eight. All right. Now I'm going to come back here. Now I have to print out the current node which is seven and then after seven I finished printing my left and right. I finished my left and right subtree. Now I have to print out the current node which is six. All right. And that's how you do post order. So I did in order pre order and post order. Those are the traversals. Right. I'm going to explain it again. In order you go left right left node right. Left node. Right. I'm going to print my left side subtree. Then my node to my right pre order pre order prints route before the other values in either subtree. So pre order I'm going to print the node then my left and right post order. I'm going to print left right and then my node left right and then note. Okay. That's what these traversals are. These are tree traversals. Okay. Hope you guys understand. Okay. So how do you code this. Well this is I'm going to show you guys the pseudocode for in order traversal from CLRS in order traversal. We call it they'd call it tree walk. So they do it like this. If it's not null. If my current node is not null. We're going to go in order. We call it in order tree walk again on the left side x dot left. I print out my current node. Then I do in order tree walk x dot right. Okay. So that's that's the pseudocode how you would do it. Go left print the node in order tree walk. Right. Okay. So recursively call these. All right. You guys could probably figure out how to do pre order and post order. It's not that much different. The pseudocode of it. Okay. Now here's the theorem. This is just a theorem from the book. It's called 12.1 but it really doesn't matter the name. If axis is the root of the end node subtree in order traversal takes O of n time x is root of n node subtree in order walk takes theta of n time. So it would take one loop in order to do it one theta of n one for loop to do it. All right. We're going to prove this now. Okay. So guys we're going to prove this theorem that it takes theta of n time. Okay. Let's do this. So we're going to prove this. This is a proof. Let's write this mathematical function that tells us the time. We're going to call these T of n which is the time it takes for in order. Okay. When it is called on the route. Okay. This in order visits all the nodes of the subtree. So since if this node visits all the order of the subtree the I think omega it's going to have T of n omega n. So T of n would equal to omega n because of visits all routes of subtree all nodes of subtree. So now we just have to show that T of n is equal to big O of n. So you have to show T of n is equal to big O of n. Okay. So remember I'm I'm not going to explain what omega means. But in another video I'll explain asymptotic notation. So for now just bear with me. Okay. So we have to show this. So let's say that in order takes us time constant. Okay. The computer has to run something for in order walk when the tree is empty. So we have let's say it takes this time constant C. So at T of zero we equal to C for some constant C. This is the time it takes to you know process an empty tree. So this is empty node empty tree right time for an empty tree time empty tree. Okay. So so now suppose we call the in order tree walk on a node X whose subtree has K nodes. Okay. So back in our thing. I don't know where I put it. Let's say that this let's say we call in order traversal on this this subtree and this subtree has K nodes. Let's say this left this whole side has K nodes. Alright. So that means that the right side of the subtree is going to have and minus K minus one nodes. Alright. And minus K minus one nodes. Okay. So remember this subtree let's assume this has K nodes. Okay. Then the right side is going to be and minus K minus one nodes. Okay. We're assuming this. Okay. The reason why it is N is a total number of nodes. And if left side has K nodes, then we're then the right side we have to subtract the total number of nodes minus K and minus the current root. So the right side is going to have N minus K minus one. So suppose subtree has K left subtree. I think it's left subtree. Right. Suppose left subtree has left left subtree has K nodes. That subtree has K nodes. Right. Then right subtree is going to have N minus K minus one nodes. Okay. This is just an example. Okay. The time that it takes to run in order traversal now is going to be bounded by this function. Okay. Now why is that? Okay. So let's say you run a comp. So this is going to be the time it takes to run on N nodes. Okay. The reason why is because that one you have to process the left side. Right. You have to process the left side. And another one you have to process the right side. So this function this is processing the left side. This is the time it takes to process the right side. Okay. I hope you guys bear with me. Okay. So the time it takes to let this is the time it takes to process the left side. This is the function the time it takes to process the right side. Okay. This D is just a random constant that the computer would take for like any verifying like different types of computers. Right. So if one processor has greater time than this is the time it takes for this extra some constant that it takes to process nodes. Right. It depends on whatever computer. So we call this a random constant D. Okay. I don't know why I put for some D over here for some D some D. Okay. So this is the time it takes to process the left side. This is the time it takes to process the right side. This is just a random constant for the computer. Okay. And we bound this. This is the maximum bound that we bound it. Okay. Reason why T of N could be less than or equal to this is because some some computers could be really, really fast. Okay. And really, really fast. It's going to take less than this amount of time it takes. But the maximum time that it could take is going to be this. Okay. It's going to be the time it takes to add the left plus the time it takes to add the right plus some now in a constant to process the current route or something. Okay. That's this mathematical function that we bounded. Okay. So now, now we need to show the time on recursive calls. So we have to prove this. I'm going to assert that this is true. Assert. I'm going to guess a time it takes after this recursive call after number of recursive calls. Okay. I'm going to guess that T of N is going to take C plus D and plus C for some constant. Okay. So these are random constants that we are going to assume it takes. Okay. We are this is assumption we're guessing we are guessing this this time it would take. Okay. So this C is just some random constant that it takes the cost. This D is a random constant for this for it takes the process. I'm guessing one node because like this seems like processing the route. Okay. So I think D is processing the route but it doesn't explain the textbook but this is the we're guessing this time constant. Okay. We are guessing it and C is just remember these are just constants. Okay. So now we need to show so we have to prove that T of N is less than this right. So let's say for N is equal to zero for N is equal to zero when N is equal to zero. So we have zero nodes. Okay. That means that this this equation is going to equal to C plus D times zero plus C. Okay. And this is going to equal to zero plus C which is going to equal to T is zero right. So this means that at T is zero we're going to have some constant C that that it takes to process. So this is when there's zero number of nodes and we're this is going to T is zero is going to cost just a C constant. Okay. So at zero nodes the computer is going to run some amount of time and that's our C. Okay. So now for N is greater than zero we are going to substitute our equation that we guessed. So we're going to substitute this into our equation of this. Okay. Our equation that we pretty much made up that is the upper bound. And the reason why we're able to do this is because we're asserting that this is less than or equal to. Okay. We're asserting that this function is going to be less than that. So that's where we're able to substitute. So using substitution T of N. Hope you guys can see T of N is less than or equal to T of K plus T of N minus K minus one plus D. And this is going to equal to C plus D times K plus C plus C plus D times N minus K minus one plus C and plus D. And then using factoring it's going to get you to C plus D times N plus C. Okay. So we just proved that this answer we got is the exact assumption that we guessed. So therefore it's true. Okay. Because by plugging in the values plugging in the equation that we bounded the upper bound by and because it's less than or equal to it we just proved that it's true. Okay. The reason why we're able to plug it in is because we're assuming it's less than that the bound is less than or equal to our big equation. So by the way let's see how did we plug it in. We just plugged into function C plus D and then we multiply by K plus C right and then we plugged it in the same thing C plus D then we multiply by the N here which is N minus K plus minus one plus C and then plus D and yeah. So yeah that that's the proof. Okay. And basically that that's just proved that our in order tree walk takes theta of N time. So this just proved that so this C plus D times N plus C this is equivalent to theta of N. Okay. And that's theta of N time complexity for this function. Okay. All right guys. So how do you search through a binary search tree? Well it's pretty easy. So let's say I want to find the value for like I want to find this node for how do I search it? Well I'm going to start at the top and if my value that I'm trying to find is less than what I'm currently at I'm going to go to the left. Otherwise I'm going to go to the right. Okay. So four is less than 15. I'm going to go to the left. Okay. Now I'm at six is six less than is four less than six. Yes. Okay. I'm going to go to the left again. All right. I'm at three is four less than three. No. I'm going to go to the right. I'm at four now. Okay. So yeah. That's how you search in a binary search tree. So the pseudocode is like this. This is how CLRS wrote it. So tree search X is the current node. So X and then K is the what you're trying to find. So K is trying to find node trying to find the node you're trying to find X is the current node. So X is the current node you're at. Okay. So if your current node that you're at is null which that almost never happens because that means that like that means it's empty but or K is equal to X's value key value whatever key value doesn't matter. Then I'm going to return X. Okay. That means I'm at the position already. Right. If the value I'm trying to find is equal to my current nodes value that return X. Okay. Otherwise we have to check if what we're trying to find is less than the current nodes value or key whatever it's called. Yeah. That key. Then I'm going to return search X is left. I'm going to search that the X is left. And then otherwise I'm going to do a tree search search X is right. Okay. So this is the pseudocode. So now I should have wrote tree search here but it doesn't matter. So tree search X is the current node I'm at. K is a node I'm trying to find. If I'm at if it's null then that means like well yeah then it's null whatever or my value I'm already at the value then I'm going to return X because that's my current node. I'm going to return it because that means I'm equal to I know where it is. Otherwise I have to check if the current node I'm trying to find is less than my current node I'm at. If the node I'm trying to find is less than the current node I'm at then I'm going to go to the left side return to search on the current nodes left side. Otherwise I'm going to search the right side. Okay. So that's how you search. Okay. Now how do you oh yeah I forgot something. So now let's think about how to do this iteratively. So this is an iterative version. This is a search X K same thing. So X is current node K is what you're trying to find. Okay. So X is current node K is the one you're trying to find while my current node is not null and K is not equal to X is key or value whatever they call it. It really doesn't matter if we got to check if it's left if my what I'm trying to find is less than my current key then I'm going to go to my current key. I'm going to go to left. Okay. I'm going to keep going to left. Otherwise I'm going to go to the right. So remember X is the node you're we're currently on K isn't the node that we're trying to find the key we're trying to find. Okay. And then at the end I'm going to return X. So that's what this means. While I'm currently searching through the node what my while my current node is not null and I'm not at the same at the right key like I'm not I can't find the key right then then what I'm going to do is I'm going to search through the left if the key that I'm trying to find is less than my current node I'm going to go to my current nodes left. Okay. Otherwise I'm going to go to my current nodes right. Okay. I'm the right side. So I'm going to go to the right side afterwards after this while loop is done I'm going to return my current node which is going to be where I'm at. Okay. We're just going to be after this while loop it's going to equal to the X's key or probably will equal to null that means you can't find it. But yeah that's how you do the iterative version iterative version. This is the pseudo code for the iterative version. This is the pseudo code for the recursive version. Okay. Now let's think about minimum and maximum how to find the minimum and maximum. All right guys. So how do you find minimum and maximum? This is how to find minimum and maximum. So let's say I'm at this. This is the tree I'm at 1568. This is the tree that I just drew right. What is the minimum value? Well minimum value is two and what is that that's all the way on the left side over here. All around left side reason wise because like remember left side is the value that's your less than the current value you're at. So the minimum value is going to be all the way on the left side. So this is the minimum value min node. Okay. What about our maximum value? Well the maximum value is going to be all the way on the right side because remember the right side is going to be greater than the current node. So we the maximum value maximum possible value in this tree is going to be all the way on the right side. So this is the max. So 20 is the maximum value in our tree. Two is the minimum value in our tree also. And how do you write it? This is how you write it to find the minimum. All you just have to do is keep going to the minimum. So tree min is the pseudocode that's the lrs did. So while my left X is left is not null. I'm going to go to the left and then after that just return X. So what does this mean? X is the current node I'm at current node. Remember X is the current node I'm at I'm at. So let's say I start at 15. While my left is not null six is not null right so I'm going to keep go to left. Well now I'm at six. Where's my left three three is not null. So I'm going to go to three. Well three. Okay now I'm at three. What is my left two. Well two. Well two is not null. So I'm going to go to two. Okay now I'm at two. What is my left. My left is null. I'm done. So then I'm going to return to so that's how you do that's what this code is doing. Okay. This is my pseudocode tree minimum X while X is left is not null. I'm going to go to the X is left. Okay. And then after that I'm going to return X. So this is the pseudocode. Let's think about how to do maximum now. It's pretty much the exact same thing. Tree max X. This is the pseudocode while X is right is not null. I'm going to do X is equal to X dot right. Then return X. So yeah. So to do that let's go back up to here. So I'm at my current node 15. My right side is not null. 18 is not null. So I'm going to go to the right. Okay. Now I'm at 18. My right side 20. It's not null. Right. 20 is not null. So I'm going to go to right 20. Now my right side my right side is null. Right. My right side is empty. Right. No means empty. Sorry. Sorry guys. If I didn't explain that but my right side is null. There's nothing on the right side. So that means I'm going to return 20. So yeah. That's how you do this tree max. Okay. Now it's going to be a pretty difficult to think about. Let's go. Let's go over successor and predecessor. Okay. This is going to be pretty difficult to go over. Okay guys. So we're going to go over how to find the successor. So what do I mean by the successor. It's the next value that is greater than our current node. That is in order. If it's sorted in order. Okay. So like let's say I'm at 15 the next value that is sorted in order. That's a successor of 15 is going to be 17. Right. So if I were to sort all these nodes from smallest to largest the next value that is greater than 15 in sorted order is a successor. Okay. So next value that's greater than X in sorted order is going to be the successor. So we're trying to find that now. So let's see for 15 the successor is going to be 17. Okay. Because if I sort everything 15 successor 17. All right. Now let's think about this. Well about six. Okay. So what is my successor of six. Well it's such a seven because seven sorted order is yeah it's just seven. Okay. That was not a good that was not a good example to go over. Let's see. Well about 13. What is my 13th successor. Okay. That my success for 13 is 15. So 13th successor is 15. So we have to think about a case for that. But let's think about what's my successor for let's see 17. Yeah. 17th successor 17th successor is 18. So that's that. So we have to think about how to figure out for that case also. So ideally if let's go back to the first case 15 15th successor 17. So to find the successor of 15. It's the smallest value on the right side. Right. Because if it's sort in order the smallest value on the right side the right side is like 18. It's smallest value. It's 17. And that's the in order successor. So it's the smallest value on the right side. So to do that first is going to be the pseudocode. If X is right is not null then we're going to return the smallest value tree man of X is right. Okay. And yeah that's it's pretty much the first part if X is right side is not null. So 15 right side 18. It's not null. We're going to keep going all the way down which is in this case is 17. Then that will be the successor. Okay. Now if there's more nodes let's let's say I had like 16 here then the successor is going to be 16. Right. So you have to go all the way down the smallest value on the right side. So here 18s plus value of 18 will keep going down down down just 16 and that's going to be the success the successor of 15. Okay. So that's how you do this part. All right. Now we're going to think about the other weird case in this case let's say 13 13s weird case. Okay. So as we could see here 13s successor is 15. Right. So because of this we see that 13s right side is null. Right. 13s right side is null. So this we can't just find the smallest value of the right side since 13s right side is null. So what do we have to do we have to literally go up the tree from 13 until we find a node that is a left child of the parent. Right. So if I were to keep going up seven seven's not a left child of the parent six. Right. So I'm going to keep going up six. Oh six is the left child of the parent. Okay. So then I'm going to return the parent 15. So that's how you would find the in order successor in order successor if the right side is null. Right. So let's say find let's find the in order sex successor of nine. So what is the in order successor of nine. Well nine's nine's parent is the left side already. So that's just 13. So yeah. But okay. Yeah. That that was the easy one. Four. It's fine. In order successor four. Well the right side is null. Right. So this we can't just find the minimum of the right side. So we have to keep going up until we find a child that is the left side that is the left child of the parent in this case is three. So then we have to return the parent which is six. So that's that's that. So the pseudocode here is that here's how CLRS did it. So they're going to have a Y which is equal to X's parent while Y is not null. And X is equal to Y is right. X is going to equal to Y. Y is going to go to its parent and then return Y. So Y is going to actually going to be the current parent you're on because you're going to keep going up to the current parents. So it's going to be the parent. Yeah. So current parent. And then yeah. So X is the current node. So what what does this do. So let's say I'm at 13. Y is going to be 13 parent which is seven. So Y is going to be 13th parent or seven when check is Y null. No. Yeah. So Y is not null. And is X is equal to Y is right. Is X 13 equal to Y is right. Yes. So then we're going to go up. Okay. We're going to go up. So now six is seven. Okay. Now we're going to check is Y not null. Yes. Y is not null. X is a seven right. Is X is equal to Y is right. Yes. So then we have to go up again. So we're going to come up here. Now we're at 15. Y is equal to 15. X is equal to six is X equal to Y is right. No. So then after that we're done then we just have to return Y which is 15. Okay. So I hope we guys hope I explained this correctly. So that's that's what the successor is doing. Okay. Okay. I'm going to make it you guys. How about you guys write the tree pre-order predecessor. Okay. So how about you guys write the code for the predecessor write the pseudocode for the predecessor. I don't know. That's just a good good. That's a good good procedure for you guys. Good tip for you guys. Okay. Sorry guys. If I didn't show the pseudocode properly I just looked at the video again. So yeah if the right side is not null then you just return the middle of the right. So then what we're going to do is we're going to get the current height of the current parent of X then while the X is equal to Y is right which is the parent while X is the right child of the parent. What we're going to do is we're going to just keep going up. So we're going to set X is going to now equal to the parent and then Y is going to equal its parent. So that's what this code does and then at the end you return Y. Okay. So that's the pseudocode of this. Sorry guys if I didn't explain it correctly I didn't show it to you guys. But yeah this is a pseudocode. All right guys. So now let's think about how do we insert a node into a binary search tree. Well let's say I want to insert 16. Well I need to find the location of 16 first where it's going to be inserted into this tree and I need to I need to keep track of the parent and the reason why is because that once I get to the end I have to set the parents left or right to be 16. So in this case let's say I want to insert 16. So 16 is greater than 12. So I'm going to go to the right 16 is less than 18. So I'm going to go left 16 is greater than 15. I'm going to go to the right. So now I'm at here. I need to get this parents right after maintain the parent and then 17 after check is it's less than 17 right 16 less than 17. So I'm going to put 16 on the left of 17. Okay. So that's how you insert. So whenever you insert you have to keep track of the parent and the reason why you have to keep track of the parent is that at the end you have to insert your new node at the left or the right of whatever parent you're at. So like when you go down when you're traversing down you have to keep track of which parent you're currently at. So in order to know where you're going to insert your node from. So yeah what we're going to do I'm going to show you the pseudocode now. So this is the pseudocode and I'll explain how it works. So this is tree insert. T is the tree zero Z is the node you're inserting. Okay. So Z is the node you're inserting. Okay. So they say Y is equal to null. Okay. They said Y equal to null and they said X as the current T's root, which is the top root. Then while X is not null, they're going to get set Y is equal to X and then do if Z is these keys less than X is key, then go to the left else X equal to X dot right. Then after that they said Z's parent is equal to Y. Normally you can't even do this. Like when you code. Unless you actually have like a parent node parent pointer for every single key, every single node. But yeah, anyway, Z is equal to null if Y is equal to null T root is equal to Z and else if Z's key is less than Y's key, I'm going to explain all this Y dot left is equal to Z else Y, right is equal to Z. Okay. So Y is actually the current parent you're at. So current parent you're at that you're keeping track of X is the current node you're traversing downward. So X is current node going downward and Z is a node you're inserting. So what we started first is that let's say I'm going to find let's say I want to insert 16 again. Right. So our value we're trying to insert is 16. So that's going to be our Z node. Right. Y is going to be the current parent. And then X is going to be the root that you're currently on. So the current node that you're on. So while X is not null, what they do as they set Y is equal to Y's parent is equal to X. Right. And they're going to traverse downward. So if the node that I'm trying to insert so in this case, 16 is less than my current node I'm at, I'm going to go to the left. Otherwise, I'm going to go to the right. So 16 is greater than 16 is greater than 12. Right. The current node I'm at. So I'm going to go to the right. And then yeah, it'll come back up here. Then it's going to set. So X is not equal to null. Right. So X, X is still not going to equal to not cause like our current value of our current node is now 18. And X is still not equal to null. So yeah, then what we're going to do is we are going to go set Y is equal to X. So Y is going to now equal to this. And then X is we didn't update X yet. So then now they say, if Z is less than if the node we're trying to insert is less than the current node, then we're going to go to left again, otherwise go to the right. So we do the same thing. So 16 is less than 18. So we're going to go to the right. So now I'm not right. That's the left, we're going to left. So now X is going to equal to this, right? Our current nodes and now you're going to go to 15. And our parent is 18, right? Comes back up here. We resets its parent. So now the parent is now equal to 15. And now we're going to check. So now Y is equal to the current node. So now our current parent is 15. And now our X is going to be we're going to check is a node I'm inserting. So my node is inserting is 16, right? Is 16 less than the current node I'm at. So 15. No, it's not. So we're going to go to the right. So now our X, our current node is now going to equal to this 17. And it comes back up here. And then is X null? No, it's not null. So we're going to set Y is equal to X. So Y is going to now equal to X. So Y is going to point to 17, which is the parent is is 16 less than 17. Yes, so then X is now going to point to its left. So now X is going to point to a no value of here. Okay, so so while this this loop is just keeping track of the parent and the current child we're currently going down on. Okay. So now this Z's parent is equal to what so we're trying to insert 16, right? They're assuming that we have a pointer that when I know we're inserting is going to point back to the parent. That's not always the case. So let's just ignore that. But based on your implementation, you could write it like this, you could have another pointer pointing back to the parent. That'll just be really weird though, because imagine doing that. That's technically like a directed graph, think about it, because if you have a pointer going back up and then going down, yeah, over and over again. Yeah, that'll be really weird. So let's just ignore that. We're going to ignore this for now. Anyway, yeah, but what this is doing is it's setting the new nodes parent to equal to the parent that I'm inserting at. So yeah, I'm going to insert 16 over here on the left of 17. So what if I'm doing this, I'm going to set 16 parent to equal 17. But yeah, we normally don't have pointers like that in BST implementations. So let's ignore this. If y is equal to null, okay, so this means that it's empty. So like, if y is equal to null, that means the whole tree was empty, because that means that the I never had a parent that literally means that whenever I'm traversing down, I never had a parent at all. So that means because like, while I'm going downward, I'm updating my parent that I'm currently at, like while I traverse downward, that this means that there's no, there's no, the whole tree is empty. So what they're doing is they're setting the root to be equal to the new node. So they're, so if this is the case, this isn't going to be the new root, right? 16 is going to be the new root, because that means the whole tree is empty. But that's not always the case, right? So that's not the case in this case. Alright, so now what they're going to do, they're going to check check is six is a new key that we're inserting. 16 is it less than wise key, which is apparent? Yes, it is. So what it's going to do is it's going to set wise left. So this is why wise left is going to equal to 16. And yeah, that's basically the whole code. That's how this whole pseudo code works. I hope you guys could see it. Yeah, that's the whole pseudo code, how this works. And that's how insertion works. Deletion is way more complicated. I have to go over that. So I'm going to go over that with you guys soon. Alright, guys, so how do we do deletion? Okay, so let's say the node that I'm deleting 16, I'm trying to delete it, right? If it has no children, well, if it has no children, I could just technically set the parents node to be empty to be null, right? So here, 16 has no children, right? So then I could just have 16 parents set 16 values to be null. So then that will get rid of 16. So that's the first case, the easy case. Now let's think about if I was one, one child. So let's say I wanted to delete 15. All right, if I want to delete 15, 15 has one child, 17, right? All I have to do is move 17 up. That's all I have to do. Okay, because if I delete 15, we know 17 is just going to be like either 17, either or no is going to be less than or greater than 15, right? And because it's only one child, nothing else, nothing else on the left side or the right side is going to bother it. So I could just move 17 upward. And then that'll be my node. Okay, if it has one child. All right, the issue is is if it has two child, two children, if it has two children, we have to maintain the order of the tree by using we have to maintain the order of the tree by taking its in order successor. So let's say, let's say I want to delete 12. If I want to delete 12, right, I can't just take 17 and put on the top, right? The main reason for that is that that's going to violate a lot of things. I can't take like 15 upon top, not 15, I can't put 18 on the top, right? That will violate a lot of things because then if I put 18 on the top, let's say let's say I delete 12, and I put 18 on the top. 15 is going to be on the over here and that violates it because 15 is not greater than 18, right? So if I want to delete the delete a node that has two children, I have to get the in order successor. So remember the in order successor is the one is just in sorted order. It's the next larger value than your current node, right? So 12's next larger value is 15, right? So I'm going to have to replace 15 with 12 and then reorder the rest of the tree. Okay, that takes some that's going to take a bunch of splitting cases and I'll redraw the cases with you guys, okay? Right now. Okay, so CLRS labels these as Q, L and R. So I'm going to do the same thing. I accidentally screwed up the first drawing, but for one child, remember if Q is the parent and Z is the one you were deleting and R is just that one child. If I delete Z, I could just move R up. So then Q would be still be the parent and R will just be moved up. Okay, so let's say I still have one child and I'm deleting Z, right? Z has one child L. If I'm deleting Z, L just gets moved up. So then Q will be Q, then let's see Q and then L. Okay, so these are that's one child. Now let's think about the node we're deleting has two children. So two children. So I'm going to still label a Q, Q, then what does Z, Z, Z, L, Y, and X. Okay, so let's say I'm going to remove Z, right? I'm removing Z. So if I'm removing Z, let's say so Z is an order successor, which is the smallest value on the right side. Remember, at our 12, in order to successors is the one value that's larger than 12, which is 15. So to get that, we have to go to the smallest value on the right side. So 18 smallest values 15. So we have to replace that. So Z is an order successor. What is the right side? Right side is Y. What is its smallest value? It's null. So if it's null, and get ridding Z, what I could do is I could just put Y move Y up. So then if I could just move Y up, then I have L, and then I have X. Okay, so if I don't have an in order successor, so this is the case of no in no successor. If no successor, then I could just move Y up. So this is why move wise value up. Yeah, so then this value becomes Y. Yeah, the Z becomes Y. Yeah, if I delete Z, I could just move value up while up. So if there's no successor, I could just do that. Okay, so now the problem is when you have an in order for your successor. So this is with there's an order successor. So successor exists. So now to do it, let's say we have Q, Z, Z, R, left, L's left. And then there's, is it L? There's, yeah, okay, so in order to successor Y, then X, okay, and we're deleting Z, right? So the Q is just the parent of Z. So okay, so I'm deleting Z. So I'm trying to delete Z. Z is in order to successors Y, right? Y is the smallest value that it's on the right side of Z. So why smallest Y? So how am I going to replace it? Well, what I have to do is I have to replace Y to become the parent of R. And then add it, remove Z and then add that as a new value. So I have to make this So I have to split this into two trees. So I have to make this. So they have Z Q Q is still the parent of Z, by the way. It really doesn't matter Q. So we have to split this into like Q Z and then L. And then what I have to do is I have to change Y, the in order to successor. So why does it know in order to successor, right? You have to make this into the parent of R. So we have to literally rotate this to become the top to the parent of R to rotate it. And then once I once this rotation is done after delete Z, delete Z, then I could set my Y value my in order to successor with the right value. Okay, so let me reiterate that again. If I want to delete Z, I have to replace Y to become the the Y, which is the in order successor, right? We have to get the successor. So Z is going we have to replace Z with Y the in order in order successor, which is the next value in the sorted order. To do that, I have to rotate my Y to become the parent of R now. So then that's what I did here. Y is now the parent of R. And this is ours. R is going to X has to become the child of R. So that's what I had to do over here. Then after that, we just have to delete Z and then move Y up to become Q, Y, L, R and X. Okay, so that's this. That's how the theory of this completely thing goes. If there's a successor exists, we have to replace, we have to rotate our, our tree of our in order to successor to become the parent of R. And then yeah, then once we do that, we have to put our in order to successor up and remove our value. And that's becomes this. So the reason why we could rotate this to become this is because I know that R is going to be greater than Y, right? So then if I want to make my Y my parent, I just move Y to the top and then R has to be on the right side of Y. And because we know X is always less than yeah, yeah, because we know X is greater than Y, but it's less than R, you have to make X as a little small value of R. Okay, so that's what this rotation does. And then it does that. Okay, so CLRS doesn't really have what they did was they they did a thing where they created another method called transplant, which replaces the subtree at a node U with the subtree at a node V. So then you the use parent becomes V's parent and use parent ends up becoming V's child. Okay, so they created like a completely separate method that does that. And I'll explain that. And now. All right, guys, so this is the transplant method. If, if use parent is null, then that means the tree was empty in the first place. So then the tree's route now is equal to V. Otherwise, if use is on the left side of its parent, then the parent use parent, parents left is now equal to V. Otherwise use parents right is going to equal to V. So this is going to change the parent to be V. If V is not equal to null, then V's parent is now equal to use parent. Okay, that's what this does. Okay, so let's see this in action at transplant r y. Okay, so the first if statements not going to happen because it's not empty. So so now in this case, R is you, why is V? Okay, so ours, ours parent. So, so if R is on the left side of the parent, which is not, okay, so R is on the right side of the parent, right? R is on the right side. So it's going to go to this else statement. So then ours parents right is now going to equal to V. So what is our V? V is Y. So then Z's, ours parent, which is Z, Z's right is going to be Y. Okay. Let the left stays the same. So Z's right is going to be equal to Y based on this. So it changes the parent to be Y. Okay, if V is not null, yeah, so Y is not null. Why is V's dot p parents is going to equal to use parent. So use parent was Z. So then this is going to just update the parent pointer, which is going to equal to that. Okay, so that's just going to push Y as the to become the parent of. So what this is going to do is this is just going to move Y up. So now Y is going to become the parent of Y moves up to become the right child of Z. Okay, this doesn't actually move the R. So it doesn't update the V's left and V's right. So we have to be responsible to do that. So this transplant doesn't update V's left and V's right. So it doesn't update Y's left and Y's right. So what this transplant did was literally just move this Y to become the top. Okay. So we have to be responsible for moving this up to the top. Okay, so yeah, this isn't really a rotation actually because like we're actually supposed to move R to become the bottom, the child of Y. Like it's supposed to be this right R and then X supposed to become that. But it didn't do that. So let's see what else CLRS has to say. Okay, so they actually wrote an actual pseudocode for it. So I'm going to go over that. I'm going to write that now. All right, guys. So this is the code for pseudocode to delete a node for Z deleting Z. So if Z's left is null, we're going to transplant Z and Z right. So that's going to move the right node to replace Z. Okay. And that makes sense because if you have one node, the left side is null, then we're going to move right to replace the else if if Z's right is null, then we're going to transplant Z and Z is left. So that's going to move the left node to replace Z. Okay. So based on this case, in this case, Z's are moving Z right. Z's left is null, right in the first case here. So we're going to transplant Z's right to replace Z. So Z's right is R and that replaces Z. So that becomes this. Okay, so that makes sense. Okay, because we're moving if there's one, this is a case for one child. Okay. All right. Same thing with the one child case here. If Z's right now, right here, Z's right is null, then and I'm deleting Z. I'm going to replace L move up to replace Z. So that's what this does is so I'll now replace Z. And that's what this code does transplant Z's left Z. Okay, so that's that move Z's left puts a room on Z. Okay. So that's what this does. Okay, so now let's look at this else case. Y is equal to minimum of Z dot right. So what does that do that gets the in order successor the successor of of Z's of Z. So the successor is the next value that is the next value that's in sorted order. So that's going to be Y. So let's not look at the first let's not look at the if statement for now. So let's say that here we this is a Z right, we're deleting Z. And we're going to find wise in order to successor. Okay, so Z's in order to successor is null. So this is going to be null, right? And in this case, if, if Y dot parent is not equal to Z. So in our case, why is it going to be null, right? Because why is successor Z's right here is why okay, this is kind of confusing. But in this case, these right, the minimum of Z's right is null, right? Because there's nothing here, right? So then this is going to be null. And then we're going to transplant Z Y with Y, right? And that's going to move this up and that becomes this. Okay. So, yeah. That's what this does. Actually, yeah, with the in order successor should be the minimum on the right side. And, okay, it's not no, actually, yeah, it will just move Y up. So that's what this does. Okay, so if Y is equal to minimal of Z's right, then transplant Z with Y, and then that's going to move Y up. Okay, so now let's think about this if statement that we didn't go over, if the parent if Y dot, if the Y dot parent is not equal to Z. So that's this case that's down here. I'll draw it out. That's the one that we went over already. This case, this crazy case, Y and X. Okay. So remember, it does this. Yeah, we have to split it. Remember, we have to make Y become the parent. So I'll show you guys how it does this. Okay, so Q is just the parent of Z. Okay, it doesn't really matter. It doesn't really affect anything. I don't even know why they included in the case. Okay, so Z's in order to assessor, right, which is the smallest value of Z's in order to assessor is the smallest value on the right side, right? So that's going to be Y. So Y is going to be here. Now, Y's parent is not equal to Z, right? Y's parent is not equal to Z. As we see down here, Y's parent is R and it's not equal to Z. Okay, so this it's going to run this if statement. Now we're going to transplant Y with Y's right. So what does that do? Y, we're going to transplant Y with fries, right? So that means X is going to move up. Okay, so X is going to move up. Well, they don't even rotate it. So technically, they're going to move X up. So X is going to move up, and it's going to be like R. And then yeah, X is going to move up. Y's right. Is it going to equal to Z's right? So Y's right is going to equal to Z's right. So Y is right. So here's what it does. Okay. It actually doesn't do this. I'm going to get another sheet of paper to explain how it does it. So remember we have this case that we're trying to go over the last case. Okay. So Y is now, so we know it doesn't have two children, right? It has, it doesn't have one child. So if statement's going to go here, so why is it going to equal to Z's right? So why is it going to equal to the minimum of Z's right? So Z's the minimum of Z's right, R is going to be Y. So Y is going to equal to Y. Okay. Y does not equal to Y's parent is not Z. So Y's parent are it's not Z, right? It's not Z. So what does it do? Transplants Y with Y's right. So what is Y with Y's right? So this is going to move X up. So Y's right is going to be X. And that's what it's going to do. Okay, it's going to move X up. Okay. Then Y's right is going to equal to Z's right. So Y's right is going to equal to Z's right. So then Y's right, Z's right, Z's right is R. So then Y's right is going to equal to Z's right, which is R. So that's what this does. Okay, Y's right is going to equal to Z's right. And then it updates the parent, Y's right parent. So Y's right parent, R's parent is going to equal to Y. And that's we don't care about that. Okay, transplant TZY. So then now what's going to do is going to move Y upward and transplant Y with Z. So then now Z's going to be gone. So remember, there's a Q here. That's just a parent of that. So now transplant Z with Y. So now Z's going to be gone. And then it's going to be Q points to Y, right? Y's left is going to be Z's left. So Z's left is L. So Y's left is going to be Z's left. So that's L. And then, yeah, Y's left parent, L's parent is not equal to Y. Okay, and that's how this code works. So yeah, that successfully deleted the fourth case. And it did the whole rotation thing of our last case of this. So yeah, Raycoms subscribe. I'm really tired. It's like 2am. So yeah, I hope you guys understood my explanation of binary search trees. So that was everything about binary search trees. Raycoms subscribe. I'll check you guys later. Peace.