text stringlengths 1 81 | start float64 0 10.1k | duration float64 0 24.9 |
|---|---|---|
So therein lies our
logarithmic running time. | 3,903.32 | 3.14 |
Therein lies the height
of the tree, so long | 3,906.46 | 3.43 |
as I am good about
keeping the tree balanced. | 3,909.89 | 5.88 |
There's a danger. | 3,915.77 | 1.25 |
Suppose that I go ahead and start
building this tree myself in code | 3,917.02 | 6.42 |
and I'm a little sloppy
about doing that. | 3,923.44 | 2.59 |
And I go ahead and I insert, for
instance, let's say the number 33. | 3,926.03 | 7.88 |
And it's the first
node in my tree, so I'm | 3,933.91 | 2.28 |
going to put it right
up here at the top. | 3,936.19 | 2.69 |
And now suppose that the
next number that just happens | 3,938.88 | 2.45 |
to get inserted into this tree is 44. | 3,941.33 | 2.225 |
Well, where does it go? | 3,943.555 | 1.485 |
Well, it has no children
yet, but it is bigger, | 3,945.04 | 2.57 |
so it should probably go over here. | 3,947.61 | 1.5 |
So, yeah, I'll draw 44 there. | 3,949.11 | 1.79 |
Now, suppose that the
inputs to this problem | 3,950.9 | 2.36 |
are such that 55 is inserted next. | 3,953.26 | 1.79 |
Where does it go? | 3,955.05 | 1.19 |
All right, 55, it's bigger,
so it should go over here. | 3,956.24 | 4.09 |
And then 66 is inserted next. | 3,960.33 | 2.2 |
All right, it goes over
here-- never mind that. | 3,962.53 | 6.14 |
So, what's happening to
my binary search tree? | 3,968.67 | 3.342 |
Well, first of all, is
it a binary search tree? | 3,972.012 | 1.958 |
It is because this node is
bigger than its left child, | 3,973.97 | 4.51 |
if any-- there just isn't any--
and it's less than its right child. | 3,978.48 | 3.69 |
How about here, 44? | 3,982.17 | 1.02 |
It's bigger than its left child,
if any-- because there is none-- | 3,983.19 | 2.81 |
and it's smaller than its right child. | 3,986 | 1.94 |
The same thing is true for 55,
the same thing is true for 66. | 3,987.94 | 2.71 |
So, this is a binary search tree and
yet somehow what does it look like? | 3,990.65 | 5.41 |
It looks like a linked list, right? | 3,996.06 | 2.27 |
It's at a weird angle. | 3,998.33 | 1.09 |
I've been drawing
everything horizontally, | 3,999.42 | 1.75 |
but that's a meaningless
artistic detail. | 4,001.17 | 2.12 |
It devolves potentially
into a linked list. | 4,003.29 | 2.67 |
And so, binary search trees if
they are balanced, so to speak, | 4,005.96 | 3.58 |
if they are built in the right order
or built with the right insertion | 4,009.54 | 3.8 |
algorithm such that they do
have this balanced height, | 4,013.34 | 4 |
this logarithmic height, do afford
us the same logarithmic running time | 4,017.34 | 4.49 |
that the phone book example did and
our binary search of an array did. | 4,021.83 | 4.04 |
But we have to do a little
bit more work in order | 4,025.87 | 2.47 |
to make sure that these
trees are balanced. | 4,028.34 | 2.05 |
And we won't go into detail
as to the algorithmics | 4,030.39 | 2.21 |
of keeping the tree balanced. | 4,032.6 | 2.07 |
But realize, again, there's
going to be this trade-off. | 4,034.67 | 2.44 |
Yes, you can use a binary search tree or
trees more generally to store numbers. | 4,037.11 | 4.03 |
Yes, they can allow you to achieve that
same binary or logarithmic running time | 4,041.14 | 4.78 |
that we've gotten so
used to with arrays, | 4,045.92 | 1.94 |
but they also give us
dynamism such that we | 4,047.86 | 2.34 |
can keep adding or even removing nodes. | 4,050.2 | 2.39 |
But, but, but, but it
turns out we're going | 4,052.59 | 2.58 |
to have to think a lot harder about
how to keep these things balanced. | 4,055.17 | 4.02 |
And indeed, in higher
level CS courses, courses | 4,059.19 | 2.35 |
on data structures and
algorithms will you | 4,061.54 | 1.75 |
explore concepts along
exactly those lines. | 4,063.29 | 2.96 |
How would you go about implementing
insert and delete into a tree | 4,066.25 | 4.34 |
so that you do maintain this balance? | 4,070.59 | 2.01 |
And there is yet more variance
on these kinds of trees | 4,072.6 | 2.3 |
that you'll encounter accordingly. | 4,074.9 | 1.55 |
But for our purposes, let's consider
how you would implement the tree itself | 4,076.45 | 3.59 |
independent of how you might
implement those actual algorithms. | 4,080.04 | 4.27 |
Let me propose this type of node. | 4,084.31 | 1.74 |
Again, notice just the very
generic term in programming | 4,086.05 | 2.62 |
where it's usually like a container for
one or more other things, and this time | 4,088.67 | 4.436 |
those things are an
integer-- we'll call it n | 4,093.106 | 1.874 |
but it could be called
anything-- and two pointers. | 4,094.98 | 2.87 |
And instead of next, I'm going to just
by convention call them left and right. | 4,097.85 | 4.78 |
And as before, notice that I do
need to declare struct node up | 4,102.63 | 5.13 |
here or some word up here. | 4,107.76 | 1.769 |
But by convention I'm just going to do
typedef struct node, because C reads | 4,109.529 | 4.431 |
things top to bottom, left to right. | 4,113.96 | 1.5 |
So if I want to refer to
a node inside of a node, | 4,115.46 | 3.069 |
I need to have that vocabulary, per
this first line, even though later on I | 4,118.529 | 4.381 |
just want to call this
whole darn thing a node. | 4,122.91 | 3.019 |
And so, that's the distinction. | 4,125.929 | 1.291 |
This actually has the side
effect of creating a data | 4,127.22 | 2.219 |
type by two different names. | 4,129.439 | 1.166 |
One is called struct node, and
you can literally in your code | 4,130.605 | 2.805 |
write struct node something,
struct node something. | 4,133.41 | 2.44 |
It just feels unnecessarily
verbose, so typedef | 4,135.85 | 3.179 |
allows you to simplify
this as just node, which | 4,139.029 | 2.62 |
refers to the same structure. | 4,141.649 | 1.371 |
But this is necessary for this
innermost implementation detail. | 4,143.02 | 4.459 |
So, now that we have the
ability with a structure | 4,147.479 | 2.101 |
to represent this thing, what
can we actually do with it? | 4,149.58 | 3.29 |
Well, here is where recursion
from a few weeks ago | 4,152.87 | 3.88 |
actually gets really compelling. | 4,156.75 | 1.75 |
When we introduced that sigma example
a while ago and talked in the abstract | 4,158.5 | 4.41 |
about recursion, frankly, it's kind of
hard to justify it early on, unless you | 4,162.91 | 4.35 |
actually have a problem that lends
itself to recursion in a way that | 4,167.26 | 3.41 |
makes sense to use recursion
and not just iteration, | 4,170.67 | 2.559 |
loops-- for loops, while
loops, do while, and the like. | 4,173.229 | 2.811 |
And here we actually have a
perfect incarnation of that. | 4,176.04 | 2.739 |
What does it mean to search
a binary search tree? | 4,178.779 | 2.45 |
Well, suppose I'm
searching for a number n | 4,181.229 | 2.781 |
and I'm being given a pointer
to the root of the tree, | 4,184.01 | 2.75 |
and I'll call it tree. | 4,186.76 | 1.04 |
So, just like when I was
searching a linked list, | 4,187.8 | 1.63 |
I'm given two things, the
number I'm looking for | 4,189.43 | 1.82 |
and a pointer to the first thing in
the data structure-- the first thing | 4,191.25 | 2.779 |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.