Okay, so hopefully you said no, that doesn't work, and here's why.
The key reason why this doesn't work is because of this while loop condition
that we have here.
We're running that loop as long as curr is not equal to null.
Meaning that once that loop stops and breaks out, curr is equal to null.
It's actually fallen all the way off the bottom of that tree, it points to nothing.
Well, that's no good because now when we create a new node,
we end up creating it just off the bottom of the tree.
So we can create it, have curr point to it, but
it's not connected into the tree at all.
So it hasn't successfully been inserted into the binary search tree.
In fact, what we have done here in this statement is we've linked that node's
parent to curr, which was null, and so it's just floating off there in space.
Once curr goes away, it'll go away too.
So that didn't quite work.
The key to this algorithm is, and the implementation is,
we need to stop curr before it falls off the bottom of the tree.
So, in order to do that,
we're gonna need to change the structure of our while loop just a bit.
So here's what we're going to do.
In some cases, like this one here, where curr is pointing to C,
which is a leaf node, it might be pretty straightforward.
If the node curr is pointing to is a leaf,
it has no children at all, then it's easy to tell that we should stop.
But other cases is a little bit more subtle.
So what if curr is pointing to this node, B?
B has one child.
How do we know if curr should stop?
Curr should stop if the node is supposed to be inserted to the left, but
it shouldn't stop if the node is supposed to be inserted to the right.
So we're gonna need to do something a little more clever than just check to
see if the node is a leaf.
And I've alluded to what we're going to do,
which is we need to check the direction we're going to go first,
in order to determine whether we wanna stop or keep going.
So what we're going to do here is we're gonna create a while loop whose condition
checks first which direction curr is trying to go, and
then verifies to see whether that direction it's tried to go is not null.
So, if comp, as we saw before, if comp < 0, curr wants to go left.
And as long as that left child is not null, it can go left, so
the while loop should continue.
If comp > 0, curr wants to go right, and
as long as that right child is not null, it can continue.
So there's our while condition.
Now, inside the loop, our goal is just to walk curr down the tree.
Again, we need to check comp.
If comp < 0, curr goes to the left.
If comp > 0, curr goes to the right.
Now there's one more thing we need to do, which is we need to update comp so
that it's ready to go for the next while loop check.
Now you'll notice that this while loop doesn't handle the case that
comp is actually equal to 0, and we'll talk about that in just a moment.
So, what should we do next?
Well let's consider what might be true when this while loop ends.
So when does this while loop actually break out of its loop?
It breaks out if either of these two conditions is true.
So, either curr is pointing to a node that's about to fall off the end,
so it's ready to insert the new thing that it's trying to insert.