What other things can trees help you to solve? 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 second group of trees have a well defined structure. Putting something into this kind of tree is like returning a book to the library. Does the library let you put the book back on the shelves anywhere you think is a good place? No. In fact, you don’t get to put the book on the shelves at all. The librarians do this for you. When you place an item into this type of tree, it’s your code, or the tree code, that does the work of figuring out where the item will eventually be located in the tree. The tree might even need to be reorganized to make room for the new item.
Listen to the full episode 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 to to Become a Patron. For a dollar a month, you can get access to extra podcast episodes.