Chapter 11
Red-black Trees

11.1 Properties

A binary search tree in which

Empty subtrees of a node are treated as subtrees with roots of black color.

The relation n > 2h/2 - 1 implies the bound h < 2 log 2(n + 1).

11.2 Insertions

A discrepancy can result from a parent and a child both having a red color. The type of discrepancy is determined by the location of the node with respect to its grand parent, and the color of the sibling of the parent.

Discrepancies in which the sibling is red, are fixed by changes in color. Discrepancies in which the siblings are black, are fixed through AVL-like rotations.

Changes in color may propagate the problem up toward the root. On the other hand, at most one rotation is sufficient for fixing a discrepancy.

LLr              --Ao----
           ---     ---|-
          o-       - o -
        ||B ||     -AR|-
       ||    ||||   ||-
      ||     --o|-
     Co|    --   -
   ||| |||   -BR--
 -|||- -|||-
-- o --- o -
--CL----CR--
  |||   |||              --Ao----
           ---     ---|-
          o-       - o -
        ||B ||     -AR|-
       ||    ||||   ||-
      ||     --o|-
     Co|    --   -
   ||| |||   -BR--
 -|||- -|||-
-- o --- o -
--CL----CR--
  |||   |||
if ‘A’ is the root, then it should be repainted to black
LRr            ---oA ----
        ----       ---|-
       o-          - o -
     ||B||         -AR|-
  ||||   |||        ||-
--|o|-     ||
--   -     Co|
 -BL--   ||| |||
       -|||- --||-
      -- o --- o -
      --CL-- -CR--
        |||   |||            ---oA ----
        ----       ---|-
       o-          - o -
     ||B||         -AR|-
  ||||   |||        ||-
--|o|-     ||
--   -     Co|
 -BL--   ||| |||
       -|||- --||-
      -- o --- o -
      --CL-- -CR--
        |||   ||| if ‘A’ is the root, then it should be repainted to black
LLb              --Ao----
           ---     ---|-
          o-       - o -
        ||B ||     -AR|-
       ||    ||||   ||-
      ||     --o|-
     Co|    --   -
   ||| |||   -BR--
 -|||- -|||-
-- o --- o -
--CL----CR--
  |||   |||       -----Bo-----
     Co          Ao
  |||| ||||   |||| ||||
--|o|---|o|----o|- --o|-
--   ---   ---   - -   -
 -CL-- -CR-- -BR-- -AR--
       ----Bo ---
      --         --
   ||Co||      ||Ao|||
 -|||- -|||- --||- --||-
-- o --- o --- o - - o
--CL-- -CR-- -BR-- -AR--
  |||   |||   |||   |||
LRb            ---oA ----
        ----       ---|-
       o-          - o -
     ||B||         -AR|-
  ||||   |||        ||-
--|o|-     ||
--   -     Co|
 -BL--   ||| |||
       -|||- --||-
      -- o --- o -
      --CL-- -CR--
        |||   |||       -----Co-----
     Bo          Ao
  |||| ||||   |||| ||||
--|o|---|o|----o|- --o|-
--   ---   ---   - -   -
 -BL-- -CL-- -CR-- -AR--
       ----Co ---
      --         --
   ||Bo||      ||Ao|||
 -|||- -|||- --||- --||-
-- o --- o --- o - - o
--BL-- -CL-- -CR-- -AR--
  |||   |||   |||   |||

Discrepancies of type RRr, RLr, RRb, and RLb are handled in a similar manner.

insert 1 o1 o1
insert 2 1o-
  --
  2o
insert 3 1o||
   ||
   2o-
      o
     3
RRb discrepancy
--o2--
o1  3o  --o2--
 -   -
1o   o3
insert 4   ||o2||
 ||   ||
1o     o3--
         o
         4
RRr discrepancy
  ||o2||
 ||   ||
1o     o3--
         o
         4   ||o2||
 ||   ||
1o     o3--
         o
         4
insert 5  --2o--
--    --
o1      o3||
          o
         4 --
            o
           5
RRb discrepancy
 ||o2||
||   ||
o1   ||o4||
    o   o
    3   5  ||2o||
 |    ||
1o   --o4--
     o   o
     3   5

11.3 Deletions

Let A denote the lowest node with unbalanced subtrees. The type of discrepancy is determined by the location of the deleted node (Right or Left), the color of the sibling (black or red), the number of red children in the case of the black siblings, and and the number of grand-children in the case of red siblings.

In the case of discrepancies which result from the addition of nodes, the correction mechanism may propagate the color problem (i.e., parent and child painted red) up toward the root, and stopped on the way by a single rotation. Here, in the case of discrepancies which result from the deletion of nodes, the discrepancy of a missing black node may propagate toward the root, and stopped on the way by an application of an appropriate rotation.

Rb0         ||oA ---
      |||    --o|-
     Bo     --   -
    || ||    -AR--
 -|||- -|||-  |||
-- o --- o -
--   ---   -
 -BL-- -BR--         ||oA ---
      |||    ---|-
      o     -- o -
    |B ||    -AR|-
  |||   |||   ||-
--|o|---|o|-
--   ---   -
 -BL-- -BR--
change of color, sending the deficiency up to the root of the subtree
Rb1         ||oA ---
      |||    --o|-
     Bo     --   -
    || ||    -AR--
 -|||- -|||-  |||
-- o --- o -
--   ---   -
 -BL-- -BR--    ----oB ||
 -|o|-    |||
--   -     Ao
 -BL--    || ||
  |||  -|||- -|||-
      -- o --- o -
      --   ---   -
       -BR-- -AR--
           ---oA -----
       ----        --o|-
       oB           -   -
    ||| |||        -AR--
 -|||-    ||        |||
-- o -     ||
--   -     Co|
 -BL--   ||  |||
       -|o|- --o|-
      --   ---   -
       -CL-- -CR--
         |     |       -----Co-----
      o           o
  |||B ||||   |||A ||||
--|o|- -|o|- --o|- --o|-
--   ---   ---   - -   -
 -BL-- -CL-- -CR-- -AR--
Rb2            ---oA -----
       ----        --o|-
       oB           -   -
    ||| |||        -AR--
 -|||-    ||        |||
-- o -     ||
--   -     Co|
 -BL--   ||  |||
       -|o|- --o|-
      --   ---   -
       -CL-- -CR--
         |     |       -----Co-----
      o           o
  |||B ||||   |||A ||||
--|o|- -|o|- --o|- --o|-
--   ---   ---   - -   -
 -BL-- -CL-- -CR-- -AR--
Rr0         ||oA ---
      |||    ---|-
      o     -- o -
    |B ||    -AR|-
  |||   |||   ||-
--|o|---|o|-
--   ---   -
 -BL-- -BR--     ---oB |
 -|---    |||
-- o -      o
--BL--    |A ||
 -||-   |||   |||
      --|o|- --o|-
      --   ---   -
       -BR-- -AR--
might result in LLb discrepancy of parent and child having both the red color
Rr1            ---oA ----
        ----       ---|-
       o-          - o -
     ||B||         -AR|-
  ||||   |||        ||-
--|o|-     ||
--   -     Co|
 -BL--   ||| |||
       -|||- --||-
      -- o --- o -
      --CL-- -CR--
        |||   |||        ----Co ---
      --         --
   ||Bo||      ||Ao|||
 -|||- -|||- --||- --||-
-- o --- o --- o - - o
--BL-- -CL-- -AR-- -CR--
  |||   |||   |||   |||
             ----Ao-------
         -----           ---|-
        o                - o -
     ---B||              -AR|-
  |---    |||             ||-
-- o|-       |
--   -      |oC||
 |BL--   ||||  ||
       -|||-    |||
      -- o -      o
      --CL--    |D ||
        |||   |||   |||
            -- o-- --o--
            --   ---   -
             -DL-- -DR--          ------Do------
       ---            ---
   ----oB ||          ||Ao|||
 -|-|-    |||      --||- --||-
-- o -     Co     -- o - - o -
-|BL--    || ||    -DR-- -AR--
  |||  -|||  -|||   |||   |||
      -- o---- o -
      --   ---   -
       -CL-- -DL--
Rr2              ----Ao-------
         -----           ---|-
        o                - o -
     ---B||              -AR|-
  |---    |||             ||-
-- o|-       |
--   -      |oC||
 |BL--   ||||  ||
       -|||-    |||
      -- o -      o
      --CL--    |D ||
        |||   |||   |||
            -- o-- --o--
            --   ---   -
             -DL-- -DR--          ------Do------
       ---            ---
   ----oB ||          ||Ao|||
 -|-|-    |||      --||- --||-
-- o -     Co     -- o - - o -
-|BL--    || ||    -DR-- -AR--
  |||  -|||  -|||   |||   |||
      -- o---- o -
      --   ---   -
       -CL-- -DL--

Similar transformations apply to Lb0, Lb1, Lb2, Lr0, Lr1, and Lr2.

11.4 Demo Applet

11.5 Assignment #5 (due Fr, Oct 29)

  1. Construct a binary search tree by introducing the following keys in the given order: 4, 3, 2, 1, 11, 8, 5, 6, 7, 9, 10, 12, 13. Color the nodes at odd level with black, and the nodes at even levels with red (the root is assumed to be at level 1). Then repeatedly use the red-black transformations to transform the tree into a red-black tree, while showing all the intermediate trees being created in the process.

    In each stage, the transformation should be conducted at a discrepancy that is farthest from the root. A discrepancy is assumed to be caused by a deletion, when the numbers of black nodes in the different paths are not equal. Otherwise, it is assumed to be caused by an insertion.

  2. Can every binary search tree be transformed into a red-black tree by using only red-black tree transformations, assuming the coloring strategy of the previous problem, and that in each stage the transformation should be conducted at a discrepancy that is farthest from the root? Justify your answer.

  3. Consider the following operations.
    LL                   ----Ao--------||
              -----          --  |-
             -Bo -           --AR  -
           ---   ----        -| |--
        ----       ---||-     |||
       --          -    -
     ||Co|||        - BR -
 ||||     ||||     -|||--
--  |-   --| ||-
- CL -   - CR  -
-|  --   -|| |--
 ||||      |||  |--------Co----
--  |-         -----
- CL -           --Bo-
-| |--       -----   ---
 |||      -|--|        ---
         --   --          -
         --CR --        --Ao--
          ||||-      |---    ---|
                   --| |-    --  |-
                   - BR -   --AR  -
                   -|  --    -|  --
                    ||||      ||||
    LR                -----Ao----------||
          -----             ---  |-
        -Bo|                --AR  -
    ----   |||               -| |--
--|---       ||||             |||
-    -          ||
- BL -        ||Co||
-|||--     ||||     ||||
         --| ||-   --  |-
         - CL  -   - CR -
         -|| |--   -|  --
           |||      ||||        ---------Co---------
    ---Bo ---           ---Ao ---
--|---    ---||    --|---    ---||-
- B  -   --C  --   - C  -   --A   -
-  L -   --  L--   -  R -   --  R--
-|||--    ||||-    -|||--    -|||-
    L            ----Ao ------||
        ----        --  |-
       Bo-          - AR -
    ---   ---       -|  --
 ----|     ---||      |||
--   --   --   --
--BL --   --BR --
 -|||-     ||||-   |-------Bo---
 -- ||-       -----
--BL  -         -Ao-
 -| |--       ---  ----
  |||      -|--|    ---||-
          --   --   -    -
          --BR --   - AR -
           ||||-    -|||--

    A binary search tree is said to be splayed at a given node, if the node is moved to the root with the above, and symmetric, operations. Splay trees (applet), or or self-adjusting search trees, are trees which are splayed whenever their nodes are accessed. On the average, they are accessed and modified in O(log n) time.

    Construct a binary search tree by introducing the following keys in the given order: 7, 6, 5, 4, 3, 2, 1. Then splay the tree at node 1, and show all the intermediate trees being created in the process. (In the LL and LR cases, 1 is represented by the node C. In the L case it is represented by the node B.)