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

The "Introduction to Trees" 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 introduces trees by coding a basic tree in JavaScript, diagramming its various parts, and giving real-world examples.

Preview
Close

Transcript from the "Introduction to Trees" Lesson

[00:00:00]
>> Bianca Gandolfo: So, maybe we have another question, like do you have milk, Yes or No, right? These are separate objects, we have a third object here. And this one has the Recommendation, cool? So these are just objects with properties, and then we combine them like this. So if you notice, this Yes is now an object, and this No is now an object.

[00:00:32]
Cool, so we're just nesting our objects here. We all feel comfortable about that, we've done that before in our life, probably. Yeah, so that's a tree, right? So we have our tree, it's a hierarchical data structure. And we have nodes connected with relationships. So our top node we call a root, right?

[00:00:58]
We retain access to the root and we explore the tree from the root. So we have things like left trees, right trees. The interesting thing about a tree is it's made up of more trees. So if we only had this node here, this would be a tree with one node in it, right?

[00:01:16]
If we remove this node and we only have this left tree, this is a tree, this node is a tree. These three things are a tree, all of these together are trees. So it's like our trees are made up of other trees. And when you start to hear that, maybe start having it click recursion, right?

[00:01:33]
When we breaking down problems into smaller versions of itself, that's gonna be recursive problem. So, we recursive data structures, we have recursive algorithms, think about it in that way. And another thing we should know about trees and the terminology, so we can talk the talk, is we have leaves.

[00:01:51]
So a node that has no children is a leaf node. And when we talk about the relationships between nodes in a tree we talk about them like family. Like parent, child, sibling, so this root is a parent to this left tree node. These two pink ones are siblings, etc.

[00:02:17]
And we also would say this pink node has an ancestor, right? So we just talk about it like we're all family here, so be very comfortable about that. And if you don't know what it is, just think about how you would actually talk about a family tree and it's probably right.

[00:02:34]
Or they'll think you're poetic, that you know this so well that you're just inventing terms that make sense.
>> [LAUGH]
>> Bianca Gandolfo: What's a good one? I don't know, maybe this one's the great aunt or something. Anyway, so talked about the terminology. Where do we see trees in real life?

[00:02:54]
A DOM is a tree, who works in DOM, in the browser land? Yeah, so you're dealing with trees all the time, whether you know it or not. Did you guys know that, the DOM is a tree? Good, good, so autocomplete, you start typing in Google. We did that on birthday, Florida man activity, where you type in your birthday, and then Florida man, and then whatever fills in says something.

[00:03:20]
I don't know, it's just funny? Is that the point? It's just funny, okay? Sometimes it tells your future or something. Or, in a past life, you did that, I don't know. So this autocomplete feature uses a kind of tree called a trie. And when we're coding or running our code under the hood, parsing, we have these abstract syntax trees.

[00:03:46]
So they're all trees, trees are everywhere, they're useful. Many trees is actually called a forest, which I think is kinda cute. If you have lots of trees it's a forest. What else do I wanna tell you about trees? Do you guys have questions? Yeah.
>> Speaker 2: So this would be one-dimensional still, or is it two-dimensional, multi-dimensional?

[00:04:08]
How do you, could you-
>> Bianca Gandolfo: What's a dimension to you?
>> Speaker 2: Well, that's my question.
>> Bianca Gandolfo: Yeah.
>> Speaker 2: Cuz you guys brought that up with the linear structures, and I was just, cuz those are, but this is looks one-dimensional to me.
>> Bianca Gandolfo: Yeah, I see what you're saying.

[00:04:24]
So I don't think about linear versus non-linear, dimensional or not-dimensional, I think about it in levels. Does it have like a single level or does it have multiple levels? Does this seem like it's one level or multiple levels to you?
>> Speaker 2: Multiple levels.
>> Bianca Gandolfo: Yeah, for sure.

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