RedBlack Tree Neil Tang 02042010 CS 223 Advanced
Red-Black Tree Neil Tang 02/04/2010 CS 223 Advanced Data Structures and Algorithms 1
Class Overview Ø Definition Ø Tree height Ø Rotation and color flip Ø Insert CS 223 Advanced Data Structures and Algorithms 2
Definition A red-black tree is a special binary search tree in which Ø every node is either red or black; Ø the root is black; Ø if a node is red, then both its children are black; Ø every simple path from a node to a null reference contains the same number of black nodes. CS 223 Advanced Data Structures and Algorithms 3
Definition CS 223 Advanced Data Structures and Algorithms 4
Height Ø Theorem: A red-black tree with N nodes has height at most 2 log(N+1). (pp. 274 Cormen’s book) Ø Experiments suggests that the average red-black tree is about as deep as an average AVL tree. Ø The rotation happens less frequently. CS 223 Advanced Data Structures and Algorithms 5
An Example CS 223 Advanced Data Structures and Algorithms 6
Insertion Ø Always color the new node to be inserted red. Remember that the new node will always be a leaf node right after insertion. Ø If its parent is black, done! Ø If its parent is red, perform a single (double) rotation! CS 223 Advanced Data Structures and Algorithms 7
Rotation if S is Black CS 223 Advanced Data Structures and Algorithms 8
Color Flip if S is red CS 223 Advanced Data Structures and Algorithms 9
Example: Insert 45 CS 223 Advanced Data Structures and Algorithms 10
Insert CS 223 Advanced Data Structures and Algorithms 11
Insert CS 223 Advanced Data Structures and Algorithms 12
Insert CS 223 Advanced Data Structures and Algorithms 13
- Slides: 13