Red-black trees were first introduced by Guibas and Sedgewick [38]. Despite their high implementation complexity they are found in some of the most commonly used libraries and applications. Most algorithms and data structures textbooks discuss some variant of red-black trees.
Andersson [6] describes a left-leaning version of balanced trees
that is similar to red-black trees but has the additional constraint
that any node has at most one red child.  This implies that these trees
simulate 2-3 trees rather than 2-4 trees.  They are significantly simpler,
though, than the 
 structure presented in this chapter.
 structure presented in this chapter.
Sedgewick [64] describes two versions of left-leaning red-black trees. These use recursion along with a simulation of top-down splitting and merging in 2-4 trees. The combination of these two techniques makes for particularly short and elegant code.
A related, and older, data structure is the AVL tree [3].
AVL trees are height-balanced:
At each node  , the height
of the subtree rooted at
, the height
of the subtree rooted at 
 and the subtree rooted at
 and the subtree rooted at 
 differ by at most one.  It follows immediately that, if
differ by at most one.  It follows immediately that, if  is the
minimum number of leaves in a tree of height
 is the
minimum number of leaves in a tree of height  , then
, then  obeys the
Fibonacci recurrence
 obeys the
Fibonacci recurrence
 
 and
 and  .  This means
.  This means  is approximately
 is approximately
 , where
, where 
 is the
golden ratio.  (More precisely,
 is the
golden ratio.  (More precisely, 
 .)
Arguing as in the proof of Lemma 9.1, this implies
.)
Arguing as in the proof of Lemma 9.1, this implies
 
 and
 and 
 operations
by walking back up the path to the root and performing a rebalancing
operation at each node
 operations
by walking back up the path to the root and performing a rebalancing
operation at each node 
 where the height of
 where the height of 
 's left and right
subtrees differ by two.  See Figure 9.10.
's left and right
subtrees differ by two.  See Figure 9.10.
| ![\includegraphics[scale=0.90909]{figs/avl-rebalance}](img4494.png)  | 
Andersson's variant of red-black trees, Sedgewick's variant of red-black
trees, and AVL trees are all simpler to implement than the 
 structure defined here.  Unfortunately, none of them can guarantee that
the amortized time spent rebalancing is
structure defined here.  Unfortunately, none of them can guarantee that
the amortized time spent rebalancing is  per update.  In particular,
there is no analogue of Theorem 9.2 for those structures.
 per update.  In particular,
there is no analogue of Theorem 9.2 for those structures.
 , there are red-black
  trees with
, there are red-black
  trees with 
 nodes that have height
 nodes that have height 
 .
.
 and
 and 
 .  What do
  these operations do to the underlying 2-4 tree that is being simulated
  by the red-black tree?
.  What do
  these operations do to the underlying 2-4 tree that is being simulated
  by the red-black tree?
 , there exist sequences
  of
, there exist sequences
  of 
 and
 and 
 operations that lead to red-black trees
  with
 operations that lead to red-black trees
  with 
 nodes that have height
 nodes that have height 
 .
.
 in the
 in the 
 implementation
  perform the assignment
 implementation
  perform the assignment 
 ?  Shouldn't this already
  be done by the call to
?  Shouldn't this already
  be done by the call to 
 ?
?
 , has
, has 
 leaves and
 leaves and 
 internal nodes.
 internal nodes.
  
 , as a function of
, as a function of 
 ?
?
 , as a function of
, as a function of 
 ?
?
 is a red-black tree that represents
 is a red-black tree that represents  , then how many red
     nodes does
, then how many red
     nodes does  have?
 have?
  
 nodes and a
  height of at most
 nodes and a
  height of at most 
 .  Is it always possible to colour the
  nodes red and black so that the tree satisfies the black-height and
  no-red-edge properties?  If so, can it also be made to satisfy the
  left-leaning property?
.  Is it always possible to colour the
  nodes red and black so that the tree satisfies the black-height and
  no-red-edge properties?  If so, can it also be made to satisfy the
  left-leaning property?
 and
 and  that have the
  same black height,
 that have the
  same black height,  , and such that the largest key in
, and such that the largest key in  is smaller
  than the smallest key in
 is smaller
  than the smallest key in  .  Show how to merge
.  Show how to merge  and
 and  into a single red-black tree in
  into a single red-black tree in  time.
 time.
 and
 and  have different black heights,
 have different black heights, 
 .
  The running-time should be
.
  The running-time should be 
 .
.
 operation, an AVL tree must perform
  at most one rebalancing operation (that involves at most two rotations;
  see Figure 9.10).  Give an example of an AVL tree and a
 operation, an AVL tree must perform
  at most one rebalancing operation (that involves at most two rotations;
  see Figure 9.10).  Give an example of an AVL tree and a
  
 operation on that tree that requires on the order of
 operation on that tree that requires on the order of 
 rebalancing operations.
 rebalancing operations.
 class that implements AVL trees as described
  above.  Compare its performance to that of the
 class that implements AVL trees as described
  above.  Compare its performance to that of the 
 implementation.   Which implementation has a faster
  implementation.   Which implementation has a faster 
 operation?
 operation?
 ,
, 
 , and
, and 
 for the
 for the 
 implemeentations
 implemeentations 
 ,
,
  
 ,
, 
 , and
, and 
 .  Be sure to include
  multiple test scenarios, including cases where the data is random,
  already sorted, is removed in random order, is removed in sorted order,
  and so on.
.  Be sure to include
  multiple test scenarios, including cases where the data is random,
  already sorted, is removed in random order, is removed in sorted order,
  and so on.