| I. Properties of Red-Black Trees
Red-Black Trees adhere to 2 main conditions:
- There can be no 2 consecutive Red edges
- The number of black nodes from the root to each leaf must be the same
II. Insertion
There are 3 possible cases for insertion into a Red-Black
Tree:
A. Case 1 No Conflicts
- Leaf nodes have empty children; the edges connecting these empty children
are always black
- A new node is always added with a red edge
- If a new
node is added with a red edge, and its parent has a black edge, there is no
conflict
B. Case 2 Two consecutive Red Edges -- Sibing Node of first Red
Edge is BLACK
Reorganize Nodes
- Find X,Y,Z:
- Z = Parent node before the 2 consecutive Red edges
- Y
= Node between both Red edges (parent edge & child edge both red)
- X =
Child node after the 2 red edges
- Find A,B,C:
- Use In-Order traversal
methodology to determine which nodes (of X/Y/Z) will be re-labelled A,B,&C.
Rearrange Nodes
- Reconnect B with Black edge
- Connect A & C as B's children with Red
edges
- Have A & C's children connected with Black edges
C. Case 3 Two consecutive Red Edges -- Sibing Node of first Red
Edge is RED
Recolor Edges
- Assume that the node owning the first Red edge is labelled N
- Reset
the Red edge of N's sibling as Black
- Also, recolor N's Red edge itself as
Black
- Recolor the incoming edge of N's parent as Red
II. Deletion
There is never a conflict when deleting leaf nodes with red
edges...
If the node to delete is not a leaf, we must substitute a value from lower in the tree --
the value is determined by In-Order traversal (whichever value directly
precedes the one to be removed)
A. Case 1 Leaf node with Red connection -- No
conflicts; just remove
B. Case 2 Internal Node
- Find In-Order predecessor & substitute
- Remove node of the predecessor
that was swapped in
B. Case 3 Leaf node with Black connection
- Remove node as normal, but mark the connecting edge as "Double Black"
- Rectify the Double Black (DB) situation, as in the following cases:
- DOUBLE BLACK Case 1:
Sibling to DB edge is BLACK, with 1-2 RED children
- Order & Rotate
- Define Nodes X,Y,Z:
- Z = Parent of DB edge
- Y = Sibling to DB edge
- X = One of the Red children of Y
- Determine Nodes A,B,C by traversing X,Y,Z In-Order
- Re-Organize A,B,C as usual
- DOUBLE BLACK Case 2:
Sibling to DB edge is BLACK, with 2 BLACK children
- Recolor
- Add a Black edge to the connecting edge of DB parent (making this DB, if
necessary)
- Make connecting edge of DB sibling Red
- Continue upward, if another DB situation was created
- DOUBLE BLACK Case 3:
Sibling to DB edge is RED
- Shift
- Move the sibling of the DB edge up a level (connect it between the node
above the DB's parent and DB's parent)
- A new sibling will now be connected as the sibling to the DB edge, and it
should have a Black edge.
- Handle the new situation (Case 1 or 2), as above
|
--- Diagrams to Come --- |