CSC1020 Homework 14 - Red-Black Tree Repair

Learning Outcomes

Background

A RedBlackTree is a self-balancing binary search tree. Its color and ordering invariants keep its height logarithmic in the number of nodes, allowing search, insertion, and deletion to run in O(log n) time.

A valid red-black tree satisfies the following invariants:

  1. Every node is either red (R) or black (B).
  2. The root of a nonempty tree is black.
  3. Every null child is considered an implicit black leaf.
  4. A red node cannot have a red child on either side.
  5. Every path from any node to an implicit null leaf contains the same number of black nodes. This is the black-height invariant.
  6. The tree satisfies binary search tree (BST) ordering: all values in a node's left subtree are strictly smaller than its value, and all values in its right subtree are strictly larger. Values for which compareTo() returns zero are duplicates and are not permitted.

The diagrams below label each node with its value and color. Missing children are implicit black null leaves; they are omitted from the diagrams for clarity. For this assignment, count the implicit null leaf when calculating black-height. Use the same counting convention for every path.

An empty tree is valid.

Part 1: Invariant Check

For each of the following trees:

Tree A

        10B
       /   \
     5R     15B
    /
  2R

Tree B

        20R
       /   \
     10B   30B

Tree C

        25B
       /   \
     10B   40B
    /         \
   5R         50R

Tree D

        15B
       /   \
     10R   20R
    /   \
   5B   12B
        /
      11R

Part 2: Rotation Identification

Each structure below represents a red-red violation following an insertion. The grandparent is black, and the uncle is an implicit black null leaf.

For each structure:

  1. Identify the rotation case (LL, LR, RL, or RR), describing the path from the grandparent to the parent and then to the newly inserted node.
  2. Identify the direction of each required rotation and the node at which it is performed.
  3. Identify any required recoloring.
  4. Draw the resulting tree, including the color of every node. For a double rotation, show the intermediate tree as well.

The final tree must satisfy every red-black tree invariant and maintain BST ordering.

Case 1

        30B
       /
     20R
    /
  10R

Case 2

        30B
       /
     10R
       \
       20R

Case 3

        10B
          \
          30R
         /
       20R

Case 4

        10B
          \
          20R
            \
            30R

Part 3: Insert-and-Fix Trace

Begin with this valid red-black tree:

        20B
       /   \
     10B   30B

Insert the following values in order, using the result of each insertion as the starting tree for the next insertion:

5, 1, 15

For each insertion:

  1. Insert the value using standard BST ordering and color the new node red.
  2. Draw the tree immediately after insertion, before any repair.
  3. Identify any violations. If no repair is required, state why.
  4. Apply the standard bottom-up red-black insertion fix-up, identifying the parent, grandparent, and uncle when a red-red violation occurs.
  5. Show each rotation and recoloring step, labeling the operation performed.
  6. Draw the final valid tree before proceeding to the next insertion.

Remember that a red uncle requires a different repair from a black uncle. A missing uncle is treated as black. If a repair propagates a violation toward the root, continue repairing until the tree is valid, and ensure the root is black.

Part 4: Validator Implementation

The starter code provides the binary tree, binary search tree, rotation, and red-black tree implementations. You do not need to implement insertion, deletion, or rotations for this part.

Complete the following method in RedBlackTree:

public boolean isValidRedBlackTree(RedBlackNode<E> root)

The method must return true if the tree rooted at the supplied node satisfies all red-black tree invariants and BST ordering, and false otherwise.

Requirements

You may assume that the supplied tree consists of red-black nodes with non-null comparable values and has an ordinary tree structure (no cycles or shared child nodes). The validator must examine the tree passed as the parameter, rather than assuming it is the tree stored in this.root.

Use recursion to examine the subtrees. Consider how a recursive helper could report both whether a subtree is valid and its black-height without traversing the same subtree repeatedly. You may add private helper methods or supporting types as needed.

Test your implementation using the supplied test suite. The tests construct tree shapes directly so that they can exercise both valid and invalid trees independently of the provided insertion and deletion methods.

Submission Requirements

Trees may be drawn by hand or digitally, provided that all values, colors, links, and operations are clear. Follow your instructor's submission and due date instructions for the written work and code.