Check out a free preview of the full Tree and Graph Data Structures course

The "Binary Tree traverse Solution" 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 live codes the solution to the exercise for the traverse method for binary trees.

Preview
Close

Transcript from the "Binary Tree traverse Solution" Lesson

[00:00:00]
>> Bianca Gandolfo: All right, so we wanna do a few things here. We're gonna do a traverse and contains. Let's go back over to our guy over here. Let's take our class, and we'll just copy it over and we'll do it together. So we can leave our,
>> Bianca Gandolfo: Traverse as notes that we can reference for ourselves.

[00:00:27]
Okay, again this is a little bit different because we're creating a class. We're gonna see this and whatnot, okay? So, for each tree we wanna give it, we know we had our question = question kind of deal. Here we go. Okay. So we have a question. And then we're gonna have our yes and our no.

[00:00:50]
Does this?
>> Bianca Gandolfo: Okay, this is our binary tree with yes and no. Okay? If you did it with left and right that's totally fine. And in the solutions you'll see that it's also left and right. Okay, so we have a traverse here. And often with our traverse we want to we want it to take like a function.

[00:01:18]
So once you visit every node you might want to do some work, right? So, so far we've been console logging, but there are a variety of things you might want to do on each node. Maybe you're validating that the data there is correct. Maybe you're looking for something in the sense of contains, right?

[00:01:46]
Yeah, all kinds of fun things we do with traverse. All right, so as you might notice in the exercise as well, there are different orders that you can traverse. But we can just work with what we have here. We can just start with this, and we'll adapt it for our class, okay?

[00:02:11]
So right off the bat, we know that this tree.name isn't relevant, right? Right, so we want this.question, right? So the first time we get here we're just gonna print out the question, right? What if we wanted to do something different? What if we wanted to pass a function here to be called,instead of, so console.log is a function, right?

[00:02:40]
Console is like a DOM object. Log is a method. And we call it like we would. Sometimes when you see things a lot you kind of take it for granted and don't break down what it is. So console.log is just a question, so we can pass our function here and we can replace console.log with some function.

[00:03:01]
And when we call it let's see we have myTree.traverse. We can actually just pass in console.log here and it will do the same thing. Everyone following? Yes, can I get some thumbs, we're good? Right, awesome. So we have our question, and this is gonna be different too, right?

[00:03:28]
Because we don't have an array, we have two different property names. How did you guys handle it when you did this?
>> Speaker 2: I called in order transversal on this.left, and then called it on this.right.
>> Bianca Gandolfo: Yeah. So we just recurse, right? Again you can say,
>> Bianca Gandolfo: Something like that, seem reasonable?

Learn Straight from the Experts Who Shape the Modern Web

  • In-depth Courses
  • Industry Leading Experts
  • Learning Paths
  • Live Interactive Workshops
Get Unlimited Access Now