5 - AVL Trees
Published online by Cambridge University Press: 10 November 2016
Summary
Bounding the Depth of Trees
We saw earlier that binary search trees are a good compromise between sequentially allocated arrays, which allow direct access, but are not updated easily, and linked lists, that support inserts and deletes in constant time, but cannot be searched efficiently. There is, however, still a major challenge left to be dealt with: the shape of the tree, and in particular its depth, on which ultimately all update complexities depend, is a function of the order in which the elements are processed. The same set of elements, when given in different orders, may produce completely different trees (see Figure 5.1).
Yet, we have generally no influence on the order the elements are presented, so we might get the worst case scenario, in which the depth of the tree is. This is unacceptable, as itwould also imply search and update times of the same order. Nothing has then be gained by passing from a linear linked list to a tree structure. What we would like is a tree structure for n elements in which the update times are guaranteed to be as close as possible to the optimal, which is log2n. Imposing, however, that the tree should be completely balanced, which yields the log2n bound on the depth, seems to be too restrictive: itwould require reshaping the tree constantly.
An elegant tradeoff has been suggested by Georgy Adelson-Velsky and Evgenii Landis and is known by the acronym of its inventors as AVL trees. The idea is to define a condition that on one hand is easily enforceable, but on the other hand yields trees that are almost balanced. Let us recall some definitions.
Background Concept: Depth of a Tree
The depth of a tree, sometimes also called its height, is the number of edges one has to traverse on the path from the root to a most distant leaf. The depths of the trees in Figure 5.1 are 1, 2 and 2, respectively. It is the number of levels minus 1, hence a tree consisting of a single node (the root is a leaf) has depth 0, and it is convenient to define the depth of an empty tree as −1.
- Type
- Chapter
- Information
- Basic Concepts in Data Structures , pp. 65 - 82Publisher: Cambridge University PressPrint publication year: 2016