Would you like to inspect the original subtitles? These are the user uploaded subtitles that are being translated:
1
00:00:09,050 --> 00:00:16,160
High in this video we're going to review the properties of red black trees and also see how the process
2
00:00:16,160 --> 00:00:19,720
of inserting into a red black tree works.
3
00:00:19,730 --> 00:00:25,910
So right here I have a binary tree that's completely unbalanced.
4
00:00:25,910 --> 00:00:31,430
So if you want to put values on this you could say something like this is a 10.
5
00:00:31,430 --> 00:00:36,540
This is a nine eight seven and just keep going down the line.
6
00:00:36,790 --> 00:00:41,820
It's possible for a tree to look like this even though this doesn't even resemble a tree.
7
00:00:41,870 --> 00:00:49,940
And when this is the case the complexity for searching on a tree like this actually equals order of
8
00:00:50,040 --> 00:00:55,980
n and which if you know your complexities you know that that's extremely slow.
9
00:00:56,150 --> 00:01:04,700
And the reason for it is because the system will have to iterate through every single node looking for
10
00:01:04,850 --> 00:01:06,430
the item that it wants.
11
00:01:06,560 --> 00:01:10,600
And so there has been a ton of research on how to fix that.
12
00:01:10,640 --> 00:01:18,800
And one of the procedures that people use is to create a red black tree and a red black tree what its
13
00:01:18,800 --> 00:01:27,020
goal is to develop a well-balanced tree and trees which remain balanced and then they can guarantee
14
00:01:27,350 --> 00:01:32,310
a search complexity of order of law again.
15
00:01:32,330 --> 00:01:37,920
And in a dynamic environment they also can be rebalanced.
16
00:01:37,940 --> 00:01:42,510
So as you insert the tree will actually rebalances self.
17
00:01:42,590 --> 00:01:47,340
There is an additional complexity which also equals the order of law again.
18
00:01:47,390 --> 00:01:52,570
While that's happening so you have to you have to kind of know that that's something that's required
19
00:01:52,580 --> 00:01:57,290
however it is a great way of making sure that you do have a balance tree.
20
00:01:57,350 --> 00:02:03,250
It's also important to know the properties of a red black tree.
21
00:02:03,350 --> 00:02:09,270
Some of the properties are that every node is either red or black.
22
00:02:09,380 --> 00:02:19,730
Every leaf null is black meaning that every single leaf whether it whether it be red or black has what's
23
00:02:19,730 --> 00:02:24,800
called a sentinel node which is a node node which is a pointer to nothing.
24
00:02:24,800 --> 00:02:32,000
It's essentially used just so you know when you've reached a leaf it's just something and this is not
25
00:02:32,000 --> 00:02:35,190
something that is unique to red black trees.
26
00:02:35,300 --> 00:02:37,460
Trees use something very similar as well.
27
00:02:37,460 --> 00:02:40,460
So it's You'll come across quite a bit.
28
00:02:40,460 --> 00:02:44,630
Another thing to know is that the root node is always covered black.
29
00:02:44,790 --> 00:02:52,160
There is no path from the root to the leaf that has two consecutive red nodes nodes are inserted in
30
00:02:52,160 --> 00:02:55,780
the same way initially just like a binary search tree.
31
00:02:55,790 --> 00:02:58,260
And we're going to go through that process here in a minute.
32
00:02:58,490 --> 00:03:05,000
And then the tree is fixed by recoloring which we're also going to go through a live animation to see
33
00:03:05,000 --> 00:03:06,280
how this works.
34
00:03:06,320 --> 00:03:13,130
And then when the last properties is at each of the paths from the root to the leaves have the same
35
00:03:13,130 --> 00:03:15,280
number of black nodes.
36
00:03:15,290 --> 00:03:21,700
So now we're going to go and see the way this actually works on a practical basis.
37
00:03:22,040 --> 00:03:30,110
So I'm going to start by inserting a node and I'm going to give it a value of 10 and you'll see that
38
00:03:30,110 --> 00:03:35,510
that node is colored black and by default that is the root.
39
00:03:35,510 --> 00:03:38,900
Now I'm going to give it a 12 or add a 12.
40
00:03:39,110 --> 00:03:47,510
So it looks at this at the root and realizes 12 is larger than 10 and it adds 12 to the right hand side.
41
00:03:47,690 --> 00:03:52,640
If you know anything about binary search trees you know that that's the way they work as well.
42
00:03:52,640 --> 00:03:59,300
Now I'm going to add on 11 and you'll see that when I add the 11 it's going to start by being inserted
43
00:03:59,750 --> 00:04:01,270
to the left of the 12.
44
00:04:01,370 --> 00:04:06,960
But the tree is going to recognize that now it's imbalanced and then it's going to fix itself.
45
00:04:07,010 --> 00:04:15,950
So you'll see it looks at the 10 and then the 12 goes to the left and then realizes hey this imbalance
46
00:04:15,950 --> 00:04:16,940
tree.
47
00:04:17,410 --> 00:04:23,660
So it moves to the left and then it actually readjusts and you can see it makes the 11.
48
00:04:23,660 --> 00:04:25,260
Now the root node.
49
00:04:25,340 --> 00:04:32,060
So this is when now you get to go through the actual algorithm you'll see it's recursive which means
50
00:04:32,060 --> 00:04:41,750
that it looks at every single parent option and then adjust the cell based off of that value and the
51
00:04:41,750 --> 00:04:50,070
whole goal of a red black tree is to continually rebalance itself see the best form tree possible.
52
00:04:50,090 --> 00:04:51,530
So now add.
53
00:04:52,190 --> 00:04:58,570
Start adding some larger numbers and put a 100 in which is going to move all the way to the right.
54
00:04:58,720 --> 00:05:07,060
Now the system knows that both of these are are that 12 and 100 were both red so it readjust because
55
00:05:07,060 --> 00:05:17,590
remember that you can never have a red parent and a red child you have to readjust the coloring to ensure
56
00:05:17,590 --> 00:05:18,310
it works.
57
00:05:18,310 --> 00:05:27,430
So now we'll add a 95 95 will automatically go to the left of 100 but then you'll see as the system
58
00:05:27,430 --> 00:05:33,730
adjusts.
59
00:05:33,910 --> 00:05:35,600
And there you go.
60
00:05:37,090 --> 00:05:38,870
And it readjusts again.
61
00:05:38,920 --> 00:05:47,380
So you notice that this is a very dynamic type system that continually looks to find ways to make sure
62
00:05:47,380 --> 00:05:56,080
that the the tree becomes balanced and it's looking with each iteration on different ways that it can
63
00:05:56,080 --> 00:06:01,650
readjust itself and it uses the rest of the process it is called the recoloring.
64
00:06:01,840 --> 00:06:10,240
And you can see when we add it at that number it got readjusted and we're back to making sure that we
65
00:06:10,240 --> 00:06:16,030
have the black red black type of color scheme.
66
00:06:16,030 --> 00:06:23,330
Now we're going to add 55 55 is going to look at the 11 realize it's greater 95 is less.
67
00:06:23,360 --> 00:06:26,500
It's greater than 12 greater than 25.
68
00:06:26,500 --> 00:06:32,260
So we'll start out by going to the right and then it's going to readjust.
69
00:06:32,410 --> 00:06:33,390
And there you go.
70
00:06:33,400 --> 00:06:34,760
We're balanced again.
71
00:06:34,960 --> 00:06:38,490
And now I'll start adding a few to the left hand side.
72
00:06:38,860 --> 00:06:40,800
So add in the 16.
73
00:06:41,030 --> 00:06:47,290
Six teens going to realize it's greater than 11 so the right hand side.
74
00:06:47,560 --> 00:06:54,560
But it's also greater than 12 and there you go so I'll readjust again
75
00:06:58,630 --> 00:07:01,120
and they're perfect you.
76
00:07:01,510 --> 00:07:09,370
Even though with the traditional just a plain tree that could have been a node that could have rebadge
77
00:07:09,430 --> 00:07:16,630
or unbalance the whole tree with a red black system it will continually continually run through the
78
00:07:16,630 --> 00:07:20,160
algorithm to make sure that it can remain balanced.
79
00:07:20,250 --> 00:07:27,730
I'm going to put in a 19 now and you can see that's going to go all the way to the right of the 16 but
80
00:07:27,730 --> 00:07:33,140
then you can watch and see as it as it readjust.
81
00:07:33,190 --> 00:07:33,480
So
82
00:07:37,690 --> 00:07:38,340
there you go.
83
00:07:38,380 --> 00:07:42,160
So now 16 is the parent of both 12 and 19.
84
00:07:42,160 --> 00:07:43,370
And we could do this for days.
85
00:07:43,370 --> 00:07:48,510
I think you probably have a good sense of the way the system works.
86
00:07:48,620 --> 00:07:53,170
A really important thing to know is that the root node can be changed.
87
00:07:53,230 --> 00:07:59,650
And that's something that you that it took a little while for me to kind of understand the concept of
88
00:07:59,650 --> 00:08:03,880
how the entire make up of the tree can actually change dynamically.
89
00:08:03,880 --> 00:08:08,460
And the only some of the only constants are those properties.
90
00:08:08,470 --> 00:08:13,540
So it's very important especially if you're taking this for an algorithm class or something like that
91
00:08:13,540 --> 00:08:18,910
to memorize those properties and to become very familiar with them because those are really going to
92
00:08:18,910 --> 00:08:24,700
help you understand the way the whole process works and as you analyze the algorithm it will really
93
00:08:24,700 --> 00:08:32,290
help you because whenever you go through recursive algorithms knowing the formal semantics and knowing
94
00:08:32,380 --> 00:08:37,780
the properties behind the system is what's going to help you understand it and analyze it a lot better.
95
00:08:37,780 --> 00:08:47,110
So and once again just to reiterate the complexities if you have a red black tree you're going to see
96
00:08:47,350 --> 00:08:54,580
complexities of order of log in and that's because just like with a traditional binary search tree That's
97
00:08:54,580 --> 00:08:55,510
balance.
98
00:08:55,510 --> 00:09:02,230
You're not going to have to iterate through that many values because you can see even though we may
99
00:09:02,230 --> 00:09:08,620
have given a lot of different random numbers the system continually updated continually rebalanced and
100
00:09:08,620 --> 00:09:13,990
so you're going to end up with those really nice order of log and type of complexity.
101
00:09:13,990 --> 00:09:21,070
So in the next video we're going to go over how to delete or remove a node from a red black tree.
11223
Can't find what you're looking for?
Get subtitles in any language from opensubtitles.com, and translate them here.