Why should you consider using trees?

You can find more information about trees in some of the earlier episodes. I’ll try to not repeat the same things here. Episodes 41 and 42 discuss binary trees and left-child right-sibling trees. Episode 54 explains recursion and uses a tree in the examples. And episode 66 explains the composite design pattern which can be thought of as a tree. Listen to these episodes for more details.

Trees are great at organizing items. Anytime you have a situation where organization can start out in broad terms and then get more specific, then a tree is great.

Listen to the full episode for more examples and insight, or you can also read the full transcript below. And don’t forget to sign up to be a patron of the podcast by clicking the button at the bottom of the page and selecting your reward level. It doesn’t cost much and you can get some awesome benefits such as actual code reviews to help you write better code. Who knows, maybe you might even find out that your code should be using a tree or something else instead.

Transcript

You can find more information about trees in some of the earlier episodes. I’ll try to not repeat the same things here. Episodes 41 and 42 discuss binary trees and left-child right-sibling trees. Episode 54 explains recursion and uses a tree in the examples. And episode 66 explains the composite design pattern which can be thought of as a tree. Listen to these episodes for more details.

A lot of the earlier episodes break about halfway through for a sponsor message. That sponsor is Take Up Code and I used the time to promote live classes. I still organize live classes and that’s a great way for you to supplement your training. Some topics are best explained with actual code instead of just words.

There’s a new way that you can get some great extra benefits and help support this podcast at the same time. I just setup a Patreon account with several reward levels for you to choose from. You can get design reviews of your project ideas, code reviews, and participate in game development sessions each month. You can find a link to the Patreon page at the bottom of each Take Up Code website page.

There’s also a chance for you to win a free hour of tutoring. And you’ll have the best chances of winning the tutoring right now when the number of patrons is small. I call it tutoring, but really, the main thing is that you can get an hour of some serious experience applied to your project. So visit takeupcode.com today, click on the button to become a patron, and select your reward level. I thank you for your support.

Alright back to trees.

Instead of going into more theory about how trees work, at least for now, it’s good to ask why you would want to use a tree in the first place. What problems do trees help you to solve? And what difficulties would you face if you tried to avoid them?

Trees are great at organizing items. Anytime you have a situation where organization can start out in broad terms and then get more specific, then a tree is great.

Maybe you have a store that sells items. You can group them into big top-level groups such as food, furniture, and tools. Then each of these can be further divided into smaller and smaller groups. So maybe you have food, then vegetables, then grains, and finally wheat.

Or you can approach the problem from the other direction and think about where you could put a ruler. It’ll be in tools, certainly. But where in tools? Well, that depends on how many different kinds of tools you have or think you might need to organize. If the only tools that your store sells are rulers and saws, then maybe it doesn’t matter and you can just put them both in tools and be done. But when you start getting so many tools that it becomes hard to find what you’re looking for, then that’s when you need to add extra organization levels to your tree.

Maybe you can start by splitting all the tools into either hand tools or power tools. Then you can split those by their intended purpose such as measuring and cutting. Then the ruler can go into the measuring group.

There’s no single right way to organize a tree like this. You might instead prefer to divide tools into the intended use categories right away. And then organize them into hand tools vs. power tools.

Sometimes, you might find that something could fit equally well into multiple categories. What do you do then? This is where a tree alone might not be the best solution. Maybe some kind of tagging system will be better.

Trees work best when there’s an obvious and well structured way to organize things so they can be found easily. They can add structure to a large list of items and let users find something even if the exact name or identity isn’t known.

These items can be things or objects or they can be more abstract such as available actions that a game player can take, or available upgrade options.

As long as you make your tree start out with easily understood nodes with no overlap and then follow this same rule for additional nodes, then your users will come to understand your system. But if you have a tools node in your tree and you try to put hand tools, power tools, and rulers as child nodes, then you’re going to confuse people. Hand tools, power tools, and rulers seems a bit wrong to me because of a couple reasons.

First is the size that I image each child node to be. Without counting, it just seems like there would be a lot of hand tools and a lot of power tools. But probably very few rulers.

And second is that I would have expected rulers to be included in hand tools. Not sitting in their own node right next to hand tools.

Trees also help limit what your code needs to consider at any given time. If you’re building a game with a large list of items of different types, then a tree can help you so you only need to keep a reference to the root of the tree. Anytime you want something, just go through a few of the nodes in the tree until you reach a smaller group of similar items. This is a lot better than scanning through the entire list each time or trying to remember every item that you found in case you need it again.

Imagine going into a grocery store where everything was arranged on the shelves in alphabetical order. Now, you might at first think, why not? If you know exactly that you want bread and sugar, then this might work. I mean, it’s how dictionaries organize the words and that works out pretty good. Why not organize grocery stores the same way?

Because it might not be obvious exactly where something will be. Take bread for example. Is it under b for bread? Or under s for sandwich bread? What if you wanted a roll of french bread? Is that under r for rolls, of f for french, or back to b for bread? By putting all the various types of bread together, not only does the store avoid this problem, but might even get you to consider buying a new type of bread that catches your attention.

Even this system is not perfect though. Where would you find ketchup? Should it be with the bread? Or with the potatoes? Or with the canned tomatoes? Or should it get its own section?

Whatever you decide, organizing items into some kind of tree is still much better than a single big list organized alphabetically.

Feedback

What's on your mind?
On a scale of 0 to 10, how likely are you to refer us to friends?