|6 Red-Black Trees|
The last chapter saw us implementing a library for one particular type of balanced trees. Red-black trees were invented by R. Bayer and studied at length by L. J. Guibas and R. Sedgewick. This chapter will implement a library for another kind of balanced tree, called a red-black tree (see red-black tree). For brevity, we'll often abbreviate “red-black” to RB.
Insertion and deletion operations on red-black trees are more complex to describe or to code than the same operations on AVL trees. Red-black trees also have a higher maximum height than AVL trees for a given number of nodes. The primary advantage of red-black trees is that, in AVL trees, deleting one node from a tree containing n nodes may require log2 (n) rotations, but deletion in a red-black tree never requires more than three rotations.
The functions for RB trees in this chapter are analogous to those that we developed for use with AVL trees in the previous chapter. Here's an outline of the red-black code:
192. <rb.h 192> = <License 1> #ifndef RB_H #define RB_H 1 #include <stddef.h> <Table types; tbl => rb 14> <RB maximum height 195> <BST table structure; bst => rb 27> <RB node structure 194> <BST traverser structure; bst => rb 61> <Table function prototypes; tbl => rb 15> #endif /* rb.h */
193. <rb.c 193> = <License 1> #include <assert.h> #include <stdio.h> #include <stdlib.h> #include <string.h> #include "rb.h" <RB functions 196>
See also: [Cormen 1990], chapter 14, “Chapter notes.”