text stringlengths 1 81 | start float64 0 10.1k | duration float64 0 24.9 |
|---|---|---|
and the computer's time. | 3,635.03 | 1.7 |
But could we achieve
the beauty of divide | 3,636.73 | 2.91 |
and conquer and binary search from
week zero without the constraints | 3,639.64 | 4.28 |
that arrays impose? | 3,643.92 | 1.31 |
And today, the solution to
all of our array problems | 3,645.23 | 2.61 |
seems to be linked lists or
more generally pointers so | 3,647.84 | 3.33 |
that, one, we can dynamically allocate
more memory with malloc when we need it | 3,651.17 | 3.25 |
and then use pointers to
thread or stitch together | 3,654.42 | 2.88 |
that node with any existing nodes. | 3,657.3 | 2.52 |
So, indeed let me propose this
variant on a tree structure | 3,659.82 | 3.92 |
that the world calls binary
search trees or BSTs. | 3,663.74 | 2.98 |
Binary in this case means two, and this
just means that every node in this tree | 3,666.72 | 4.59 |
is going to have 0, 1,
or 2 maximally children. | 3,671.31 | 4.7 |
And now, in this case binary search tree
means that for every node in the tree | 3,676.01 | 7.35 |
it's left child is less than it and
its right child is greater than it. | 3,683.36 | 5.36 |
And that's a recursive definition. | 3,688.72 | 1.67 |
You can look at the root of this
tree and ask that same question. | 3,690.39 | 3.48 |
55? | 3,693.87 | 0.7 |
Is it greater than its left child? | 3,694.57 | 1.5 |
Yep. | 3,696.07 | 0.54 |
Is it less than its right child? | 3,696.61 | 1.38 |
Yep. | 3,697.99 | 0.83 |
That is the beginning, it would
seem, of a binary search tree. | 3,698.82 | 2.67 |
But it's recursive in so far as
this is indeed a binary search | 3,701.49 | 2.92 |
tree if that statement is true. | 3,704.41 | 2.08 |
Those answers are the same for
every other node in the tree. | 3,706.49 | 2.88 |
33, is its left child smaller? | 3,709.37 | 2.16 |
Yep. | 3,711.53 | 0.5 |
Is its right child bigger? | 3,712.03 | 1.2 |
Yep. | 3,713.23 | 0.53 |
How about over here, 77? | 3,713.76 | 1.16 |
Left child smaller? | 3,714.92 | 0.95 |
Yep. | 3,715.87 | 0.5 |
Right child bigger? | 3,716.37 | 1.16 |
Yep, indeed. | 3,717.53 | 0.95 |
How about the leaves of the tree? | 3,718.48 | 2 |
Is 22 greater than its left child? | 3,720.48 | 3.93 |
I mean, yeah, there is no child,
so yes, that's a fair statement. | 3,724.41 | 3.4 |
It certainly doesn't violate
our guiding principle. | 3,727.81 | 2.66 |
Is it less than its right child, if any? | 3,730.47 | 2.05 |
Yes, there just isn't any. | 3,732.52 | 1.82 |
And so this is a binary search tree. | 3,734.34 | 1.96 |
And indeed, if you took a scissors and
snipped off any branch of this tree, | 3,736.3 | 4.54 |
you would have another binary
search tree, albeit smaller. | 3,740.84 | 3.98 |
But it's recursive and that definition
applies to every one of the nodes. | 3,744.82 | 3.09 |
But what's beautiful here now is that
if we implement this binary search | 3,747.91 | 3.88 |
tree, similar in spirit to how
we implemented linked lists | 3,751.79 | 2.89 |
using not arrays but using pointers
and not one pointer but two pointers | 3,754.68 | 4.34 |
whereby every node in this tree
apparently has up to two pointers-- | 3,759.02 | 4.42 |
let's call them not next but how about
left and right just to be intuitive. | 3,763.44 | 5.03 |
Well, if every node has a
left and a right pointer, | 3,768.47 | 2.83 |
now you can conceptually attach
yourself to another node over there | 3,771.3 | 3.59 |
and another node over there,
and they too can do the same. | 3,774.89 | 3.29 |
So, we have the syntax already with our
pointers with which to implement this. | 3,778.18 | 4.22 |
But why would we? | 3,782.4 | 1.03 |
Well, one, if we're using
pointers now and not an array, | 3,783.43 | 2.8 |
I can very, very easily allocate
more nodes for this tree. | 3,786.23 | 3.97 |
I can insert 99 or 11 really
easily, because I just | 3,790.2 | 4.26 |
called malloc like I did before. | 3,794.46 | 1.82 |
I put the number 99 or
11 inside of that node, | 3,796.28 | 2.76 |
and then I start from
the root of the tree, | 3,799.04 | 1.88 |
much like I start from the first
element in the linked list, | 3,800.92 | 2.78 |
and I just search for its destined
location going left or right | 3,803.7 | 3.81 |
based on the size of that value. | 3,807.51 | 1.62 |
And what's nice, too, here is
notice how short the tree is. | 3,809.13 | 3.85 |
This is not a linked list. | 3,812.98 | 1.17 |
It's not a long list, whether
vertically or horizontally. | 3,814.15 | 3.5 |
This is very shallow this tree. | 3,817.65 | 2.93 |
And indeed I claim that if we've
got n elements in this list, | 3,820.58 | 4.33 |
the height of this tree
it turns out is log of n. | 3,824.91 | 4.04 |
So, the height of this tree is
log of n, give or take one or so. | 3,828.95 | 4.86 |
But that's compelling, because
how do I search this tree? | 3,833.81 | 3.14 |
Suppose I am asked-- I'm trying to
answer the question is 44 on my list? | 3,836.95 | 3.855 |
How do I answer that? | 3,840.805 | 0.875 |
Well, we humans can obviously just look
back and it's like, yes, 44 is in it. | 3,841.68 | 2.97 |
It's not how a computer works. | 3,844.65 | 1.17 |
We have to start from what
we're given, which in this case | 3,845.82 | 2.416 |
is going to be as the arrow
suggests a pointer to the tree | 3,848.236 | 2.864 |
itself, a pointer towards first node. | 3,851.1 | 1.77 |
And I look is this the number 44? | 3,852.87 | 2.36 |
Obviously not. | 3,855.23 | 1.36 |
55 is greater than 44, so I'm
going to go down to the left child | 3,856.59 | 3.835 |
and ask that same question. | 3,860.425 | 1.125 |
33, is this 44? | 3,861.55 | 1.19 |
Obviously not, but it's
less than it so I'm | 3,862.74 | 2.45 |
going to go down to the right child. | 3,865.19 | 1.56 |
Is this 44? | 3,866.75 | 0.98 |
Yes, and simply by
looking at three nodes | 3,867.73 | 4.88 |
have I whittled this problem
down to my yes no answer. | 3,872.61 | 3.96 |
And indeed, you can think
of it again with scissors. | 3,876.57 | 2.49 |
I'm looking at 55 at the
beginning of this story. | 3,879.06 | 2.61 |
Is 44 55? | 3,881.67 | 1.07 |
No, 44 is less. | 3,882.74 | 1.12 |
Well, you know what? | 3,883.86 | 1.21 |
I can effectively snip off
the right half of that tree, | 3,885.07 | 4.29 |
much like I tore that phone book in
week zero, throwing half of the problem | 3,889.36 | 3.55 |
away. | 3,892.91 | 0.5 |
Here I can throw essentially half of the
tree away and search only what remains | 3,893.41 | 4.41 |
and then repeat that process again,
and again, and again, whittling | 3,897.82 | 3.2 |
the tree down by half every time. | 3,901.02 | 2.3 |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.