CSC1020 Homework 14 - Red-Black Tree Repair
Learning Outcomes
- Identify violations of red-black tree invariants.
- Determine which rotations and recoloring operations are needed to restore the invariants.
- Trace the insertion and repair of a red-black tree.
- Use recursion to validate both red-black tree invariants and binary search tree ordering.
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:
- Every node is either red (R) or black (B).
- The root of a nonempty tree is black.
- Every null child is considered an implicit black leaf.
- A red node cannot have a red child on either side.
- Every path from any node to an implicit null leaf contains the same number of black nodes. This is the black-height invariant.
- 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:
- State whether the tree is valid or invalid.
- If invalid, identify all violated invariants, not just the first violation you notice.
- Give a brief (one- or two-sentence) explanation of your reasoning. Identify the relevant nodes or paths when describing a violation.
Tree A
10B
/ \
5R 15B
/
2RTree B
20R
/ \
10B 30BTree C
25B
/ \
10B 40B
/ \
5R 50RTree D
15B
/ \
10R 20R
/ \
5B 12B
/
11RPart 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:
- 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.
- Identify the direction of each required rotation and the node at which it is performed.
- Identify any required recoloring.
- 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
/
10RCase 2
30B
/
10R
\
20RCase 3
10B
\
30R
/
20RCase 4
10B
\
20R
\
30RPart 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:
- Insert the value using standard BST ordering and color the new node red.
- Draw the tree immediately after insertion, before any repair.
- Identify any violations. If no repair is required, state why.
- Apply the standard bottom-up red-black insertion fix-up, identifying the parent, grandparent, and uncle when a red-red violation occurs.
- Show each rotation and recoloring step, labeling the operation performed.
- 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
- Return
trueifrootisnull. - If the tree is nonempty, require its root to be black.
- Check that no red node has a red left or right child.
- Treat null children as implicit black leaves when checking black-height.
- Verify that the black-height invariant holds throughout the tree, not just between the two subtrees of the root.
- Verify BST ordering throughout the tree using
compareTo(). Checking only a node's immediate children is not sufficient: values in an entire subtree must also satisfy the ordering imposed by their ancestors. - Reject duplicate values (values for which
compareTo()returns zero). - Validate the supplied tree without modifying its values, colors, or links.
- Run in O(n) time, where n is the number of nodes in the supplied tree.
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
- Part 1: validity judgments and brief explanations.
- Part 2: labeled rotations, recoloring, and resulting trees for all four cases.
- Part 3: the complete insertion and repair trace, including intermediate and final tree diagrams.
- Part 4: the completed
isValidRedBlackTree()implementation in your assignment repository.
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.