Introduction to Trees
Summary and Introduction to Trees
A major skill in computer programming is understanding how to work with data. The simplest way to store data is in a simple variable:
int my_int = 3;
A slightly more complicated storage mechanism is the array:
Trees are simply another way of arranging and storing the data. Trees get their name because the general shape of the structure (if you draw it out) resembles a tree. All of the elements in the tree are called nodes. Just like a family tree, there is one node from which all the other nodes descend. This is the root node. Each of the descendents can also have descendents. In other words, each child of the root can be seen as the root of its own tree. It is in this way that a tree is naturally recursive. This means that at each level, we find essentially the same structure. If you pick any node in the tree and consider from it down, you still have a tree. Even if you pick a leaf, you have a tree, albeit a branchless one.
The next question is when and why you might want to use such a structure. There are situations in which the data itself can naturally be thought of as a tree. Such an example is a family genealogy, where each person is always a child of someone else and has the potential to have children. In addition, there are many situations where trees make implementing certain algorithms very simple. In the section on binary search trees we will see such an application. The fact that the data in a tree is arranged hierarchically makes it easier (quicker in terms of the number of branches between the root and any other node) to access nodes. This makes a tree a very appropriate structure for holding data that must be searched often.