Data Structures and Algorithms in JavaScript

# Depth-First Search with a Tree Solution

This course has been updated into a 4 part series! We now recommend you take the A Practical Guide to Algorithms with JavaScript course.

Check out a free preview of the full Data Structures and Algorithms in JavaScript course:
The "Depth-First Search with a Tree Solution" Lesson is part of the full, Data Structures and Algorithms in JavaScript course featured in this preview video. Here's what you'd learn in this lesson:

## Bianca looks at the depth-first search algorithm for a Tree and compares the implementation to the Graph version. The code for the DFS Tree implementation is located in the Github exercise repository on the "solutions" branch.

Get Unlimited Access Now

Transcript from the "Depth-First Search with a Tree Solution" Lesson

[00:00:00]
>> Bianca Gandolfo: What do you remember about depth first search with a tree, and how is it different? Let's compare just for fun.
>> Speaker 2: You don't have to keep track of where you've visited.
>> Bianca Gandolfo: Let's go,
>> Bianca Gandolfo: Why don't we have to keep track of where we've visted in the tree?

[00:00:21] Solution.
>> Speaker 3: Cuz you don't have to worry about the pads.
>> Bianca Gandolfo: Yeah, cuz here's our tree one.
>> Bianca Gandolfo: So for a tree, in this case we're not keeping track of our depth. We could do that though if we wanted in a similar way. What we're doing this for each here.

[00:01:05] The main difference is we have those for each. This for each is gonna check if it's visited. This tree we don't.
>> Bianca Gandolfo: And what's the reason again? I said it but then suddenly forgot.
>> Bianca Gandolfo: Why? What happens--what's the difference between a tree and a graph? The main difference?

[00:01:40]
>> Speaker 3: A graph can have cyclical paths.
>> Bianca Gandolfo: Yeah, and so what would happen if we didn't check, if we visited and we had a cycle?
>> Speaker 2: Infinite loop.
>> Bianca Gandolfo: We'd have an infinite loop. And so we have this catch that's saying okay, if this has been visited we're not gonna keep traversing, and recursing into that every time.

[00:02:06] Cool? Great.
>> Bianca Gandolfo: All right. Any questions?
>> Bianca Gandolfo: So, there was a question online about passing this to for each. For each takes a second parameter. And that's just setting the context so that you don't lose the value of this. We briefly talked about this being kind of finicky and there's certain rules.

[00:02:39] And how we can't explicitly set it inside of the body of the function except for using call and apply or using the keyword new. Or what else did we say? Just calling it to the left of the dot, etcetera. And then some functions take that into consideration in their implementation like for each to make sure that you're able to explicitly set what this is, so that you don't lose your context.

[00:03:08] Which is a common problem in JavaScript, just losing your context and then you're saying this dot my function, and then you're getting an error, you're running out of the window for some reason. Or it says window is not a function, or something crazy like that. So, that's this.

[00:03:29] Okay.