text stringlengths 1 81 | start float64 0 10.1k | duration float64 0 24.9 |
|---|---|---|
in a linked list or the
first thing in a tree. | 4,194.029 | 1.901 |
And in this case, we would
call that first thing | 4,195.93 | 1.999 |
in a tree a root, generally speaking. | 4,197.929 | 2.521 |
So, the first thing I had
better do in my search function | 4,200.45 | 2.78 |
is check, wait a minute. | 4,203.23 | 1.16 |
If tree equals equals
null, don't do anything. | 4,204.39 | 3.88 |
Do not risk touching any pointers,
because as you may have gleaned already | 4,208.27 | 3.95 |
or soon will with some
of CS50's problems, | 4,212.22 | 2.5 |
you will cause quite probably
a segmentation fault, | 4,214.72 | 4.63 |
a memory-related problem in your
program such that it just crashes. | 4,219.35 | 3.28 |
It literally says segmentation
fault on this screen | 4,222.63 | 2.88 |
if you touch memory that you should not. | 4,225.51 | 1.71 |
And you should not touch null values. | 4,227.22 | 2.22 |
You should not go to null values. | 4,229.44 | 1.76 |
You should not do star of any
value that itself might be null. | 4,231.2 | 3.89 |
And so, if tree equals equals null
is super, super important here, | 4,235.09 | 3.74 |
because I want to make
sure to immediately | 4,238.83 | 1.75 |
say, well, if you hand me null, that's
like handing me no tree whatsoever. | 4,240.58 | 3.53 |
So, my answer is obviously false. | 4,244.11 | 1.72 |
N can't be in a non-existent tree. | 4,245.83 | 2.84 |
But we need that condition
up top, because the next case | 4,248.67 | 3.135 |
is [? noticed through ?] the following. | 4,251.805 | 1.625 |
Else if n-- the value
we're looking for-- is less | 4,253.43 | 3.95 |
than the value of n in
this node-- tree, recall, | 4,257.38 | 4.39 |
doesn't refer to the whole
thing, per se, in this context. | 4,261.77 | 2.54 |
It refers to the current
node that we've been | 4,264.31 | 1.89 |
past, which at the beginning of
the story is the root of the tree. | 4,266.2 | 3.11 |
So, if the number n in the root of
the tree is greater than the number | 4,269.31 | 4.43 |
we're looking for, we
want to go to the left. | 4,273.74 | 3.77 |
Else we want to go to the right
and search the right subtree. | 4,277.51 | 5.81 |
So, what's the syntax here? | 4,283.32 | 1.5 |
If the n we're looking
for, like 44, is less | 4,284.82 | 3.21 |
than the value at the current node,
55, then what do we want to do? | 4,288.03 | 5.18 |
We want to call search, still
searching for the same number n | 4,293.21 | 4.06 |
but searching on the left subtree. | 4,297.27 | 2.85 |
And how do you pass in a
pointer to the left tree? | 4,300.12 | 3.86 |
Well, you have in tree a
pointer to the root node. | 4,303.98 | 3.44 |
Tree arrow left just means go to my left
child and past that value in instead, | 4,307.42 | 4.83 |
pass its address in instead. | 4,312.25 | 1.23 |
Meanwhile, if the number
you're looking for | 4,313.48 | 2.43 |
is actually greater than the
value in the current node, search | 4,315.91 | 2.75 |
the right subtree, else return true. | 4,318.66 | 5.12 |
Because if the list is not null-- if
there is actually a list and the number | 4,323.78 | 4.56 |
you're looking for is not
less than the current node | 4,328.34 | 2.27 |
and it's not greater than the current
node, it must be the current node, | 4,330.61 | 4.17 |
so you can return true. | 4,334.78 | 2.39 |
But there's one important detail here. | 4,337.17 | 2.09 |
I didn't just call search. | 4,339.26 | 1.96 |
I called return search in each
of these two middle cases. | 4,341.22 | 3.75 |
Why is that? | 4,344.97 | 0.786 |
Well, this is where recursion gets
potentially a little mind bending. | 4,345.756 | 2.874 |
Recursion is the act of a
function calling itself. | 4,348.63 | 3.53 |
Now, in and of itself, that sounds bad,
because if a function calls itself, | 4,352.16 | 3.63 |
why wouldn't it call itself again, and
again, and again, and again, and again, | 4,355.79 | 4.27 |
and just do this
infinitely many times such | 4,360.06 | 2.06 |
that now you get a stack overflow
where all of those frames on the stack | 4,362.12 | 3.06 |
hit the heap and bad things happen. | 4,365.18 | 1.89 |
But no, recursion works beautifully
so long as every time you recurse, | 4,367.07 | 5.58 |
every time a function calls itself it
takes a smaller byte of the problem. | 4,372.65 | 6.01 |
Or rather, put another
way, it throws away | 4,378.66 | 2.31 |
half of the problem, as in this case,
and looks only at a remaining half. | 4,380.97 | 3.042 |
Because if you keep shrinking,
shrinking, shrinking, shrinking | 4,384.012 | 2.583 |
the problem, you will
eventually hit this base case | 4,386.595 | 2.475 |
where either there is no more tree
or you're looking right at the node | 4,389.07 | 4.16 |
that you want to find. | 4,393.23 | 1.27 |
And so, by returning
search and tree left, | 4,394.5 | 3.52 |
or returning search and tree
right, you're deferring the answer. | 4,398.02 | 4.72 |
When you, the search
function, are called and asked | 4,402.74 | 3.22 |
is the number 44 in
this tree, you might not | 4,405.96 | 2.875 |
know because the node you're looking
at at the beginning of the story | 4,408.835 | 2.875 |
was again 55. | 4,411.71 | 1.57 |
But you know who does know? | 4,413.28 | 1.21 |
I bet my left child will know
the answer to that if I just | 4,414.49 | 3.05 |
ask it by passing it-- passing to
search a pointer to it, my left child, | 4,417.54 | 6.98 |
and passing in that same number 44. | 4,424.52 | 2.31 |
So, saying return search is
like saying I don't know. | 4,426.83 | 3.5 |
Ask my left child. | 4,430.33 | 1 |
Or I don't know, ask my right child
and let me return as my answer | 4,431.33 | 4.39 |
whatever my child's answer is instead. | 4,435.72 | 3.52 |
So, you could do this same
function using iteration. | 4,439.24 | 4.4 |
But you could solve it arguably much
more elegantly here using recursion, | 4,443.64 | 5.475 |
because a data structure like
this-- like a binary search tree, | 4,449.115 | 2.625 |
which again is recursively
defined-- each node is conceptually | 4,451.74 | 4.15 |
identical, if numerically
different from the others, | 4,455.89 | 3.14 |
allows us to apply this algorithm,
this recursive algorithm | 4,459.03 | 4.69 |
to that particular data structure. | 4,463.72 | 3.264 |
Now, let's look at a
more concrete incarnation | 4,466.984 | 1.916 |
of trees that allows us to do something
pretty neat and pretty real world. | 4,468.9 | 4.23 |
Indeed, this is another problem borne
of a real world domain of compression. | 4,473.13 | 5.02 |
We talked a couple weeks
ago about encryption, | 4,478.15 | 2.07 |
the art of concealing or
scrambling information. | 4,480.22 | 2.33 |
Compression, meanwhile, is the art
of taking something that's this big | 4,482.55 | 3.54 |
and compressing it to make it smaller,
ideally without losing any information. | 4,486.09 | 5.24 |
It's pretty easy to take
a 10 page essay that's | 4,491.33 | 3.01 |
maybe-- that was supposed
to be a five page essay | 4,494.34 | 2.63 |
and just remove paragraphs from
it or remove sentences from it. | 4,496.97 | 3.85 |
But that changes the meaning of
the paper, makes it a worse paper, | 4,500.82 | 3.45 |
even though you're compressing
it by making it smaller. | 4,504.27 | 2.33 |
No, what most students would typically
do, if you've written 10 pages | 4,506.6 | 2.82 |
and it needs to fit into five,
you really, really, really | 4,509.42 | 2.3 |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.