Would you like to inspect the original subtitles? These are the user uploaded subtitles that are being translated:
1
00:00:09,620 --> 00:00:16,960
High in this video we're going to go through how to delete notes from a red black tree.
2
00:00:16,970 --> 00:00:22,250
If you watch the previous video on the properties of red black trees and how to insert that would be
3
00:00:22,990 --> 00:00:29,990
a good basis for understanding how to remove these nodes because it's very important to understand the
4
00:00:29,990 --> 00:00:36,020
properties of the red black tree because if you don't know that then you're not going to know what steps
5
00:00:36,020 --> 00:00:40,840
we need to take in order to rebalance after a node has been deleted.
6
00:00:40,940 --> 00:00:48,720
And so I'm going to go through some exercises here and we can see exactly how the process works on an
7
00:00:48,770 --> 00:00:49,980
animated basis.
8
00:00:50,040 --> 00:00:54,460
So first I'm going to show you what happens when we delete the root node.
9
00:00:54,470 --> 00:00:56,720
So you can see that root node is 16.
10
00:00:56,720 --> 00:01:05,570
And so when we delete this you can see it looks at 16 and then it traverses the tree all the way down
11
00:01:05,570 --> 00:01:16,820
to 15 and then readjusts and now it makes 14 be connected to a node leaf and then it readjusts at all
12
00:01:16,820 --> 00:01:24,540
so that it restores the balance and brings that 12 up so now 12 points to 10 which is less than 12 and
13
00:01:24,620 --> 00:01:25,430
14.
14
00:01:25,460 --> 00:01:28,730
And you can see how that automatically balances.
15
00:01:28,730 --> 00:01:36,290
Now if we want to remove 21 which you can see is the parent to 18 and 25 you can probably guess how
16
00:01:36,290 --> 00:01:44,630
it's going to work but it's going to start at the root then iterate through and find 21.
17
00:01:44,960 --> 00:01:47,930
It's going to swap places with 18 and 17.
18
00:01:47,960 --> 00:01:56,540
And now you know that you have to swap out the colors because you can't have any red nodes connected
19
00:01:56,540 --> 00:02:00,960
to any other red nodes and so that was a pretty easy one right there.
20
00:02:00,980 --> 00:02:03,610
And now let's take 55 out.
21
00:02:03,950 --> 00:02:05,470
So we'll delete 55.
22
00:02:05,480 --> 00:02:15,530
It goes to 15 then it finds 55 and it goes down the list to find the highest value closest to it.
23
00:02:15,530 --> 00:02:17,300
It swaps that out.
24
00:02:17,300 --> 00:02:23,780
You can't see it in animation but it you know puts a no leaf connected to 25.
25
00:02:23,790 --> 00:02:32,390
And so you can see with each deletion the tree automatically rebalances and a user uses a recursive
26
00:02:32,720 --> 00:02:35,080
algorithm to make that possible.
27
00:02:35,090 --> 00:02:41,500
And so now we're going to take down 95 and you'll see 95 is a red Knodell all the way are almost all
28
00:02:41,510 --> 00:02:42,640
the way the right.
29
00:02:42,650 --> 00:02:44,690
So let's go look at ninety five.
30
00:02:44,720 --> 00:02:45,460
Then it's go.
31
00:02:45,470 --> 00:02:47,160
Compare that with 66.
32
00:02:47,210 --> 00:02:51,110
Swap out those values and readjust itself.
33
00:02:54,140 --> 00:02:59,840
And see we'll just do two more we'll take out the root note again and see how that works.
34
00:02:59,840 --> 00:03:05,570
It's going to do the same thing it did before where it looks at 15 goes to the left side of the tree
35
00:03:05,570 --> 00:03:07,200
swaps it with 14.
36
00:03:07,340 --> 00:03:14,330
And now we're going to have 14 12 and 10 and it's going to swap out 10 so that's a red note.
37
00:03:14,600 --> 00:03:21,320
And now because we wouldn't want to have a tree just with a single line like that you can see the entire
38
00:03:21,320 --> 00:03:29,650
tree gets completely reassembled 18 becomes the new root node and you have a much more balanced tree.
39
00:03:29,780 --> 00:03:36,100
If you noticed you were going to have 14 12 and 10 all in the line together.
40
00:03:36,200 --> 00:03:42,710
And that's definitely not you know those by themselves with 14 being a root would not be considered
41
00:03:42,740 --> 00:03:44,150
a well-balanced tree.
42
00:03:44,150 --> 00:03:50,470
So that's why the entire tree did a switch when the recoloring process happened.
43
00:03:50,660 --> 00:03:58,610
And so that's that's essentially the whole key to understanding deleting from a red black tree is simply
44
00:03:58,610 --> 00:04:01,950
to understand the properties of that tree.
45
00:04:01,970 --> 00:04:04,990
You have to know that the black nodes.
46
00:04:05,690 --> 00:04:11,420
There is always going to be a black node at the root no black or I'm sorry no red node is ever going
47
00:04:11,420 --> 00:04:13,580
to have any black children.
48
00:04:13,760 --> 00:04:21,500
And then also just understand that the whole goal of the red black tree is to continually bring balance.
49
00:04:21,500 --> 00:04:29,120
And so when you are deleting any items especially take 18 for example we've done it a few times in this
50
00:04:29,120 --> 00:04:35,570
exercise so you should probably know if I was good to lead 18 it would go to 14 to 17 and then it would
51
00:04:35,570 --> 00:04:38,470
swap the value of 17 to 18.
52
00:04:38,600 --> 00:04:48,380
It would give a pointer to a null value which is also called a sentinel value to 14 and then the entire
53
00:04:48,380 --> 00:04:50,270
tree would merge over again.
54
00:04:50,360 --> 00:04:51,650
And so we can go through that.
55
00:04:51,670 --> 00:04:53,910
This last one so you can see that.
56
00:04:53,960 --> 00:05:06,610
So do 18 delete 18 goes down to 14 to 17 swaps that value out and then it does the single rotate.
57
00:05:06,610 --> 00:05:07,190
Right.
58
00:05:07,360 --> 00:05:08,070
And there you go.
59
00:05:08,080 --> 00:05:09,140
We're rebalanced.
60
00:05:09,190 --> 00:05:14,100
So good job you now know how to delete nodes from a red black tree.
61
00:05:14,110 --> 00:05:16,870
Please let me know if you have any questions whatsoever.
62
00:05:16,870 --> 00:05:24,060
And in our final video for red black trees we're going to go over how to find elements in the tree.
6608
Can't find what you're looking for?
Get subtitles in any language from opensubtitles.com, and translate them here.