Would you like to inspect the original subtitles? These are the user uploaded subtitles that are being translated:
1
00:00:09,270 --> 00:00:14,390
In some past videos I've talked about binary search trees and how to put them together.
2
00:00:14,460 --> 00:00:21,170
However I took a data set that was very well structured and so the tree was nice and balanced.
3
00:00:21,180 --> 00:00:28,410
And then in some of our other videos we went over red black trees and how to balance a tree that wasn't
4
00:00:29,310 --> 00:00:34,260
really structured the way you'd want a red black tree to be structured in this video.
5
00:00:34,260 --> 00:00:42,030
I'm going to show you how you can take an array and how you create or construct a binary search three
6
00:00:42,420 --> 00:00:48,020
tree based on the elements of that array in their original order.
7
00:00:48,030 --> 00:00:57,920
So you can see on the right hand side here we have an array with elements 5 7 1 15 9 2 14 8 7 and 3.
8
00:00:58,110 --> 00:01:05,460
And so if we're going to create a binary search tree with this array they have to actually go in order
9
00:01:05,460 --> 00:01:07,260
and show you how that works.
10
00:01:07,260 --> 00:01:10,830
So the first element in our array is going to be 5.
11
00:01:10,890 --> 00:01:18,430
And so it's an ICC we create 5 and put it inside of the at the root node.
12
00:01:18,720 --> 00:01:21,590
The next one we're going to use is going to be 7.
13
00:01:21,690 --> 00:01:27,240
And because seven is greater than 5 we'll do that comparison and then you'll put 7 on the right hand
14
00:01:27,240 --> 00:01:29,360
side right next to 5.
15
00:01:29,400 --> 00:01:32,360
After that we have a one.
16
00:01:32,750 --> 00:01:35,240
One is less than five.
17
00:01:35,280 --> 00:01:40,410
So it goes left and right here we have a very nice basic tree structure.
18
00:01:40,500 --> 00:01:42,650
We have our root node.
19
00:01:42,810 --> 00:01:47,310
We have one bean the item or the node to the left seven to the right.
20
00:01:47,310 --> 00:01:49,770
So far everything is normal.
21
00:01:49,770 --> 00:01:54,730
Now we're going to add 15 15 is greater than 5.
22
00:01:54,780 --> 00:01:59,090
It's greater than 7 so it needs to go all the way to the right.
23
00:01:59,100 --> 00:02:02,870
Now I'm just going to start adding some more in order in the array.
24
00:02:02,880 --> 00:02:09,100
So we go 5 7 15 now nine is less than 15.
25
00:02:09,210 --> 00:02:11,280
And so we put it there on the left.
26
00:02:11,280 --> 00:02:18,120
Now if you are familiar with red black trees or some of these more self-balancing trees you would know
27
00:02:18,120 --> 00:02:25,950
that this is not the way this would have been constructed it would have rebalanced itself and would
28
00:02:25,950 --> 00:02:32,280
have swapped out the nine right here so the nine would be connected to the five the seven would be on
29
00:02:32,280 --> 00:02:34,860
the left hand side 15 on the right hand side.
30
00:02:34,860 --> 00:02:38,220
However this is just a regular binary search tree.
31
00:02:38,280 --> 00:02:42,780
So we have to treat it as such and we have to add the elements in order.
32
00:02:42,780 --> 00:02:49,200
And this is the reason why people created things like red black trees was because as you'll see here
33
00:02:49,200 --> 00:02:54,230
in a minute this tree is going to look pretty odd in a short period of time.
34
00:02:54,270 --> 00:02:59,830
So next element is to use less than 5 so we know it goes the left.
35
00:02:59,850 --> 00:03:01,090
It's greater than 1.
36
00:03:01,140 --> 00:03:09,140
So you get pulled over into the right and we're going to add 14 is greater than five greater than seven.
37
00:03:09,270 --> 00:03:14,280
But less than 15 so it's going to move in the left and it's greater than 9.
38
00:03:14,310 --> 00:03:20,780
So you'll see that moves all the way to the bottom of our binary search tree.
39
00:03:20,790 --> 00:03:23,700
So at 14 we have just three elements left.
40
00:03:23,760 --> 00:03:32,190
Eight it's going to traverse the tree we see that it's very than seven but less than 15 and less than
41
00:03:32,190 --> 00:03:33,230
nine.
42
00:03:33,900 --> 00:03:37,810
And our last or second last one is seven.
43
00:03:37,890 --> 00:03:44,010
Now that seven is an interesting one because you can see that it is greater than five it's actually
44
00:03:44,070 --> 00:03:48,010
equal than seven but because it's equal to seven it's not less than seven.
45
00:03:48,120 --> 00:03:49,980
It gets moved.
46
00:03:49,980 --> 00:03:51,340
It goes to the right.
47
00:03:51,360 --> 00:03:53,020
It's less than 15.
48
00:03:53,130 --> 00:03:54,380
Less than nine.
49
00:03:54,420 --> 00:03:55,430
Less than eight.
50
00:03:55,560 --> 00:03:57,470
And it goes right here.
51
00:03:57,480 --> 00:04:07,500
So even though these two nodes are actually the same value one is directly descendant of the root node
52
00:04:07,800 --> 00:04:09,790
and the other one is all the way here.
53
00:04:09,810 --> 00:04:18,240
Now it is the closest node to the 7 because they are equal and so it's neat to see the way that that
54
00:04:18,300 --> 00:04:25,020
automatically happens but it's still pretty important to see that you can have a tree that is structured
55
00:04:25,020 --> 00:04:25,720
like this.
56
00:04:25,740 --> 00:04:33,600
If you take everything in order and last one we're going to do is a 3 and that's greater than 1 greater
57
00:04:33,600 --> 00:04:34,540
than 2.
58
00:04:34,710 --> 00:04:36,140
So it gets pooled here.
59
00:04:36,180 --> 00:04:42,900
And so given this data structure here on the right and side this is what a visual representation of
60
00:04:42,930 --> 00:04:44,980
the tree would look like.
61
00:04:45,000 --> 00:04:47,120
So you can see it's not balance.
62
00:04:47,160 --> 00:04:49,940
However it is properly structured.
63
00:04:49,980 --> 00:04:52,690
If you were to run a search on this.
64
00:04:52,800 --> 00:04:56,020
So say that we want to define eight.
65
00:04:56,370 --> 00:05:01,940
So if we want to find 8 they will go to 5 7 15.
66
00:05:01,950 --> 00:05:06,060
It's less than 15 so it knows and look to the left is less than nine.
67
00:05:06,510 --> 00:05:09,240
And there it is found the element.
68
00:05:09,240 --> 00:05:15,160
So this is how you construct a binary search tree given in an array.
6820
Can't find what you're looking for?
Get subtitles in any language from opensubtitles.com, and translate them here.