Tree and Graph Data Structures

Binary Tree Traversal Exercise

Bianca Gandolfo

Thumbtack

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

The "Binary Tree Traversal Exercise" 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 instructs students to write a traverse method for binary trees.

Preview
Close

Transcript from the "Binary Tree Traversal Exercise" Lesson

[00:00:00]
>> Bianca Gandolfo: So we're gonna get ready for our next exercise. So this is a binary tree. So a binary tree is a tree that has a property that says each node can only have two children. We've been doing that right? Because our chatbot tree, yes or no, has two children.

[00:00:29]
>> Bianca Gandolfo: So when you make this, you can choose to make your, you can name them like yes or no like we did. And that may be useful for continuity or you can call them left and right, that's another convention for when you make a binary tree. I think someone was asking about left and right before.

[00:00:51]
This is the context where that makes sense. For a regular tree, we call a regular tree an N-ary tree. That means it can have N number of children in ary. I like to think imaginary, but I don't know. Okay, so we have some operations just to think about fixed and dynamic, just to think about, we'll talk about them later.

[00:01:22]
But we're going to jump into the exercise. This is a binary tree exercise, you can follow the link. It's the same, it's just a different file name. So make sure it's binary trees, and not binary search trees. Cool, and we are going to work on the traverse. You may need to adapt your tree to, you're gonna need to write out the constructor part of your binary tree, and you're probably gonna have to write the insert as well.

[00:01:59]
Because you're gonna wanna be able to test your tree.
>> Speaker 2: The binary tree example doesn't have a traverse, it's got a couple traversal functions.
>> Bianca Gandolfo: Really, well. Just do anyone you want. Just get it to traverse, and then we'll dive into specific types of traversal. Just get it to loop through and console log stuff.

[00:02:26]
And then we can be specific about the type of traversal, the order.

Learn Straight from the Experts Who Shape the Modern Web

• In-depth Courses