next up previous contents
Next: Binary trees Up: Binary search trees Previous: Search keys   Contents

Trees

A tree consists of nodes and branches. An example is given in Figure 2.1. Nodes are usually labelled with a data item (typically a search key).

Figure 2.1: A tree
\begin{figure}\begin{center}
\epsfig{file=figs/fig1-1.eps} \end{center}\end{figure}

In our examples, we will use nodes labelled by integers, but one could just as easily have chosen something else, e.g. strings of characters. We also refer to the label of a node as its value. For the moment, we assume that each possible value occurs at most once in the tree. It is easiest to represent trees pictorially. A formal definition will be given later.

In order to talk about trees it is convenient to have some terminology: We demand that there is always a unique `top level' node, the root. (Our trees are typically thought of as upside-down with the root forming the top level.) Given a node, every node on the next level `down' which is connected to the given node via a branch is a child of that node. In the example, the children of $ 8$ are $ 3$ and $ 11$. Conversely, the node (there's at most one) on the level above connected to the given node (via a branch) is its parent. So $ 11$ is the parent of $ 9$ and $ 14$. Nodes that have the same parent are known as siblings--siblings are always on the same level.

If a node is the child of a child of ... of a another node then we say that the first node is a descendent of the second node. Conversely, the second node is an ancestor of the first node. Nodes which do not have any children are known as leafs. (Think of the upside-down tree.)

A path is a sequence of connected branches from one node to another (branching is not allowed). Trees have the property that for every node there is a unique path connecting it with the root. (In fact, that is one possible definition of a tree.) The depth or level of a node is given by the length of this path. Hence the root has level 0, its children have level $ 1$, etc. The maximal length of a path in a tree is also called the height of the tree. A path of maximal length always goes from the root to a leaf. The tree in the picture above has height $ 3$, a tree consisting just of one node has height 0, and we define (somewhat artificially) the empty tree to have height $ -1$. The size of a tree on the other hand is given by the number of nodes it contains. The size of the tree in the picture is $ 11$.


next up previous contents
Next: Binary trees Up: Binary search trees Previous: Search keys   Contents
Martin Escardo 2005-01-11