Tree and Graph Data Structures

# Traversing One Tree

## Bianca Gandolfo

Thumbtack

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

The "Traversing One Tree" 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 uses loops to live code a traversal function for binary trees that prints the value of a root node and its leaf nodes.

Preview
Close
Get \$100 Off
Get \$100 Off!

### Transcript from the "Traversing One Tree" Lesson

[00:00:00]
>> Bianca Gandolfo: So let's take it to the editor. We're going to do group live coding. If you've taken one of my classes before, you know it's my favorite.
>> Bianca Gandolfo: So we're gonna have our data structure here.
>> Bianca Gandolfo: I wonder why it's so upset.
>> Bianca Gandolfo: It's mad at me because my file name, it doesn't like the file name.

[00:00:37]
>> Bianca Gandolfo: Okay.
>> Bianca Gandolfo: All right, okay. Here we go. Let's wrap this so we can see it.
>> Bianca Gandolfo: All right. There we go. And then, we want to traverse. Let's just go rid of that. Actually, I'll leave it cuz I'll send it out. So, let's just start fresh here, and we are going to.

[00:01:15]
>> Bianca Gandolfo: Just talk about traversing, right? So we like need some function. We'll call it traverse. You know, we can like take our graph, why not.
>> Bianca Gandolfo: Okay. So.
>> Bianca Gandolfo: Yeah, the sum value. So, this is our graph, mother, it has a name and it has children.

[00:01:50]
So the first thing we can do is what, check if it has children, right?
>> Bianca Gandolfo: Shoot,t his is supposed to be tree, you're supposed to be correcting me.
>> Bianca Gandolfo: Children.
>> Bianca Gandolfo: So if the tree has children, and notice this is different, we're not doing this is here. Why is that?

[00:02:13]
We were doing this.children.push before.
>> Speaker 2: [INAUDIBLE]
>> Bianca Gandolfo: Mm-hm. Yeah, so before we were writing class, here, we're just passing our tree to a function. So it's just a different way of approaching it. From my personal preference, I feel way more comfortable just using functions cuz I'm a little more old school, like the classes and stuff.

[00:02:38]
Like we're horrendous, yeah six classes are much nicer. However, for me, just cozy to just use a function, like why are we making this complicated? That's me, but you should choose whatever you feel good about, whatever you think showcases your skill set the best. So like if you're like an ES six wiz and you want them to know that, then yeah, go crazy.

[00:03:06]
Do all the crazy stuff. I'm always impressed anyway. Okay, so if they're children, what do you wanna do?
>> Bianca Gandolfo: Like maybe you wanna first, we wanna do something with, let's back up, we probably wanna do something with the node. The first node, right? And we can do something easy, just like console.log(tree.name).

[00:03:48]
>> Bianca Gandolfo: Cool? So we'll console log the name, so that the very first thing we pop in here, this will print out actually, right? Again, danger. We're not checking if there's a tree. But that's okay cuz we're talking about it, and we'll prioritize that if we have time, which we won't today.

[00:04:07]
But, maybe you won't either in your interview. Hopefully you don't, because that's like the boring stuff anyway, I think. So we're gonna print Ashley. Cool, everyone cool with how we got here? Now we have our children to deal with.
>> Bianca Gandolfo: What do we think?
>> Speaker 2: I mean children, your children can help children.

[00:04:36]
Check to see if the child is a parent.
>> Bianca Gandolfo: Well first, let's just deal with these immediate children. Let's imagine a world where our children don't yet have children.
>> Speaker 2: Okay.
>> Bianca Gandolfo: Let's just start simple.
>> Speaker 2: It's start with the index at zero as the left side.
>> Bianca Gandolfo: Mm-hm.

[00:04:56]
And then? And then do what?
>> Speaker 2: So, I guess it would just console.log, children at 0, and that would be the last side of the node.
>> Bianca Gandolfo: Like this?
>> Bianca Gandolfo: Cool, so then, that would print out little Summy. But what about Alex?
>> Alex: So we can loop through the array.

[00:05:30]
>> Bianca Gandolfo: Yeah.
>> Alex: Print out the name of each child node.
>> Bianca Gandolfo: Sure.
>> Bianca Gandolfo: Let's go crazy.
>> Bianca Gandolfo: Okay. We'll do something like this, val.name.
>> Bianca Gandolfo: Does seem reasonable. There are bugs here.
>> Bianca Gandolfo: Are you guys running this code in your mind? Do you know how to do that?

[00:06:14]
Mind debugging. It's also a good thing. Useful in your interview, is debugging your code without running it.
>> Alex: We don't need the if, anymore, cuz they're using for each.
>> Bianca Gandolfo: Yeah.
>> Alex: I couldn't hear that, sorry.
>> Speaker 4: We don't need the if anymore. Cuz for each we'll [INAUDIBLE].

[00:06:36]
>> Alex: But if i'ts undefined for each will.
>> Speaker 4: Good point.
>> Bianca Gandolfo: Yeah.
>> Speaker 4: That do all trees have children by default?
>> Bianca Gandolfo: Yes, we define that, right? There are gonna be empty, it'll be an empty array. Okay, so maybe we need the f maybe we don't, let's go with node if, I like less.

[00:06:59]
>> Bianca Gandolfo: Cool. So again, this is all like even treed out name, we just need to make sure there's a tree. We can catch that before, but we don't need to worry about that. Let's just assume that we always have a tree. And just note, that we do need a handle that kind of thing but we'll do it later, cool?

[00:07:24]
>> Bianca Gandolfo: So now, this is cool, we have printed out all the names of this little family. So sweet, I feel like I know them already.
>> Bianca Gandolfo: Now it gets fun. What if Sammy gets a child, maybe its a dog name Bowser, and Bowser, Bowser's got puppies. Let's just say one, because my slide was too small.

[00:08:08]
But you'll know it's little puppy pickles here. So, we traversed through our tree with one level, right? One to five, how complicated was that for you? This.
>> Bianca Gandolfo: One to five, like one being not complicated and five being very complicated. One or two?
>> Speaker 4: Two.
>> Alex: Two.
>> Bianca Gandolfo: Two, yeah.

[00:08:37]
So when we look at it, you're like, yeah, it's simple now that we know, but coming up with that might be what takes it to make it a little bit more complicated, but now you know. Now we all know, that if we get a tree and we need to go through this one level of children, will just look through.

[00:08:55]
It makes sense now after we have started, but sometimes it's hard to get there. So, we'll be like as with practice, we just get that intuition and how that all works.

### Learn Straight from the Experts Who Shape the Modern Web

• In-depth Courses