 Hi everyone, so we're in our code now. This is the code for assignment 9 that I have given you We're and again for assignment 9 what we're going to do is we're going to implement a map ADT and Unlike assignment 7 where you did a hash map now we're going to use a tree map So this binary search tree is going to be our underlying data structure. Okay now The components of the binary search tree are tree nodes, right? Those are the things that are going to hold our data values and have the left and the right children Concepts kind of like linked list nodes, but on steroids. There's more to them So we are making a map right in a map associates keys and values just like a python dictionary, right? So we want to be able to call things like tree Sub 850123456 gets Mike, right? This is kind of the dictionary style the map style So the way we will accomplish that is every node in our binary search tree is going to hold the key And this is the thing that we're really going to care about the most in our algorithms and the value that's associated with that key so we've actually got two data points in each node plus a Variable that holds a reference to the left child if there is one and a variable that holds the reference to the right child If there is one okay, so this is what we're going to put in our tree All right, so here's our tree map Now just like the linked list if you remember the linked list only knew about one node And that node was the head in the linked list in our tree map The tree map is only going to know about one node and that's the root, okay? So we are going to initialize our tree map to hold the root And we initialize it to none. This means that the tree is empty. Okay, so let's go write our code that Can insert data into this tree? All right, so here's insert. So by the way, you see all these Rays not implemented error as you go and complete your assignment. You're gonna delete these lines These are just here to support testing Right, so if you run the code just like with your other assignments, it'll run tests for you So whenever you're ready to have One of these functions or methods be tested go ahead and get rid of this line, right? It shouldn't be in your finished code All right, so let's write the insert code to do that We're going to take our algorithm that we wrote and Modify it just slightly So insert Method is going to insert data into the tree map and we're going to do it recursively The other thing that we have to worry about now We're going to change it up just a little bit because we're dealing with a map Okay Our dictionary when the key to insert already exists in the tree map Replace the tree nodes value with the new value, right? So this is to handle the case where like you previously called tree map Bob non gets one two three and then later you want to reassign it, right? non gets Nine nine nine, right? So we got to handle this case as well where we are replacing the value associated with the key Okay, so let's do an insert All right So let's go back to your algorithm that you have on your binary binary search worksheet Okay on the first page All right, so let's just translate that into Python if the item to insert is less than current dot data So you should assume and this is what we are going to do whenever insert is initially called It will be called on the root node. So why don't I even put that in here insert? Will initially Be called with Kerr equal to the root node, okay Where are we going to do that call we're going to do it in set item down here in a minute, okay? But let's not worry about that just yet when we start Kerr will be the root You don't need to know that because this algorithm is recursive though Okay, the algorithm doesn't need to know if it's dealing with the root It just needs to know what's going what the current node is whether it's the root or something else. All right So let's translate that algorithm into Python if the item we are inserting the item that we care about is the key Okay, we're inserting key value pairs our binary search tree We're going to organize it according to the keys of the dictionary the keys of the map Okay, so if the key That we are inserting is less than the current nodes key value Okay What do we need to do? That's like saying all right You know insert at value with key one and the current tree roots node or the current nodes value is five Where do we go we go left? Okay, so if current dot left is none I've found the right place to insert this thing. Okay, so let me put it there Kerr dot left gets We're going to make a new node We're going to insert that leaf onto the end and the tree node takes two parameter the key that you want to insert Okay, so that's given by the key parameter and the value you want to associate with that key That's given by the value parameter. Okay That's that part Now if Kerr dot left is not none in other words, there's something there we make our recursive call else Self dot insert right I have to call self because I'm in in a class here. You're calling class method I'm going to insert on the left child so Kerr dot left and Then pass it the data the same key and the same value This is the recursive call All right So else actually else if the key is greater than current dot key What do we want to do? Well, all right, we're trying to insert the value the key value 55 and the current value is five we got to go to the right. Okay, so if Kerr dot right is none Great, I have found the right place to insert it if you're getting kind of Confused by all this just go back go back to your map here Trying and understand what's going on these values are your keys here We just happen to be inserting a second value That's associated with it But these values are your keys make sure if it's going to go a long way to helping you debug this thing If you have a really good concept of how insert works at this level. All right So we're inserting to the right else the right ain't blank There's not room there. So we recursively call insert on the right child. All right So there's a final scenario we need to handle here in our key map our tree map Excuse me, which is when the key is already in the tree map Replace the existing tree nodes value. Okay, so if the key isn't less than Current nodes key and the key is not greater than the current nodes key. It must be equal to Key equal equal current dot key. So replace the existing tree nodes value with value easy Kerr dot Value gets value Okay That's it. That's our insert code now while I'm here I'm gonna take care of the length function Okay, length should return the number of nodes in the tree map and this algorithm must have a big O of one implementation So if we were silly and we wanted a big O and the implementation We could kind of go through everything in the tree and just keep counting nodes or Smart thing to do would be as we insert we increment some counters somewhere so in my Constructor for the tree map. I'm gonna initialize a size variable and initialize it to zero and then every time I insert a key or insert a value Into our tree map. I'm gonna insert or increment this value Okay, but only when I insert a node Which is when I hit my base cases and then later when we go to do deleting from the tree We'll Decrement this counter as well now Len in order to implement it I just need to return self dot size and as long as I am updating the size when I insert stuff This will be correct. All right, so let me run my tests and let's see what it says Okay, not implemented error set item. That's okay. I haven't gotten there yet So set item. All right, I don't have tests for this yet because Now we need to use this insert function So that I'm pretty confident this insert function is correct or method. Excuse me is correct But now we want to use it, right? The way that we are going where we are going to use it is with the set item method on our tree map So hopefully you recall from your hash map assignment Set item allows you to use code like this Tree with the bracket. This is the key gets the value. Okay, so We're gonna have set item call insert So This is pretty easy. We just need to check What we want to do is call self dot insert What? Well, where are we gonna insert? We're gonna insert at the root Key and the value Okay So this kicks off the recursive call to insert Now before we do this though We got to handle this one special case and that special case is when the tree map is empty How do you know if the tree map is empty? well This is the initial state of the tree map So a tree map is empty when its root is none or when its size is zero Okay, so I'm gonna say all right if self dot root is none What do you want to do? Okay, I'm saying insert the very first thing into our dictionary. All right Well, the very first thing I'm gonna insert is gonna be attached to the root It is the root the very first thing so Self dot root. This is just like when you add it in your linked list and it was empty You make that first thing you add the head of the list here We're gonna make that first thing you add the root of the tree self dot root gets a new tree node With the key and the value that we're passed in And we also need to increment the size. Okay else we're gonna insert All right That's the code. Let's run it. Okay. I'm failing a bunch of tests because I haven't implemented them yet but Here's my set item Good set item seems to work constructor works Length is working. This is a good sign because length works by testing set item. Okay, so I'm making some progress. It's a good Good sign. Okay, so this is the code for insert and set item We're gonna pause here and we're gonna go back to your worksheet for the next part of this