CSS 343: Notes from Lecture 5 (DRAFT)

Administrivia

Tree Rotations

AVL Tree

BST Deletion

3 cases:

  1. leaf node: delete
  2. single-child node: replace with child
  3. two-child node: replace node with immediate predecessor or successor, then delete original predecessor/successor

Additional operations may be performed to maintain balance (e.g. AVL & red-black).

Red-Black Tree

Example: red-black tree construction (click on image to enlarge) or download frame-by-frame zip. [red-black tree construction]

Also see the wikipedia article.

A red-black tree is a Binary Search Tree in which each node is labeled by a color, red or black (it could be called up/down, true/false, 0/1, but red/black was chosen). The tree has the following rules (invariants) that must be maintained:

Implication:

A parent pointer is not required, but makes coding so very much easier.

Trying to hand-label some arbitrary binary tree is hard and confusing

Insertion

Considering only insertion; deletion is similar.

Insert a new entry in the tree in the location where it would normally be inserted (ignoring the synthetic black leaves). Label the new node red. It will have two black (synthetic) children. Rebalance the tree to make sure the red-black conditions (invariants) are maintained.

In keeping with the geneology theme, the parent node of a parent node is a grandparent and the sibling of a parent node is an uncle. Geneological Naming Convention

Case-by-case analysis:

  1. node is root: relabel node black and return
  2. parent is black: return
  3. parent and uncle are both red: relabel parent and uncle black and relabel Grandparent as red. Repeat with Grandparent node
  4. parent is red but uncle is black and node is left child of parent but parent is right child of grandparent or node is right child, but parent is left child (left-right or right-left cases): rotate so node is parent of original parent and original parent is child of node and proceed to case 5 setting node to original parent.
  5. node is left child of parent and parent is left child of grandparent or node is right child and parent is right child (left-left or right-right cases): relabel parent black and grandparent red, then rotate parent to grandparent position and return.