Search Menu


Recursion with Trees

page 1 of 3

Note: This guide is not intended to be an introduction to trees. If you have not yet learned about trees, please see the SparkNotes guide to trees. This section will only briefly review the basic concepts of trees.

What are trees?

A tree is recursive data type. What does this mean? Just as a recursive function makes calls to itself, a recursive data type has references to itself.

Think about this. You are a person. You have all the attributes of being a person. And yet the mere matter that makes you up is not all that determines who you are. For one thing, you have friends. If someone asks you who you know, you could easily rattle off a list of names of your friends. Each of those friends you name is a person in and of themselves. In other words, part of being a person is that you have references to other people, pointers if you will.

A tree is similar. It is a defined data type like any other defined data type. It is a compound data type that includes whatever information the programmer would like it to incorporate. If the tree were a tree of people, each node in the tree might contain a string for a person's name, an integer for his age, a string for his address, etc. In addition, however, each node in the tree would contain pointers to other trees. If one was creating a tree of integers, it might look like the following:

typedef struct _tree_t_ {
	int data;
	struct _tree_t_ *left;
	struct _tree_t_ *right;
} tree_t;

Notice the lines struct _tree_t_ *left and struct _tree_t_ *right;. The definition of a tree_t contains fields that point to instances of the same type. Why are they struct _tree_t_ *left and struct _tree_t_ *right instead of what seems to be more reasonable, tree_t *left and tree_t *right? At the point in compilation that the left and right pointers are declared, the tree_t structure has not been completely defined; the compiler doesn't know it exists, or at least doesn't know what it refers to. As such, we use the struct _tree_t_ name to refer to the structure while still inside it.

Some terminology. A single instance of a tree data structure is often referred to as a node. The nodes that a node points to are called children. A node that points to another node is referred to as the child node's parent. If a node has no parent, it is referred to as the root of the tree. A node that has children is referred to as an internal node, while a node that has no children is referred to as a leaf node.