Skip to main content Accessibility help
  • Print publication year: 2016
  • Online publication date: November 2016

4 - Trees


Allowing Multiple Successors

The graphs of the previous chapter were a generalization of the linear lists to structures without order. We now turn to another generalization, still keeping some order, but a more relaxed one. The main motivation may be the fact that a linear list is ultimately not able to support both efficient searches and updates. To get the possibility to perform a binary search, the elements have to be stored sequentially, but then inserts and deletes may require steps. Using a linked list, on the other hand, may reduce the update time to O(1), but at the price of losing the ability to locate the middle elements needed for the binary search, so the time complexity can be.

The problem is that the constraint of every record having only a single successor, as depicted on the left-hand side of Figure 4.1, might be too restrictive. This chapter explores the structures one obtains by allowing each element to be directly followed by possibly more than one other element, as on the righthand side of Figure 4.1. Because of the repeated branching, such structures are known as trees. Formally, the definition of a tree is general enough to encompass a broad family of recursive structures, and we shall deal with several such families in this and the following chapters.

Definition 4.1. A tree is a set of elements called nodes for which:

(i) there is a special element in the set, called the root;

(ii) the other elements can be partitioned into m subsets, each of which is a tree.

Actually, almost any set can then be considered to be a tree. Refer for example to the nodes in the left part of Figure 4.2, 5 might be chosen as the root, and the others can be partitioned into m = 3 subsets, as shown. The convention is to draw such a tree in levels top down, the root being placed on the top in level 0, and the m subtrees following in some order below the root, starting with the next level. This yields the equivalent tree on the right side of Figure 4.2. Accordingly, the vocabulary of genealogic trees has been adopted, for example, 3 is the parent of 4, and 3, 9, and 10 are siblings and the children of 5.