www.delorie.com/gnu/docs/avl/libavl_88.html | search |
Buy GNU books! | |
[ < ] | [ > ] | [ << ] | [ Up ] | [ >> ] | [Top] | [Contents] | [Index] | [ ? ] |
In the last chapter, we designed and implemented a table ADT using binary search trees. We were interested in binary trees from the beginning because of their promise of speed compared to linear lists.
But we only get these speed improvements if our binary trees are arranged more or less optimally, with the tree's height as small as possible. If we insert and delete items in the tree in random order, then chances are that we'll come pretty close to this optimal tree.(9)
In "pathological" cases, search within binary search trees can be as slow as sequential search, or even slower when the extra bookkeeping needed for a binary tree is taken into account. For example, after inserting items into a BST in sorted order, we get something like the vines on the left and the right below. The BST in the middle below illustrates a more unusual case, a "zig-zag" BST that results from inserting items from alternating ends of an ordered list.
Unfortunately, these pathological cases can easily come up in practice, because sorted data in the input to a program is common. We could periodically balance the tree using some heuristic to detect that it is "too tall". In the last chapter, in fact, we used a weak version of this idea, rebalancing when a stack overflow force it. We could abandon the idea of a binary search tree, using some other data structure. Finally, we could adopt some modifications to binary search trees that prevent the pathological case from occurring.
For the remainder of this book, we're only interested in the latter choice. We'll look at two sets of rules that, when applied to the basic structure of a binary search tree, ensure that the tree's height is kept within a constant factor of the minimum value. Although this is not as good as keeping the BST's height at its minimum, it comes pretty close, and the required operations are much faster. A tree arranged to rules such as these is called a balanced tree (see balanced tree). The operations used for minimizing tree height are said to rebalance (see rebalance) the tree, even though this is different from the sort of rebalancing we did in the previous chapter, and are said to maintain the tree's "balance."
A balanced tree arranged according to the first set of rebalancing rules that we'll examine is called an AVL tree (see AVL tree), after its inventors, G. M. Adel'son-Vel'skii< and E. M. Landis. AVL trees are the subject of this chapter, and the next chapter will discuss red-black trees, another type of balanced tree.
In the following sections, we'll construct a table implementation based on AVL trees. Here's an outline of the AVL code:
<@xref{\NODE\, , License.>,1} #ifndef AVL_H #define @cindex AVL_H macro AVL_H 1 #include <stddef.h> <@xref{\NODE\, , Table types; tbl =>.> avl,14} <@xref{\NODE\, , BST maximum height; bst =>.> avl,28} <@xref{\NODE\, , BST table structure; bst =>.> avl,27} <@xref{\NODE\, , AVL node structure.>,144} <@xref{\NODE\, , BST traverser structure; bst =>.> avl,61} <@xref{\NODE\, , Table function prototypes; tbl =>.> avl,15} #endif /* avl.h */ |
<@xref{\NODE\, , License.>,1} #include <assert.h> #include <stdio.h> #include <stdlib.h> #include <string.h> #include "avl.h" <@xref{\NODE\, , AVL functions.>,145} |
6.1 Balancing Rule 6.2 Data Types 6.3 Operations 6.4 Insertion 6.5 Deletion 6.6 Traversal 6.7 Copying 6.8 Testing
See also: [ Knuth 1998b], sections 6.2.2 and 6.2.3; [ Cormen 1990], section 13.4.
[ < ] | [ > ] | [ << ] | [ Up ] | [ >> ] | [Top] | [Contents] | [Index] | [ ? ] |
webmaster donations bookstore | delorie software privacy |
Copyright © 2003 by The Free Software Foundation | Updated Jun 2003 |