Check out a free preview of the full Tree and Graph Data Structures course
The "Binary Search Exercise" Lesson is part of the full, Tree and Graph Data Structures course featured in this preview video. Here's what you'd learn in this lesson:
Bianca instructs students to write the search method for binary search trees, and completes a brief walkthrough of how the method should work.
Transcript from the "Binary Search Exercise" Lesson
[00:00:00]
>> Bianca Gandolfo: And then let's skip ahead and talk about the find and search operation and we'll do it together all at once. So, this a binary search tree, right? So, we need to be able to find/search for the thing that we are looking for right? So we are trying to detect if something is in collection.
[00:00:20]
And we are gonna need to traverse the tree to find that value, right? So, with no numbers here, we can know that we will take our value and we will compare it to this node and we'll say, is this value less than or greater than? If the value is greater than, we go to the right.
[00:00:40]
We compare it to this node now, if the value is less than or greater than. If it's less than, for example, we would go to the left. And this has no children. So if this isn't it, then it doesn't exist on this tree. Does that make sense? Cool.
[00:01:03]
So let's say that we are looking for 15, let's look for 15. So we'll start at eight, greater than or less than.
>> Speaker 2: Greater.
>> Bianca Gandolfo: Greater, everyone knows that, right? Greater, we know, compare, less than or greater than?
>> Speaker 2: Greater.
>> Bianca Gandolfo: Greater. Less than or greater than?
>> Speaker 2: Greater.
[00:01:24]
>> Bianca Gandolfo: Greater. Is there a 15 there? No. So, this does not contain our value, yeah pretty straight forward, all right. So let’s take some of our last moments together, 15 minutes, to take a stab at our binary search tree. I know it's been a long day. Well, let's just take a stab at it.
[00:01:49]
It's easier than the graph. So I think it's a nice thing. We'll end on top, we'll have completed something.
Learn Straight from the Experts Who Shape the Modern Web
- In-depth Courses
- Industry Leading Experts
- Learning Paths
- Live Interactive Workshops