 Now everyone, so the next thing we will talk about with respect to trees is traversing the tree and Traversing is a fancy name for visiting all of the nodes in the tree. Okay? Traversing you can do to any tree. We're going to apply it to our binary search tree. Specifically, we are going to do a depth-first traversal. Now depth-first means that we are going to go down the tree to the leaf nodes in some way shape or form and kind of like go down and then come back up a little and go back Down again and come back up a little and go down, right? So we're going down and then up and down and up and down There's another kind of traversal called breadth-first traversal where you go row by row. Okay? We're not going to worry about that. We don't need to do that in this course. You will experience it in your algorithms course, though So traversal, again, it is visiting the every node in the tree But there are a couple of different orders in which you can visit these nodes and each order that you visit them in Has a different purpose. Okay? The easiest one to understand is the in-order traversal of the tree Okay, so in a binary search tree, this basically means going from left to right Okay, and if you do that then what will happen is you will get the keys of the tree or the values of the tree in Their sorted order out. Okay, so what it means to visit something in order is first you visit the left child of the tree Then you visit the root and then you visit the right child and The algorithm is defined recursively, right? So for example when we want to So and and every time we visit a node we are going to do something like print out its value Okay, so that's why we're doing traversals next is we want to be able to print out our trees Okay, so an in-order traversal is going to give us a tree printed out in order of its values Okay, so how does in-order traversal work? Let's just kind of take a look We visit the left subtree then the root then the right Okay, so when we start our traversal though because this is going to be a recursive algorithm We have to start at the root. That's the only thing that we know about is the root Left tree first then then you know the middle and then the right subtree Okay, so we start here visit the left Okay, go left By calling recursively an in-order traversal. Okay, now we're here. What's our in-order traversal says visit the left middle right? Okay, go left here Left middle right go left. Okay. We're at the left alright Go left again. Ah, but there is no left So now we visit the middle the current node AK. So let's say we print it. All right print This go right. There is no right Right our recursive call has hit its base case. We're done. We visited all these the children here We're at a leaf so we go back up We just did this left now. We're at the middle print it go right print this No children come up We've already done left middle right. So we go back up here We're at the middle of this guy now. We visited his entire left subtree. So we print GA and Then traverse the right subtree go down here No left print the middle right. Okay, come back up here All right, we've done left middle right of this guy. So what our recursive call returns up and we're at Kansas We've done the whole left subtree do the middle and now we come in here and we go Left middle right for this guy again. Okay, so Kansas. We'll go down to Pennsylvania the right left of Pennsylvania LA Then to the right of LA and to the left come up print this and on and on and on Okay, so left middle right that is a in order traversal Really good for printing the contents of the tree in order. There are other traversals Pre-order pre-order means we do something with the middle first the root and then the left and the right, okay? so that a pre-order traversal would be Kansas then Georgia then Connecticut and Arkansas All right, and we're doing these in order though So we actually like print out KS now come here print out GA now come here print out CT now come here print out a K Come back up print out DE Come back up come back up now print out H. I come back up now come back up now print out K s Now PA now LA so it's set or it's set or it's set or it's just In order pre-order post-order it depends on when do you do that thing to the middle to the kind of current nodes value? Pre-order says visit the root first then the left and then the right Post-order says go left go right and then do the middle Okay pre-order traversals you copy the tree you basically rebuild it Kind of in the order it was made you make this From left to right post-order you go all the way down to the note that leaves and start deleting them So you like crop it from the bottom up, right? So Take a minute and practice your pre-order in order and post-order traversals Make sure you understand them by applying them to this tree Okay, so pause the video now, and then we'll come back. We'll give you the answers Alright, so an in-order traversal of this tree. Remember in order is you visit? left middle Right, okay, that's in order traversal. So an in-order traversal of This tree you are always going to start at the root. So we go left Left okay, there is no left from three Okay, so three is the left most then we print out the middle three And then we go right This guy has no children. So he gets printed out three four Okay We come back up Now we're at the middle here. We're at the middle of this Subtree, okay, so we print out five and then we go right Right has no children. So we print it left middle print right no child come back up Okay, now we're at the middle of the whole tree. We're at the root. So we print it out eight Okay, left middle right go right left nothing there middle 11 right Okay, come down here do it again left middle 15 Right, there is none. So this is it right the in-order traversal is in order of the values Now let's do pre-order pre-order says you visit the middle first the root then the left then the right Okay, so let's do that So middle first so we're gonna print eight Then go left Right go down visit here. We start over middle five And we go left Okay, three middle There is no left and then we go right Okay, middle is for no left no right. We're done. We come back up We've done middle left right here. We come back up. We're at five. We've already done the middle We've done the left now we go right Down to seven okay print out seven seven has no left and right. All right come back up Now we're here at eight We've already done middle left now we go right Down to 11 and then down to 15 Okay, so that's pre-order traversal right and so look at the way this was built up We went here here here here kind of built this side up then we added this this this Great for copying the tree right finally post-order we go left child right child middle Okay So left right middle What does that look like? Okay? We go left Go left There is no left. All right go right Left right middle left right Neither left or right now. We finally do the middle. So first thing we print out is four Four come back up left right middle three Come back up left right. So now we descend. We're here. We've done the left side here now We got to do the right side seven Left right middle nothing there now finally we can do the middle here Look think of it like we're deleting first We have to go down here and chop off everybody down below this node five and once we've chopped off Everybody below this node now we can finally delete the node five Okay Come back up left right Jump down here to 11 left right Down to this node left right middle. Okay now finally we can print out 15 Come back up. We've done the left on the right come back up print out 11. Okay done Come back up. We've done the left subtree done the right subtree finally the root Okay, so this is what a post-order traversal will look like Now take a second and observe these are all like totally different and it's not like this is just the reverse of this or That sort of thing you really have to know these algorithms So think of them in terms of left middle right and whenever you do that middle step That's when you're gonna like print it out or whatever you need to do Okay, what is the big O of any traversal algorithm? Well, you have to visit every node Right, you're visiting every node. So the big O of a traversal algorithm is always big O event Okay All right, so let's step over to the code and implement. We'll just implement one of these All right, so I'm gonna go I'm over here in my tree map again And I'm gonna scroll down a little bit and I'm gonna find my Traverse, let's do the in-order traversal, right? So the in-order traversal performs an in-order traversal of the tree map to print each node So that's what we're gonna do when we hit the middle part, right? Note that stirrer is already defined for the tree node. So you can just print a node Okay, do not print anything if the tree map is empty All right, so let's work on this. Let's do our traversal First thing let's handle the special case usually you want to handle those first So if the tree map is empty, how do you know that the tree map is empty if its root is none? Don't do anything. So just let's get out of here. Let's return. Let's get out a dodge, right? Otherwise, we're going to traverse the node the tree Now we're gonna want it'll be useful to us if we can Have a node to kind of keep track of where we are in the tree, right? Now note that this can be called the way it's defined we can traverse in order and We can traverse with on a node Like that will be our starting node That's passed in here or this starting node could be none Now the way I have the setup is that the starting node will be none if you call traverse on like the whole tree map, right? So something like tree map dot Traverse in order All right, so that when you see a call like this Traverse in order should start at the root. Okay, so If node is let's let's make a node to kind of like help keep track of where we are if Kerr Will set Kerr equal that's gonna be our thing So if node is none then let's start traversing at the root. Okay Otherwise Let's use The node as our starting point this will support the recursive call, right? Because now, you know as we start to descend the tree We can call traverse in order with either a left child or right child. All right. Let's see what that looks like so We're gonna do in order traversal remember in order Says left middle right, okay, so If there is a left child if there is a left child So you can say if is not none if there is a left child for the current node Whatever that is whether it's the root or some descendant in the root wherever we are if current dot left is not none Then make your recursive call self dot traverse in order the left side This descends the left side first Now once this returns Right once this returns out or if Kerr dot left is none then there's no left child or we've already processed everything So here is where we want to print left middle do the left do the middle and Now finally do the right side, okay But you only want to make the recursive call if there's actually a right or left subtree if it's not there You don't need to do anything. You there's nowhere to descend down below self dot traverse in order Kerr dot right Okay So if we've done this right We should have some good tests here. Let's grow up a little All right test eight in order traversal. This is what it expects So I have created a tree and I have inserted a bunch of things. It looks like if I've inserted The key Alice with the value three three three three three banks with one two three one four five Etc. Etc. Etc. So I've got a little tree already baked in there And my traversal is successfully visiting them in the order that it's meant to be Okay, so wrap your head around the concept of a pre-order traverse or an in-order traversal Let me know if you have any questions By the way Traverse pre-order and traverse post-order are just variations on this thing Right the big difference between pre-order and post-order is when you make your recursive call. Are you going left? middle right For in order for pre-order. It's gonna be middle left right for post-order left right middle Okay, so it's just the order that you do these things and will change up All right, so that's traversal next time we'll go about searching this tree after all it's a binary search tree Probably time to search it