 Hi everyone, so still talking about binary search tree data structure. We've talked about how to insert into the tree We've talked about the various Traversals you can do of a tree and now let's talk about its namesake the search. How do you search the tree? well searching is Fairly straightforward what we want to do is we want to know if a target item or a target key is somewhere in the binary search tree and What's going to make this relatively easy is that this tree is already sorted the binary search tree property says that if you started a node all The values to the left of that node and its left subtree are less than it and all the values to the right in this right subtree Are greater than it and that makes the search process relatively straightforward and in fact either the iterative algorithm or the recursive algorithm They're both pretty Elegant so let's just kind of make sure we understand how to do the algorithm. What's it going to look like, right? So Here's the root of our tree. We've got a bunch of state abbreviations here And what we want to do is kind of similar to the insert algorithm We want to check the value that we're searching for and say is it less than or greater than or equal to the item here and Depending on the answer to that question that will decide where we go to look next, okay? So if we want to search for Delaware DE we're going to start our search at the root of course and First we're going to go where well DE is less than KS. So where's the lesser value is going to be it's going to be in The left subtree so we go left DE is less than Georgia. Okay, go left DE is greater than CT alphabetically. So we go down here and voila. We've found it, right? So how many nodes do we visit? We visited one the root two Three and then on the fourth node. We actually found it, right? So we visited four nodes here But the logic the logic is You know is the value I'm looking for less than greater than or equal to The current node, right? So for Kansas when we start out Kansas. Well, it's not less than it's not greater than It's equal to right. So we only visited one node there the root node Okay, how about Louisiana? How many nodes do we visit? Well, we start here and change the color up start here at the root and LA is greater than KS. So we need to go right LA is less than PA. So we got to go left and LA is equal to this value, right? So we visited one two three nodes here Now how about a node that's not there, right? And how do we know? That the node isn't there. Well, let's walk through it. What should we do? One more color. How about what is this purple? Okay, let's go purple NC so NC is not here. We're gonna start our search at the root. NC is greater than KS. So we go right We're looking for NC here NC would be less than PA. So we would go left NC would be greater than LA. So we would go right NC would be greater than ME Right, but we cannot go right here because there is no child of ME Right, so that's how you know when something is not in the binary search tree when you're looking for it and you need to go in either left or right, but you can't because there's no child there, right? so We visited one two three four nodes and we know Because there is no child here But because there is no right child of ME We know that NC cannot be in this binary search tree okay so Iowa is also not in here and What will happen? We'll go Kansas Georgia It Hawaii and then we want to go right from Hawaii, but we can't so here also we've gone three But because there is no right child of Iowa Or excuse me of Hawaii We know that Iowa cannot be in this BST, right? And this is all made possible Because of the way that we inserted into the tree because every time we inserted we maintain that binary search tree property All right, so Let's answer these questions down here. How many nodes are in the tree? Quick count will tell you that there's 10 of them How many comparisons are required in the worst case? Well in the worst case we have to go all the way down to the bottom of the tree We have to go to the deepest node in the tree Okay, in this case in this tree that deepest node is going to be four levels deep, right main Delaware Arkansas these are the Arkansas. Yeah, or no Alaska. These are the the deepest ones, right? And they have depth one two three Because they are you know you visit four nodes the root and then them All right, so how does the number of comparisons relate to the height of the tree? Well, the number of comparisons is The height in the worst case plus one I should put worst case in here and I will do that before I give it out to you guys How does the worst case number of comparisons relate to the height of the tree? Well, it's the height of the tree plus one right the height of this tree is one two three Worst case height plus one. All right So given that draw a tree for which the big O of search is big O of n Well, that's going to be one of the trees Like this right where you've inserted it in order or in a reverse order, and it's just a long chain Okay, so That's not a good scenario. So again, hopefully it's kind of Apparent that the goal for your tree is that it looks like a tree that it's not a chain of things Okay. All right, so let's go write our let's go revisit our find algorithm here and fill it in All right, so we're going to search and we're going to search recursively. So let's think in this case Let's think for base cases first. So what's the base case? Well, we're going to just keep calling search on the descending nodes, okay, so if current if our current node Is none. All right, let's think of this case. What happens? Well, that's going to be our base case. We're going to say the item is not there We have called search over and over and over again, and we didn't find it on the other hand if cur.data is equal to the target Hey, then we found it right Return true. It's in Our binary search tree Otherwise, now we need to do our some other comparisons, right? If the target is greater than data, what do we want to do? We'll search for it in The right subtree and what are we searching for? We're searching for the target. Here is our recursive call else Well, the only other option is that it's less than current.data, so we should search in the left-hand side of the tree Okay, so this is the recursive structure now the reason I have this cur is none base case is because you know, I don't look at These left and right nodes before I jump I'm just saying hey jump down into them And if they are no if they're empty Return false it gives me just a little bit more elegant code on the recursive side if I do it that way Alright, so this is our recursive code Let's hop over to our Python for just a second and implement find It's going to look a whole lot Excuse me for a second. It's going to look a whole lot like that pseudo code. We just wrote So here's find right and what we are going to do is you're going to use this find method in calls to get item Which supports calls of this form, you know tree sub the key give me the thing there and also contains Right, so you're going to call find here. Okay, so find for us in our tree map is Find the key in the tree map and return its associated value All right, so let's write our Algorithm raise a key error if the key is not in the tree map. Okay so we're going to do that same thing where our base case is if We get all the way to a leaf and we try and descend we wind up calling find where the current node is none Okay, so if the current node If the current node is none, what does that mean means the key isn't in the map We've searched for it, but we've not found it and I tell you in this case Raise a key error just because this is what a Python dictionary would do Else if the current key curr.key the current node. I'm looking at is equal to The key value you're searching for great you found it What I want you to do is return the value Associated with this key. Well the value associated with the key is stirred in current dot value Current is going to be a tree node From up at the top here All right, so we're looking at the key and the value of the tree node. All right else okay, if Current if the key is Less than current dot key. Where do we go? We got to go left so we're going to call find on The left child where we're looking for the key the key is our target in this case But we got to do something here. Don't forget this step. This is a recursive call. We're descending. We're descending We're descending. We're descending and if we find the thing we're returning it Okay, so our recursive call is returning something as The recursive calls start to unwind you need to also return the results from these recursive calls Okay, so we're going to return this all the way back up the chain Okay, go back to the recursion worksheet from a couple weeks ago You'll notice we've returned the values that kept coming back same thing here All right, finally the key must be greater than the current key value So we're going to return self dot find on the right child Okay, so that'll get us there with find now again What you want to do is you're going to want to use this in your get item and your contains, right? So Let's do contains here just very quickly Support of the call of the form is x in the tree Return true if x is a key in the tree and false Otherwise, well you have this find method So where do you start? searching You start at the root of the tree So let's return a call to find on The root of the tree looking for the key Okay, excuse me looking for item All right Will that do it? I think it should Let's give it a shot. Oh I'm sorry. So we don't want to return here. We want to return true or false. Okay, so the way to do this is going to be if Find how do you know if the thing is not in the in the map? You know, it's not in the map because fine will raise a key error So what we should do is we should try right? We should do some exception handling here Try this and If a key error happens return false Okay Try this if a key error gets raised. We're gonna catch it and Say false the key. It's not in there, man. Sorry, but if this succeeds if we do find it All right contains this is x in tree contains is the in keyword you return true or false We're not returning a value So if we get past this right if this works We're going to return true Okay All right, so let's run our test and hopefully our test for contains will pass Testing for contains here we go Looks good, okay All right, so you will also be able to use this find method here in get item I'm gonna leave that part up to you in the next video. We'll talk about the last thing which is deleting from the tree