 What's up guys, my name is Michael and welcome to my YouTube channel today. We are going to go over binary search tree insertion so the Basically, we're just inserting a node into a binary search tree So let's say we have this tree four two seven one three If I want to insert the value six, it's going to be right to the left of seven So how would you insert a node into a binary search tree? So the good thing about binary search tree is that the left side is always less than the current node And the right side is always greater than or equal to the current node So as you can see here, we have four is our root To all the values on the left side of four such as two one three It's always less than four and all the values on the right side of four is seven and it's always greater than four Greater than or equal to four so yeah to insert into a binary search tree all you have to do is just Essentially keep going to the left side Actually, essentially what you're doing is your check you're comparing the the root with like the node that you're inserting and then If it's greater than the node then your root you're gonna recursively call on the right side And if it's less than it, you're gonna recursively call on the left side So I'll just go over the code with you right now So I actually created a helper function because I didn't want to deal with like I Don't know. I didn't like I don't want to deal with like passing in data over and over again So what I did was I created like a new node Called the new note that we want to insert to insert net pass in data to that to create my new node first You technically don't know how I have to do this But that's why I did and I just called a this helper function to insert my new node to insert and my current root I pass in and then I set my root to equal to this new helper function and I return the root Because that's what they want to return the new root. Okay. So what is this code for the helper function do? Basically, I have the node that I want to insert and the current root so The base case is if the current root is null, then I just return the node that I want to insert Otherwise, I get the data at the root and what I do is if the data that I want to insert out is less than or equal to The roots data that I'm going to recursively call on the left. I Said roots Latin new on roots left is now going to be The helper function recursively call on the left side of roots left that pass in to insert node Then if to inserts Otherwise if to inserts data is greater than the The data at the current root, then I'm gonna call root Arrow right is gonna recursively call on the right side of roots, right? I'm gonna pass in my new node to insert At the end. I just returned the root So I could go over an example on why this works and yeah So let's actually go over that right now and let's actually use an example of This sample input on why it works. So here we have You have this right Okay, we have this so let's say I want to insert six So first I create a new node six Right. I just have this move. That's what this first Line of code does it creates a new node of six and I'm gonna pass in my helper function of six and the Root that I wouldn't want to insert that. Okay. So now what do I do? What do I do? If the root is no, which is not right the root is starting at four. This is not no So what do I do it is um? I'm gonna get the roots data. So what is the roots data? It's gonna be four right roots data is four If the value that I'm inserting is less than or equal to roots data Which is not right six is not less than or equal to four. So we don't even do this. All right, we don't do this So we're not gonna do that Now we're just gonna check is the value that I want to insert at greater than roots data So six is greater than four than roots data Roots data is four six is greater than four. So we're gonna go to the right side of This bright side now recursively call on the right side So that's gonna be Yeah, that's gonna be seven. So now we go back to the top and root is that is now seven So is root null? It's not So i'm gonna get the data at root, which is gonna be seven. So now root is seven by the way root is seven Okay, so now now i'm here Right i'm at seven So now we're gonna check is roots Is the value that I want to insert less than or equal to roots data. So is Six less than or equal to seven it is it is less than or equal to seven So i'm gonna set sevens left sevens left. So this left Ah crap Um Yeah, so sevens left Oh my gosh, this is so hard to circle sevens left is going to point to um recursively calling on roots left on sevens left So sevens left is going to be null. So i'm gonna recursively pass um The null and the node that i want to insert at which is six So i recursively pass in here then We're gonna have uh null is our root and the value i want to insert is six So is root equal to null? It is right roots left is null because it's null then we're gonna return six So that's going to return six and then Uh sevens left is going to equal to six Eh Yeah, this is sevens left So yeah, and then um After that we return seven So after we return seven it's going to go back to here. So then it's going to say four's right is going to equal to Seven so then four's right is equal to seven. So it's going to return that so then it goes back to the top And then after four is right is equal to seven. It's going to return the root Which is going to be four the top of four. So yeah, then our whole tree gets returned So yeah, that's pretty much the gist of recursively calling this function. Um, I hope you guys understand what i'm doing basically i'm just Going to the left if it's If it's less than I go to left if it's greater than I go to the right So then I'm going to insert the value that I want to Insert it at But uh, yeah, essentially that is all there is to it and uh, yeah, I got a seed on this question But that's all there is to uh inserting into a binary search tree Hope you guys know this video write com subscribe. I'll check guys later. Peace