Skip to main content

Section 32.8 Red-Black Trees

Our next self-balancing tree is the Red-Black Tree, which is another type of balanced binary search tree. Like AVL trees, Red-Black trees maintain balance through rotations during insertions and deletions, but they use a different set of rules based on coloring nodes red or black to ensure balance.
In a Red-Black tree, each node is colored either red or black, and the tree must satisfy the following properties:
  1. The root node is always black.
  2. Null pointers are considered black.
  3. If a red node has children, then both children must be black (no red children of red parents).
  4. Every path from a node to its descendant leaves must have the same number of black nodes.
Below is a sample of a Red-Black tree. Note that the root node is black, red nodes have black children, and every path from the root to a null leaf has the same number of black nodes.
A Red-Black tree with black and red nodes. The root node is black. Each red node has black children. Every path from the root to a leaf has the same number of black nodes.πŸ”—
Figure 32.8.1. A Red-Black tree.
We often will not draw the null leaves in Red-Black trees, but it is important to remember that if we ever need to count black nodes along a path we must include those null leaves as black nodes.

Activity 32.8.1. Red-Black Trees.

Try using the Insert Random Values button a few times to build up a red-black tree. Click and drag to move the diagram around and examine the overall structure.
Then reset it and try generating another. Notice that although the trees are not perfectly balanced, it is never very far unbalanced.
If you want to see the null leaves, you can check the Show Null Leaves checkbox.

Instructions.

When the > button is highlighted, an animation is prepared to run. Use the Step button to step through the animation one step at a time.
When no animation is in progress, you can use the data controls to start a new animation.
You can click and drag on the animation area to pan around. Use the Ctrl + mouse wheel (or touchpad scroll gesture) while over the animation area to zoom in and out.
  • Core Controls.
    • << : Skip to the beginning of the animation.
    • < : Step backwards one step in the animation.
    • > : Step forward one step in the animation.
    • >> : Skip to the end of the animation.
    • Auto Step Speed Set to anything other than off to automatically step through the animation at the selected speed.
    • Zoom Set the zoom level. You can also use Ctrl + Mouse Wheel to zoom in and out.
  • Data Controls.
    • Value Use this area to enter a value when inserting, finding, deleting, etc...
The balancing process for Red-Black trees during insertions and deletions is more complex than for AVL trees, involving a series of color changes and rotations to restore the Red-Black properties. One way to understand the process is to think of each black node as representing a node in a B-tree of order 4. Red nodes are just part of the β€œgroup” that the black node represents.
A single black node is a B-tree node with 1 value and 2 children. A black node with a red child is a B-tree node with 2 values and 3 children. A black node with two red children is a B-tree node with 3 values and 4 children.πŸ”—
Figure 32.8.2. Groups of Red-Black nodes visualized as a B-tree nodes of degree 4. Red nodes are part of the group that the black node represents.
In this way, the entire Red-Black tree can be viewed as a B-tree of degree 4, where each black node and its red children form a single B-tree node.
A Red-Black tree with black and red nodes is shown alongside its equivalent B-tree of degree 4 representation. Each black node and its red children correspond to a single B-tree node.πŸ”—
Figure 32.8.3. A Red-Black tree visualized as a B-tree of degree 4.
When we insert in a red black tree, we start by inserting the new node as we would in a regular binary search tree, coloring the new node red. Then, we check for back up the tree to see if any of the Red-Black properties have been violated. If a violation is found, we perform a series of color changes and rotations to restore the properties.
  • If the parent of a red node is red, we have a violation. We then look at the uncle node (the sibling of the parent):
    • If the uncle is black (or null), that means we have a group of just three nodes. That can be fixed with rotations and recoloring:
      • Do a single or double rotation so that the median value of the three nodes becomes the new parent of the group.
      • Recolor the new parent to black and the two children to red.
      This case corresponds to stealing a value from a sibling B-tree node.
    • If the uncle is red, that means we are in a logical group of 4 nodes (one black with two red children and a new red child for one of those). We fix this by recoloring the parent and uncle to black and the grandparent to red.
      This corresponds to splitting a B-tree node and pushing a value up to the parent node.
  • We then repeat the process from the grandparent node if necessary.
  • If the root becomes red, we recolor it to black to maintain the Red-Black properties.

Activity 32.8.2. Red-Black Tree Insertion.

First try inserting 200 (type 200 into the Value text field and press Insert). Then use the > button to step through the insertion process. This new red node requires no adjustments as its parent is black.
Then reset and try adding 600. This adds a red node with a red parent, causing a violation that requires rotations and recoloring to fix. 600 requires a single rotation to fix. If you instead add 480, this will cause a zig-zag pattern requiring a double rotation.
Then add 950. This is a red child of a red parent (943) and a red uncle (800). Now the group rooted at 812 is overfull. So we β€œsplit” it by making the two red children (800 and 943) black and the grandparent (812) red. This logically adds the 812 node to the group centered at 726.
Finally, add 1. That should cause a similar group of 4 nodes that need to be split. 8 will become red, making it a part of the group that currently contains 50 and 134). Because that group now has a red child of a red node, but only has three total nodes, we can fix with a rotation.

Instructions.

When the > button is highlighted, an animation is prepared to run. Use the Step button to step through the animation one step at a time.
When no animation is in progress, you can use the data controls to start a new animation.
You can click and drag on the animation area to pan around. Use the Ctrl + mouse wheel (or touchpad scroll gesture) while over the animation area to zoom in and out.
  • Core Controls.
    • << : Skip to the beginning of the animation.
    • < : Step backwards one step in the animation.
    • > : Step forward one step in the animation.
    • >> : Skip to the end of the animation.
    • Auto Step Speed Set to anything other than off to automatically step through the animation at the selected speed.
    • Zoom Set the zoom level. You can also use Ctrl + Mouse Wheel to zoom in and out.
  • Data Controls.
    • Value Use this area to enter a value when inserting, finding, deleting, etc...
Similar rules govern removal of nodes from a Red-Black tree, however, the process is more complex. To see an example, reset the animation above and remove 134. This will end up removing a black node, which causes a violation of the property that every path to a null pointer must involve the same number of black nodes. The fix involves a series of color changes and rotations as we backtrack up the tree to restore balance.
Exhaustive treatment of the rules for a Red-Black tree are available in many online resources. Our key takeaway is that careful application of these rules can ensure that our tree follows the Red-Black properties as it is built and modified.

Checkpoint 32.8.1.

Select all the listings that represent a logical β€œgroup” that corresponds to a B-tree node:
837 is root and is black. It has left child 676 (red) and right child 886 (black). 676 has a left child 268 (black) and a right child 737 (black). 268 has a left child 115 (red) and a right child 602 (red). 737 has a right child 765 (red). 886 has a left child 853 (red).πŸ”—
  • [115, 268, 602]
  • [268, 676, 737]
  • [676, 837]
  • [676, 837, 886]
  • [737, 765]
  • [853]

Checkpoint 32.8.2.

In the Red-Black tree shown below, what would happen if we inserted 700?
837 is root and is black. It has left child 676 (red) and right child 886 (black). 676 has a left child 268 (black) and a right child 737 (black). 268 has a left child 115 (red) and a right child 602 (red). 737 has a right child 765 (red). 886 has a left child 853 (red).πŸ”—
  • It would be inserted as a red node as the left child of 737.
  • It would be inserted as a black node as the left child of 737.
  • 737 would get recolored red.
  • 737 would rotate right.
  • 765 would get recolored black.

Checkpoint 32.8.3.

In the Red-Black tree shown below, what would happen if we inserted 800?
837 is root and is black. It has left child 676 (red) and right child 886 (black). 676 has a left child 268 (black) and a right child 737 (black). 268 has a left child 115 (red) and a right child 602 (red). 737 has a right child 765 (red). 886 has a left child 853 (red).πŸ”—
  • It would be inserted as a red node as the right child of 765.
  • It would be inserted as a black node as the left child of 765.
  • 737 would get recolored red.
  • 737 would rotate left.
  • 765 would get recolored black.

Checkpoint 32.8.4.

In the Red-Black tree shown below, which insertions would result in a zig-zag double rotation to fix an issue caused?
837 is root and is black. It has left child 676 (red) and right child 886 (black). 676 has a left child 268 (black) and a right child 737 (black). 268 has a left child 115 (red) and a right child 602 (red). 737 has a right child 765 (red). 886 has a left child 853 (red).πŸ”—
  • 100
  • 750
  • 780
  • 860
  • 840

Checkpoint 32.8.5.

In the Red-Black tree shown below, if we insert 650, what things will happen?
837 is root and is black. It has left child 676 (red) and right child 886 (black). 676 has a left child 268 (black) and a right child 737 (black). 268 has a left child 115 (red) and a right child 602 (red). 737 has a right child 765 (red). 886 has a left child 853 (red).πŸ”—
  • 650 is inserted as a red node as the right child of 602.
  • 650 is inserted as a black node as the right child of 602.
  • 115 would become black.
  • 268 would become red.
  • 268 would rotate left.
  • 602 would become black.
  • 676 would become black.
  • 837 would rotate right and become red.
Hint.
Fixing the initial problem creates a new violation of the Red-Black tree properties.
You have attempted of activities on this page.