Skip to main content

Section 32.9 Red-Black Tree Performance

We can reason about the performance of Red-Black trees in a similar way to how we reasoned about AVL trees, by focusing on the worst case height of the tree. We will only sketch out the argument here, but the key idea is that the properties of a Red-Black tree ensure that the longest path from the root to a leaf is no more than twice the length of the shortest path. This means that the height of a Red-Black tree with \(n\) nodes is \(O(\log n)\text{,}\) which guarantees logarithmic time complexity for search, insertion, and deletion operations.
To store all the nodes in a Red-Black tree using just black nodes, we would need to use a perfectly balanced, complete binary tree of black nodes. Each path to a leaf must go through the same number of black nodes, so we canโ€™t have any branches that are longer than others. As we established with a binary heap, a perfectly balanced binary tree with \(n\) nodes has a height of \(O(\log n)\text{.}\) We will call this the โ€œmax black heightโ€ of a tree with \(n\) nodes.
The root node is black and has two black children. Each of those children has two black children, which are the leaves. Every path from the root to a leaf traverses 2 black nodes.๐Ÿ”—
Figure 32.9.1. An all black node Red-Black tree must be perfectly balanced. Every path from root to a leaf in this tree traverses 2 black nodes.
Now let us retain \(n\) nodes, but change some of the black nodes to red nodes. When we do so, we can now have some paths that are longer than others. However, we can only add one red node after each black node while retaining the Red-Black properties. So, the longest possible path would look like B->R->B->R->B->R. The shortest path would look like B->B->B. This means that the longest path is no more than twice the length of the shortest path.
The root node is black and so is its left child. The right child of the root (10) is red. Both of 10โ€™s children are black. The right child of 10 has two red children, which are the leaves.๐Ÿ”—
Figure 32.9.2. A Red-Black tree with 6 nodes that is as โ€œright leaningโ€ as possible. The shortest path (down the left) side, traverses 2 black nodes. We have added as many red nodes as possible to the far right branch. The path has a length of 4 but still only traverses 2 black nodes.
โ€œStealingโ€ some of the black nodes and turning them into red nodes canโ€™t possibly increase the black height of the tree, so the shortest path (an all black path) still has length that is at most \(O(\log n)\text{.}\) And because the longest path is at most twice that of the shortest path, the height of a Red-Black tree with \(n\) nodes is \(O(2 \log n) = O(\log n)\text{.}\)

Insight 32.9.1.

The height of a Red-Black tree is \(O(\log{N})\text{,}\) guaranteeing logarithmic time complexity for search, insertion, and deletion operations.

Aside

In comparing the Red Black tree to the AVL tree, we see that both have height \(O(\log n)\text{,}\) but the AVL tree is more strictly balanced. It limits the height to about \(1.44 \log n\text{,}\) while the Red-Black tree allows height up to about \(2 \log n\text{.}\) This means that AVL trees may provide slightly faster lookups on average, since they are more balanced. However, Red-Black trees often provide faster insertion and deletion operations because they require fewer rotations on average to maintain balance.
The choice between using an AVL tree or a Red-Black tree thus depends on the specific requirements of the application, such as the frequency of insertions and deletions versus lookups. The C++ std::map and std::set containers do not mandate a particular tree type, but do require guaranteed logarithmic time complexity for insertions, deletions, and lookups. To optimize for mixed use, implementation typically use Red-Black trees. (You may be able to see this by using the debugger to inspect the internal structure of these containers.)
Low level optimizations in both AVL and Red-Black trees can avoid storing extra information (color or balance factor) by using techniques such as bit-packing or pointer tagging.

Checkpoint 32.9.1.

Which are the accurate statements about the performance of Red-Black trees vs AVL trees?
  • AVL trees have a more strict balance, leading to faster lookups on average.
  • Both provide a guarantee of O(log n) time complexity for search, insertion, and deletion operations.
  • An AVL will more frequently need to do rotations after insertions and deletions.
  • Red-Black trees have a more strict balance, leading to faster lookups on average.
  • Given the same number of values, an AVL tree is potentially taller than a Red-Black tree.
You have attempted of activities on this page.