Trees aren’t just for forests.

Randy Taylor
2 min readDec 20, 2020

In this blog I have been writing about Nodes a lot. This should indicate to you the reader they must be pretty important in software. There are lots of Data Structures and a Node is just one way to represent this idea in code. Once again here we are going to be talking about Nodes. So lets refresh our Node idea. We will create a Node class that will effectively be our leggo we will use to craft our leggo castle.

Node in python

This small piece of code follows the Single Responsibility Principal which helps explains why we have seen it so much. Let me show you linkedLists, and again in ruby, and graphs all use Nodes. This level of simplicity allows us to be very DRY. Hence we can plug it in different software and have it work.

Today we are goin to talk about trees. These data structures remind me on linked lists quite a bit. The idea is there will be a Root node which has no parents. Then this Root will have children. There is no limit to the number of children a tree can have however two is common for a binary search tree. Bi, meaning two, search trees have a few rules to follow so we can work with them.

  1. They are made of Nodes
  2. They have pointers to other Nodes
  3. The Left pointer has a lesser Node value
  4. The Right pointer has a higher Node value
  5. Each child node needs to have capability to have children

We can represent this in ASCII art.

   Node(100) 
/ \
Node(99) Node(101)

This is the general idea of Trees in Software. Lets update our Node to make it useful to us. We can see the idea of the Left Node has a lesser value and the Right node has a greater value could work if we pick a key. Lets think about a number of keys to organize the data in Left Vs Right. We could have a date key for a news article, an SSN for a person in a government organization, a Medical Record Number key for a hospital. By using a key we can store our information in a tree which will point to something more meaningful.

Node in python with the added key

Number 5 gives it the possibility of recursion.

Trees can be used to model decision making in Machine learning where you give the link a probability value.

--

--