All language subtitles for 12 - Searching Through a Binary Search Tree English

af Afrikaans
sq Albanian
am Amharic
ar Arabic Download
hy Armenian
az Azerbaijani
eu Basque
be Belarusian
bn Bengali
bs Bosnian
bg Bulgarian
ca Catalan
ceb Cebuano
ny Chichewa
zh-CN Chinese (Simplified)
zh-TW Chinese (Traditional)
co Corsican
hr Croatian
cs Czech
da Danish
nl Dutch
en English
eo Esperanto
et Estonian
tl Filipino
fi Finnish
fr French
fy Frisian
gl Galician
ka Georgian
de German
el Greek
gu Gujarati
ht Haitian Creole
ha Hausa
haw Hawaiian
iw Hebrew
hi Hindi
hmn Hmong
hu Hungarian
is Icelandic
ig Igbo
id Indonesian
ga Irish
it Italian
ja Japanese
jw Javanese
kn Kannada
kk Kazakh
km Khmer
ko Korean
ku Kurdish (Kurmanji)
ky Kyrgyz
lo Lao
la Latin
lv Latvian
lt Lithuanian
lb Luxembourgish
mk Macedonian
mg Malagasy
ms Malay
ml Malayalam
mt Maltese
mi Maori
mr Marathi
mn Mongolian
my Myanmar (Burmese)
ne Nepali
no Norwegian
ps Pashto
fa Persian
pl Polish
pt Portuguese
pa Punjabi
ro Romanian
ru Russian
sm Samoan
gd Scots Gaelic
sr Serbian
st Sesotho
sn Shona
sd Sindhi
si Sinhala
sk Slovak
sl Slovenian
so Somali
es Spanish
su Sundanese
sw Swahili
sv Swedish
tg Tajik
ta Tamil
te Telugu
th Thai
tr Turkish
uk Ukrainian
ur Urdu
uz Uzbek
vi Vietnamese
cy Welsh
xh Xhosa
yi Yiddish
yo Yoruba
zu Zulu
or Odia (Oriya)
rw Kinyarwanda
tk Turkmen
tt Tatar
ug Uyghur
Would you like to inspect the original subtitles? These are the user uploaded subtitles that are being translated: 1 00:00:09,170 --> 00:00:14,320 ‫In this tutorial we're going to walk through how binary search actually works. 2 00:00:14,320 --> 00:00:18,540 ‫And before we get into that we need something to compare it to. 3 00:00:18,550 --> 00:00:24,520 ‫And so what we're going to compare it to is something called linear search on the page right now. 4 00:00:24,520 --> 00:00:31,410 ‫We have a standard array and its array of integers going from 1 to 25. 5 00:00:31,420 --> 00:00:36,670 ‫The order doesn't matter at all because all we're concerned with right now is searching for an item. 6 00:00:36,670 --> 00:00:40,090 ‫So let's do a sample search. 7 00:00:40,090 --> 00:00:47,750 ‫The first thing we're going to search for is 15 and we're going to see how many times we have to iterate 8 00:00:47,750 --> 00:00:54,480 ‫through the array which means to go through each element in the array before we get to 15. 9 00:00:54,530 --> 00:01:00,060 ‫So we start out at the zero element which actually is one is the value. 10 00:01:00,230 --> 00:01:06,620 ‫And so we go one two three four five six. 11 00:01:06,650 --> 00:01:15,370 ‫So we actually have six things that we have to go through in order to get to that 15 value. 12 00:01:15,390 --> 00:01:19,080 ‫Now if we want to go to 25 13 00:01:23,550 --> 00:01:37,380 ‫in this case we would go one two three four five six seven eight nine 10 and 11. 14 00:01:37,480 --> 00:01:42,970 ‫And what if there was nothing the element we were looking for wasn't even in the array. 15 00:01:43,000 --> 00:01:45,920 ‫So say we had ninety nine. 16 00:01:46,120 --> 00:01:54,040 ‫It would be what we call and there would be in you know which is similar to 25 except in this case it's 17 00:01:54,040 --> 00:01:59,810 ‫considered the worst case time complexity because the element isn't even in the array which means we 18 00:01:59,810 --> 00:02:05,780 ‫would go through the entire array without finding an element and then returning that we couldn't find 19 00:02:05,780 --> 00:02:05,960 ‫it. 20 00:02:05,980 --> 00:02:12,490 ‫So this is not a good way to search because right here we only have 11 elements. 21 00:02:12,670 --> 00:02:19,600 ‫What would happen if we had a thousand elements or a million elements or 10 million elements. 22 00:02:19,660 --> 00:02:27,880 ‫If you think about when you're using Google it's not going through every single site on the web to find 23 00:02:28,180 --> 00:02:29,760 ‫what you're looking for. 24 00:02:29,760 --> 00:02:33,140 ‫It has an algorithm that makes it a much much more efficient. 25 00:02:33,220 --> 00:02:38,620 ‫And what we're going to look at is not exactly what Google uses but we're going to look at a way to 26 00:02:38,620 --> 00:02:41,800 ‫make searching a lot more efficient than this. 27 00:02:42,010 --> 00:02:48,940 ‫If you're taking this for an algorithm class you'd one know the worst case time complexity is going 28 00:02:48,940 --> 00:02:57,230 ‫to be big go of and for a linear search through an array like this. 29 00:02:57,280 --> 00:03:01,210 ‫And if you're not taking it for our algorithm classes you know what it's talking about. 30 00:03:01,210 --> 00:03:03,480 ‫Don't worry it's just called Big O notation. 31 00:03:04,690 --> 00:03:06,930 ‫So we're going to do the same thing here. 32 00:03:06,950 --> 00:03:14,980 ‫We're going to go 15 25 and the 33 00:03:19,680 --> 00:03:28,360 ‫you get now to know how search works on a binary search tree it's very important to know the structure. 34 00:03:28,360 --> 00:03:34,810 ‫So if you've not watched my previous video on showing how to set up a binary search tree you may want 35 00:03:34,810 --> 00:03:35,530 ‫to go watch it. 36 00:03:35,560 --> 00:03:40,710 ‫If not just follow along and it is pretty intuitive and easy to pick up. 37 00:03:40,720 --> 00:03:47,530 ‫So we're going to first do our very first search which is 15. 38 00:03:47,530 --> 00:04:00,650 ‫In this case 15 is the root node which means that our this only took one iteration we only had to search 39 00:04:00,650 --> 00:04:04,190 ‫through one item we found it and then we returned. 40 00:04:04,220 --> 00:04:06,290 ‫That's not always the case. 41 00:04:06,290 --> 00:04:11,570 ‫In fact it's very rarely going to be the case the item you're looking for is actually at the root. 42 00:04:11,570 --> 00:04:14,540 ‫But when it is that's what it's going to return. 43 00:04:14,550 --> 00:04:18,330 ‫Now let's look at what it would be if it was 25. 44 00:04:18,530 --> 00:04:24,030 ‫If you remember last time it took us 11 steps to get to 25 here. 45 00:04:24,050 --> 00:04:25,770 ‫Let's see how many steps it's going to be. 46 00:04:25,970 --> 00:04:27,490 ‫So we start at 15. 47 00:04:27,710 --> 00:04:31,880 ‫That's one then we realize that 25 is bigger than 15. 48 00:04:31,970 --> 00:04:34,300 ‫So we go to the right hand side. 49 00:04:34,820 --> 00:04:40,200 ‫So here we see okay we compares 25 bigger or smaller than 20. 50 00:04:40,220 --> 00:04:41,020 ‫We know it's bigger. 51 00:04:41,030 --> 00:04:47,460 ‫So we look at the right note again so so far this is we've taken two steps. 52 00:04:47,460 --> 00:04:48,860 ‫Now we're at 22. 53 00:04:48,870 --> 00:04:58,050 ‫Once again we compares 25 bigger than 22 or is it less than we know it's bigger and we arrive at 25. 54 00:04:58,230 --> 00:05:04,260 ‫That took us a total of one two three three steps until we got to it. 55 00:05:04,500 --> 00:05:09,030 ‫And if you want to include that last one it took four steps to get to it. 56 00:05:09,050 --> 00:05:17,700 ‫Ninety nine would actually be the same number it would only take us four steps because we would go from 57 00:05:17,730 --> 00:05:21,450 ‫15 to 20 20 to 25. 58 00:05:21,450 --> 00:05:26,240 ‫We'd realize that 25 is the leaf and there is nothing greater than 25. 59 00:05:26,340 --> 00:05:33,090 ‫And the system would return that 20 the 99 was not in the binary tree. 60 00:05:33,130 --> 00:05:36,890 ‫So you can see right there and compare these two. 61 00:05:36,900 --> 00:05:47,150 ‫This is dramatically faster than than having to go linearly and go through every single item. 62 00:05:47,160 --> 00:05:54,270 ‫It's very rare that you would actually find something using a linear search faster than using a binary 63 00:05:54,270 --> 00:05:55,400 ‫search tree. 64 00:05:55,650 --> 00:06:04,970 ‫Mainly because it's very rare you'll find your element on the second or third second or third iteration. 65 00:06:05,160 --> 00:06:12,870 ‫But the way to think about this is what really helped me understand this at least was being able to 66 00:06:14,490 --> 00:06:17,890 ‫think of opening up a phone book. 67 00:06:18,330 --> 00:06:26,120 ‫And when you have a phone book say that you start and you're using the white pages for example it's 68 00:06:26,130 --> 00:06:36,930 ‫sorted by last name and you have somebody that Star last name you're looking for starts with an M. you're 69 00:06:36,930 --> 00:06:41,240 ‫going to open it up somewhere in the middle doesn't matter where you look it up. 70 00:06:41,430 --> 00:06:48,330 ‫And then from there you're going to compare them or you could compare whatever page you opened up. 71 00:06:48,330 --> 00:06:56,070 ‫So let's say you opened it up and you open it up at the letter D and you know that you need to get to 72 00:06:56,070 --> 00:06:56,690 ‫em. 73 00:06:56,730 --> 00:07:09,200 ‫So you have a goal of getting to them now you look at the U.S. that is less than or greater than them. 74 00:07:09,270 --> 00:07:12,390 ‫We know it's less than them alphabetically. 75 00:07:12,480 --> 00:07:14,220 ‫So what do we do. 76 00:07:14,220 --> 00:07:23,880 ‫That means that we would go to the right and we would open that up again and then we would keep on doing 77 00:07:23,880 --> 00:07:28,670 ‫this over and over again until we find them. 78 00:07:28,770 --> 00:07:32,130 ‫And so it's a very similar thing with the binary search tree. 79 00:07:32,130 --> 00:07:34,480 ‫You take the number you're trying to find. 80 00:07:34,620 --> 00:07:45,120 ‫You go through every different stage every different level of the tree and you look either to the right 81 00:07:45,240 --> 00:07:49,120 ‫or to the left and then you make your decision based off that. 82 00:07:49,170 --> 00:07:52,420 ‫So just to practice it one more time. 83 00:07:52,830 --> 00:07:56,270 ‫This time we'll look for the number 21. 84 00:07:56,400 --> 00:08:01,270 ‫So we started 15 is 21 less than or greater than 15. 85 00:08:01,290 --> 00:08:03,680 ‫It's greater than we go to 20. 86 00:08:03,840 --> 00:08:05,780 ‫Now at 20 we do the same thing. 87 00:08:05,790 --> 00:08:14,370 ‫The comparison says that 21 is greater so we go down again and we have 22 there with 22. 88 00:08:14,460 --> 00:08:24,810 ‫When we compare coin one is less than 22 and we find it and it only took us four steps which is incredibly 89 00:08:24,810 --> 00:08:25,510 ‫fast. 90 00:08:25,530 --> 00:08:30,570 ‫Now the average time complexity is big. 91 00:08:30,570 --> 00:08:38,130 ‫So of blog base to event. 92 00:08:38,400 --> 00:08:44,480 ‫And so this is extremely fast if you're familiar with the principles logarithms. 93 00:08:44,520 --> 00:08:47,310 ‫Logarithms are kind of like exponents except backwards. 94 00:08:47,320 --> 00:08:56,900 ‫So if the other search was was N log in means it's dramatically faster. 95 00:08:56,900 --> 00:09:01,400 ‫That's on the average case which is very important to note. 96 00:09:01,560 --> 00:09:02,870 ‫This is an average case. 97 00:09:03,030 --> 00:09:09,570 ‫What makes something an average face when it comes to a binary search tree has a lot to do with how 98 00:09:09,630 --> 00:09:11,500 ‫balanced the tree is. 99 00:09:11,520 --> 00:09:14,820 ‫So that is something to remember. 100 00:09:14,970 --> 00:09:18,810 ‫You want to have your binary search trees balanced. 101 00:09:19,230 --> 00:09:26,460 ‫Very important the key to this is something I'm going to show you a binary search tree can actually 102 00:09:26,790 --> 00:09:31,280 ‫equal the same complexity as a regular linear search. 103 00:09:31,320 --> 00:09:33,200 ‫And do you know how that can happen. 104 00:09:33,210 --> 00:09:35,560 ‫It's actually relatively intuitive. 105 00:09:35,610 --> 00:09:37,110 ‫Here you can see are nodes. 106 00:09:37,100 --> 00:09:41,760 ‫You have the root and you can see this tree is actually completely symmetrical. 107 00:09:41,790 --> 00:09:46,890 ‫It's very rare to have it exactly like this but it's nice when it is because it makes searching through 108 00:09:46,890 --> 00:09:48,880 ‫it incredibly fast. 109 00:09:48,880 --> 00:09:59,200 ‫Now on this page you will see this is a horribly balanced tree. 110 00:09:59,330 --> 00:10:05,480 ‫There essentially is no tree and you will run into cases where this happens. 111 00:10:05,480 --> 00:10:11,270 ‫And so what you're going to have to deal with here when it comes to searching you're going to have a 112 00:10:11,300 --> 00:10:19,130 ‫complexity of big-O event which is actually identical to searching using a linear search method. 113 00:10:19,190 --> 00:10:22,780 ‫And the reason for that is this is actually the root node. 114 00:10:22,790 --> 00:10:24,190 ‫I know it doesn't even look like a tree. 115 00:10:24,200 --> 00:10:27,210 ‫So it's hard to see however. 116 00:10:27,770 --> 00:10:29,100 ‫This is the root. 117 00:10:29,210 --> 00:10:35,140 ‫So if we have a number we can simply say the number is 1. 118 00:10:35,180 --> 00:10:44,120 ‫We actually have to go through each element and it's the last element in the search tree that has that 119 00:10:44,120 --> 00:10:44,570 ‫total. 120 00:10:44,570 --> 00:10:51,860 ‫So this makes searching really kind of pointless or using this kind of data structure pointless if this 121 00:10:51,860 --> 00:10:54,090 ‫is what your tree looks like. 122 00:10:54,200 --> 00:11:00,050 ‫It works out much better if you have a well-balanced tree and there's some tutorials I'm going to create 123 00:11:00,440 --> 00:11:06,170 ‫that will talk about how you can balance your tree out if you happen to have something that looks like 124 00:11:06,200 --> 00:11:07,350 ‫this. 125 00:11:07,560 --> 00:11:14,000 ‫But this is this is where usually can have a way that you can randomize and balance out your tree so 126 00:11:14,000 --> 00:11:19,940 ‫that you can get back to those log in type of search complexities. 127 00:11:19,940 --> 00:11:27,790 ‫But it is important to know that the worst case performance of a binary search tree can be as bad as 128 00:11:27,800 --> 00:11:28,580 ‫big event. 129 00:11:28,640 --> 00:11:32,330 ‫So it's important to just always keep that in the back of your mind. 130 00:11:32,340 --> 00:11:37,010 ‫But that's a tutorial please let me know if you have any questions on this whatsoever. 131 00:11:37,040 --> 00:11:45,980 ‫And after this we're going to start learning how to insert and delete nodes off of a binary search tree. 13842

Can't find what you're looking for?
Get subtitles in any language from opensubtitles.com, and translate them here.