 Now so far we've talked about a number of different trees That we've kind of seen in our our videos. So we have our traditional binary search tree This is effectively what allows us to kind of operate in some kind of confined manner where a node can either have one child or two children and And you know or no children But we worked off the principle that anything that was to the right was greater anything to the left was less than And we expanded on that we started to look into a few different trees We actually took that same idea of well Oh, this I can only have zero one or two children and then we expanded it to something like a two Three tree and the entire premise behind this is now Maybe we can have we can have a node with two keys and Three children Now a two three key is just a very very specific implementation of what we would call a be tree Be tree a balanced tree, but that's not the only type of tree that we talked about We also introduced something like an AVL tree and the entire premise behind the AVL tree was that it was Self-balancing by using the balance factor by using the balance factor It would do multiple rotations To set itself back into some particular order and then we also introduced this idea of splay trees Which moved Moved to the root now one of the issues that we have with every one of these is We get hung up a little bit on how we interact with it when I insert and remove in a two three tree I'm stuck with the idea that I have to split and I have to do fusions and transfers when I do AVLs I have to do rotations and if I that rotation didn't work then I do a double rotation and with splays Well, I do that same thing, but I just don't do a balance with it So one of the things that we want to introduce is a different kind of tree that looks To try and resolve those problems and we like to call that something known as the red and just because I Want to black tree So this red black tree it does follow a few rules That we do have to kind of maintain as long as we maintain these rules We can actually keep a very balanced tree. So the first rule is root is Black and now this seems a little interesting what you see we have these two colors here And what we're looking at is that my tree is now going to have nodes that are either red or black So when I say root must be black, we're saying that that root node has to have the color black to it the next one is all Leaf nodes are also black Now we you're trying to say to yourself well, when do it when do we start getting a red node in this place? well, ha ha ha how conveniently all children of Red nodes are black and Finally we have something known as a black depth We must maintain this and the entire premise behind this is that What we're looking at is for from any node from any node all children must have the same number of black nodes The entire premise here nodes is again if I'm looking at any Node say for example, I'm at the root of my tree from all Potential paths that are leading from this node. They must all calculate out to the same number of black nodes So let's actually kind of take a look at one And if you kind of take a look and you look at this for a moment You will see that yes. Ah, the root is black and Huh? Leaf nodes aren't black. You know, they're here here here so one of the things that's interesting about a Red black tree is where we're sort of doing a little bit of a disservice right now the idea is if We were to implement this if you know we were to spend time with each one of these each one of these is Some kind of node which means that this node has a left child and a right child The reason why this is important is because if I look at five if I look at 20 if I look at 40 65 80 90 They all still technically have some child It's just empty right now And so what we would do in our kind of to maintain the rules of a red black tree is We would also kind of visualize that all of our Nodes If they can have a child it is a black null child So just to kind of finish up here. You see that I'm Putting in the final touches of these null children and Done So if we follow that premise again that all of my nodes must have children And if they don't have children those are null children we classify those null children as black that second property works as well Then you see if all the children of a red node are black. Well, here's a red child or here's a red node Here's a red node. Here's four red nodes the children What do you know are black the children are black and if we're looking at these these bottom ones that have Null children since we classify a null node as being black We would say that this does in fact still work in our position Now finally we were talking about that idea of black death Black depth there we are The idea is from any node. Let's say just arbitrarily we pick 15 From any node all the possible paths from that node are going to equal Or have the same number of black nodes in them. So how many paths can 15 take it can go down the route. I'm going to Do a little bit of an eraser. I'm going to get rid of just this so we can see it You know that that's true Let's see. We'll take it and we'll We'll shrink it down There we go Yeah So that works so like I was saying I Have 15. Well, what are my paths? So I can go down to five and I can go down to its left child I can go down to five and I can go to its right child. I can go down to 25 Down to 20 down to its left child down to 25 down to 20 down to its right child Down to 25 down to 40 down to its left and then again for its right For every single one of those paths that I just identified I would count the number of black nodes that I see so in case that first one I see five that's one and then again this null is still classified as black So we'd see that this has a depth of two We check it for its second path and we see that five one black node black null node two Then when we get to 25 well 25, that's a black node. So we count it We don't count 20 because it's you know not a Black node, but it's leaf node is so what do you know this one happens to have two And as you can already guess so does this one so does this one and so does this one You could do this for every single node if I did it for five It has a black depth of all possible paths one-on-one if I did it with 50 I did it with 85 every single one of my nodes would work perfectly