In this chapter, we present red-black trees, a version of binary search trees with logarithmic height. Red-black trees are one of the most widely used data structures. They appear as the primary search structure in many library implementations, including the Java Collections Framework and several implementations of the C++ Standard Template Library. They are also used within the Linux operating system kernel. There are several reasons for the popularity of red-black trees:
 values has height at most
 values has height at most 
 .
.
 and
 and 
 operations on a red-black tree run
   in
 operations on a red-black tree run
   in 
 worst-case time.
 worst-case time.
 or
   or 
 operation is constant.
 operation is constant.
 running times are only expected. Scapegoat trees have a guaranteed
bound on their height, but
running times are only expected. Scapegoat trees have a guaranteed
bound on their height, but 
 and
 and 
 only run in
 only run in 
 amortized time.  The third property is just icing on the cake. It
tells us that  that the time needed to add or remove an element
 amortized time.  The third property is just icing on the cake. It
tells us that  that the time needed to add or remove an element 
 is
dwarfed by the time it takes to find
 is
dwarfed by the time it takes to find 
 .9.1
.9.1
However, the nice properties of red-black trees come with a price:
implementation complexity. Maintaining a bound of 
 on the
height is not easy. It requires a careful analysis of a number of cases.
We must ensure that the implementation does exactly the right
thing in each case.  One misplaced rotation or change of colour produces
a bug that can be very difficult to understand and track down.
 on the
height is not easy. It requires a careful analysis of a number of cases.
We must ensure that the implementation does exactly the right
thing in each case.  One misplaced rotation or change of colour produces
a bug that can be very difficult to understand and track down.
Rather than jumping directly into the implementation of red-black trees, we will first provide some background on a related data structure: 2-4 trees. This will give some insight into how red-black trees were discovered and why efficiently maintaining them is even possible.