Would you like to inspect the original subtitles? These are the user uploaded subtitles that are being translated:
1
00:00:08,930 --> 00:00:15,800
So far we've talked quite a bit about how to construct binary search trees how to search through them
2
00:00:15,800 --> 00:00:21,040
how to traverse them and different facets like that.
3
00:00:21,050 --> 00:00:27,350
Now what we're going to do is talk about one of the more challenging things in learning about how binary
4
00:00:27,350 --> 00:00:32,430
search trees work and that's how to delete a node from the tree.
5
00:00:32,600 --> 00:00:38,520
So the very first thing I'm going to do is show how it works when you delete the root node.
6
00:00:38,540 --> 00:00:40,300
So we have a root node of 5.
7
00:00:40,430 --> 00:00:44,130
So when we delete that we're gonna take 5 out.
8
00:00:44,150 --> 00:00:48,180
But before we can do that we need to find what we're going to put in first.
9
00:00:48,190 --> 00:00:57,440
And so when you take the root node out you traverse down the left hand side of the tree and you find
10
00:00:57,440 --> 00:01:00,460
whatever the highest of value is.
11
00:01:00,530 --> 00:01:08,000
So say Just so you can see that again if we want to take three out again or take the root node out again
12
00:01:08,030 --> 00:01:10,030
take three hit delete.
13
00:01:11,330 --> 00:01:15,290
You'll see that we go down and we swap it out with two.
14
00:01:15,350 --> 00:01:18,080
And so two is now the root node.
15
00:01:18,260 --> 00:01:26,060
So another one that we can show how how the tree would tend to get reordered is low key as you want
16
00:01:26,060 --> 00:01:32,610
to make sure that you maintain the not the proper balance because this is a balancing tree.
17
00:01:32,660 --> 00:01:38,820
Just make sure that you maintain all the correct properties of a binary search tree.
18
00:01:39,080 --> 00:01:40,910
If we want to take 15 now
19
00:01:44,210 --> 00:01:53,480
we're going to first find 15 that's the first part of the algorithm and then from there because it only
20
00:01:53,480 --> 00:02:01,730
has one child you can just take it out and move the pointer right to whatever its child is.
21
00:02:01,730 --> 00:02:03,640
That's a very easy delete.
22
00:02:03,650 --> 00:02:09,680
Now if we want to delete nine this is going to be slightly more complex because you can see Nine has
23
00:02:10,550 --> 00:02:12,720
a child and 14 as a child.
24
00:02:12,740 --> 00:02:23,270
So when we say we want to take out nine we go down the tree find nine and then we swap it out with eight
25
00:02:23,510 --> 00:02:29,120
because eight is greater than seven but it's less than 14.
26
00:02:29,240 --> 00:02:34,970
So we are able to keep all of the same properties of a binary search tree.
27
00:02:35,090 --> 00:02:44,210
The very easiest thing that you can do when removing an item from a tree is to remove one of the leaf
28
00:02:44,240 --> 00:02:46,850
nodes so we could take 14.
29
00:02:47,070 --> 00:02:55,160
We take 14 or we have to do is traverse down the tree find 14 and then simply remove it.
30
00:02:55,160 --> 00:03:01,010
If you were doing this in code you'd have to remove the pointer from 8 to 14 but when you're just doing
31
00:03:01,010 --> 00:03:04,610
it visually like this you essentially can just pop it right off.
32
00:03:04,640 --> 00:03:07,580
So that's all you have to do.
33
00:03:07,580 --> 00:03:18,020
I showed you how to remove the root node and you know that in order to do that you find whatever value
34
00:03:18,020 --> 00:03:20,440
is highest on the left hand side.
35
00:03:20,600 --> 00:03:29,180
You swap those values and then you point that value to these other nodes it becomes the parent and then
36
00:03:29,230 --> 00:03:32,390
becomes a parent of both sides.
37
00:03:32,390 --> 00:03:39,740
And that's when you need to swap out the root node when you want to delete an item that only has one
38
00:03:39,740 --> 00:03:40,820
child.
39
00:03:40,820 --> 00:03:45,520
You can simply remove it and point directly to that child.
40
00:03:45,590 --> 00:03:54,050
And then if you want to delete a node that has two children you go down to the left hand side and you
41
00:03:54,050 --> 00:04:01,160
swap places with that one and then that has a pointer to the next one and then the last if you want
42
00:04:01,160 --> 00:04:06,260
to remove the leaf then you simply pop it off and that's all you have to do.
43
00:04:06,260 --> 00:04:08,480
So please let me know if you have any questions.
44
00:04:08,510 --> 00:04:15,450
If that doesn't make any sense to you the first time you see it watch the video over once or twice.
45
00:04:15,500 --> 00:04:21,560
It took me a while to become familiar with how binary search tree deletions worked.
46
00:04:21,560 --> 00:04:27,140
It's not the most intuitive thing in the very beginning but the more you see it happen and the more
47
00:04:27,140 --> 00:04:29,130
you play around with it the more it makes sense.
48
00:04:29,140 --> 00:04:33,470
So please let me know if you have any questions whatsoever in the comments section and I'll see in the
49
00:04:33,470 --> 00:04:34,480
next video.
5447
Can't find what you're looking for?
Get subtitles in any language from opensubtitles.com, and translate them here.