www.delorie.com/gnu/docs/avl/libavl_111.html search
GNU libavl 2.0.1

 [ < ] [ > ] [ << ] [ Up ] [ >> ] [Top] [Contents] [Index] [ ? ]

# 7. 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:

 ```<@xref{\NODE\, , License.>,1} #ifndef RB_H #define @cindex RB_H macro RB_H 1 #include <stddef.h> <@xref{\NODE\, , Table types; tbl =>.> rb,14} <@xref{\NODE\, , RB maximum height.>,195} <@xref{\NODE\, , BST table structure; bst =>.> rb,27} <@xref{\NODE\, , RB node structure.>,194} <@xref{\NODE\, , BST traverser structure; bst =>.> rb,61} <@xref{\NODE\, , Table function prototypes; tbl =>.> rb,15} #endif /* rb.h */ ```

 ```<@xref{\NODE\, , License.>,1} #include <assert.h> #include <stdio.h> #include <stdlib.h> #include <string.h> #include "rb.h" <@xref{\NODE\, , RB functions.>,196} ```