205: Trees: What Can You Do With Them?
Take Up Code - Podcast autorstwa Take Up Code: build your own computer games, apps, and robotics with podcasts and live classes
Kategorie:
What other things can trees help you to solve? When thinking about trees, I usually put them in two groups. The first are trees with arbitrary structure. The second group of trees have a well defined structure. Listen to the full episode or read the full transcript below for more details and examples. And if you’re interested to see actual code for things like this, then consider supporting the Take Up Code podcast on Patreon. Once we reach the video goal, I’ll start making short videos that show code. A tree will make for a good video and will be included at some point. Just click on the link at the top to Become a Patron. For a dollar a month, you can get access to extra podcast episodes. Transcript When thinking about trees, I usually put them in two groups. All trees are composed of nodes with a clear concept of parent nodes and child nodes. One node in the tree serves as the root and has no parent. The child nodes can have their own child nodes. Some trees might be limited in how many child nodes any particular parent node can have or where nodes can be placed. It’s these limitations that I use to group trees. The first are trees with arbitrary structure. This is like the grocery store categorization from episode 203. You get to decide where things exist in the tree. Or that can also mean that somebody using your app gets to decide on the tree structure. I mean, an outline tool wouldn’t be very useful if it came with one and only one outline built-in. The user needs to be able to create their own outlines and you may want to keep track of the data as a tree inside your app. The benefit of this type of tree is that things stay exactly where you put them. Have you ever had a messy room but still knew where everything was? Different types of things such as papers, books, clothing, money, and even your phone are exactly where you put them. There’s no need to compare things. They are where they are and that’s all. You can put things in this type of tree when the placement needs to be completely under your control. It’s the placement that determines the parent child relationship. By placing your phone on your desk, the phone becomes a child of the desk. There’s order. But it’s only understandable to you or to the person using your app. Usually, this type of tree is creative and allows an unlimited number of items to be placed under each node. I say under because usually when drawing a diagram of what a tree looks like, child items are drawn under the parent. Your code will need to have some way to figure out the type of each item but the tree itself will need to treat everything the same. How can you treat everything the same? How can you treat all the things in a messy room as if they were all the same? Simple. Just find some concept that everything shares. For the messy room, and all the things I listed, about the only thing I can think of that all these things share is that they’re all physical objects. Money might be an abstract concept but a coin or a dollar bill is a physical thing. Everything in that messy room has a name, a color, a size, a weight, and materials that it’s made from. When you want to store a variety of items like this, then you can have a class representing physical objects and it’s this class that the tree knows about. For an outline app, each item is just a block of text. Or if you want a more full-featured outlining app where the user can add links to web pages, audio notes, pictures, etc. then you’ll want to do something similar to the messy room and create a class to represent the general concept of an outline node. It’s up to you then if you want to use inheritance or some other mechanism to represent the specific types of content that can be added to the outline. The point for all these is that the tree holds a structure that gets built with few or no rules at all. When I was first learning about programming and e