Tree and Graph Data Structures

# Coding a Tree

## Bianca Gandolfo

Thumbtack

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

The "Coding a 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 demonstrates one way to code the structure of a basic tree for the inner structure of the chatbot.

Preview
Close

### Transcript from the "Coding a Tree" Lesson

[00:00:00]
>> Bianca: So here's our insert. So the first thing that we would change is where we we don't want this dot value, we want this dot question, right? So every note is going to have a question right? Seem reasonable? You guys believe me. You have doubts? That looked like a doubting face.

[00:00:20]
Okay, okay, and then instead of children, we're gonna have a yes, right? Which is gonna be, let's just set it to null for now. And the reason I'm setting this to null instead of undefined is because I want it to be clear that I set it to null, and it's not just like a runtime thing assignment, right?

[00:00:50]
So runtime if something that's set it will be undefined, but when I set something, I'm not gonna ever set it to undefined, I'm always gonna set it to null. So it's a good thing, they're basically the same thing, but that's an important differentiator. Especially for debugging and stuff like that.

[00:01:06]
Okay, so here we go. Anything else?
>> Bianca: Like the recommendation?
>> Speaker 2: Yeah.
>> Bianca: So,
>> Bianca: We could also pass that in. So, if there is a recommendation, right? Man, this is a long word, right, we wanna set it.
>> Bianca: Seem reasonable?

[00:01:56]
Or we could give it a default value.
>> Bianca: Something like this.
>> Bianca: Cool, all right. So that's instantiating. Now, just one question, right? Now if we want to add a child, right? We wanna add a question, or a recommendation, to our chat bot. What do we need to change here?

[00:02:49]
>> Speaker 2: Having a function for inserting a question or inserting a recommendation, because you need different parameters.
>> Bianca: Yeah, or just keep it in one for now. So we'll need a pass in those questions or recommendations. The other thing that we could do, since a question is never a recommendation, we can always just, this could be display text, right.

[00:03:17]
So we always have a display text, right? It could be a question or it could be recommendation. If it's a question, or if it's recommendation, then the SNO will be null, like that's another way we can derive whether it's a question or recommendation. But let's just make it explicit to make it simpler for everybody.

[00:03:42]
Okay, so we're gonna have to make a new tree. We'll just keep calling this tree. And we'll add, we'll just past these down, okay?
>> Bianca: Now, this is where it gets interesting, right. So this.children.push.
>> Bianca: What do we need to know? Let's just pretend there's no recommendations for now.

[00:04:18]
Let's just stick with questions to simplify.
>> Speaker 2: So do we need to know if we're pushing the yes or no?
>> Bianca: Yeah, how can we do that?
>> Bianca: Another parameter, right? All right, I'm gonna call it side, it'll be the yes side or the no side. Anyone have a better name?

[00:04:43]
No, okay. So, if we could say this.
>> Bianca: [side] = question, so let's call this newQuestion.
>> Bianca: Cool, again, there's a danger here, what's the danger here?
>> Speaker 2: Maybe, [INAUDIBLE].
>> Bianca: Yeah, like maybe people, the consumer of your tree isn't, this is something that they pass, yep, or something weird, and it's not lining up with your expectations.

[00:05:43]
But for simplicity, as long as we were aware of that, it's fine we can add a check. You know, in an interview setting, you know, if you do something like this in your interview might say, like you know, what if it's a user error and then you can say, you know, I will put a check in, and then you can ask them.

[00:06:05]
Would you like me to do that, or do you want me to focus on actually solving the problem? Like, you can get caught up in these details of if it's undefined, all of this air checking stuff that might take away from you actually finishing the problem. And so it's useful to prioritize and to talk with your interviewer, because maybe it's really important for your job to demonstrate that you know how to check for edge cases early on.

[00:06:31]
You know, just talk with your interviewer and see what they think. But just let them know that you're thinking about it and you're making a decision right now to not prioritize that, because you're making a trade off for time and you really wanna focus, perhaps, on the meat of solving the problem rather than catching these errors.

[00:06:49]
Both are important, but it just kinda like, one of them shows. This approach shows that you're thinking about it, you're aware of it, also it might save you some time. Okay, cool.
>> Bianca: We good?
>> Bianca: Or are we great? Are we great? I'm feeling good. I'm happy about our little tree.

[00:07:19]
There's some improvements. I still feel pretty good about it.
>> Bianca: Cool, all right, back to it.
>> Bianca: Okay, so, you're here, we took a little detour to talk about how we could even get here from where we were, I think that was important for us, yeah? Okay, so now that we're here we need to figure out how do we get to the point where we know we're at a recommendation, all right.

[00:07:56]
So off the bat we know that we need to look at every single node of the tree right. Does anyone think there's a way to get past that? Read this, this would be an optimization.
>> Speaker 2: So if yes, or no, or not null on a node go to the next node, keep going down until you find one that's not null?

[00:08:23]
>> Bianca: Mm-hm, yeah, so that would require you going through the entire array, right? So we would traverse through the entire array or tree.
>> Bianca: Anything? Keep it in your mind. Just keep it on your mind. We'll revisit.

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

• In-depth Courses