In this section we will cover the most common way to implement a tree in C. This most common method involves defining a new struct and a new type, as well as making use of pointers.

As was mentioned in the introduction, each node in the tree will point to its children, which are also nodes. In other words, a node and its children are all the same type. With this in mind, when we define the type, we will want it to have children that are also of the same type we are defining. In C, however, it is not possible to include a reference to a given type in the definition of that same type. Instead, when we are defining the type to be a structure, we must name the structure which we can then reference with a pointer (structure pointers can be used in their own definitions in C). A down-side to structures is that you need to define them exactly, which means that you need to decide how many children each node can have. The most common number is two, which defines a binary tree. The final thing to decide before you go ahead and define the tree type is what sort of data each node is going to contain (don't forget that the whole reason we need trees is to structure data). Let's assume that all of our nodes simply need to contain an integer. We will discuss afterward how to extend our new type to include other data as well.

typedef struct _tree { int data; struct _tree *left, *right; } tree_t;

What we have done here is created a new type called tree_t. We can make variables that are of type tree_t the same way that we can make variables that are integers. So

tree_t my_tree;

creates a static variable that is a tree_t. We can assign data into it as follows:

my_tree.data = 42;

The two fields left and right require some further explanation. Because they are pointers, they store the address of another variable, namely another tree_t variable. In the following example we have three tree_t variables and want to relate them as their names suggest. We will use the & operator to get the address of the variables.

tree_t my_tree, left_child, right_child; my_tree.left = &left_child; my_tree.right = &right_child;

So now my_tree.left->data is the same variable as left_child.data.

If you want to include more data in each node than just an integer, you can simply add whatever other fields you desire to the struct where the data integer is.

That is the basic structure / pointer implementation of trees. In next topic we discuss how you might write functions to ease working with this structure.