Download presentation
Presentation is loading. Please wait.
1
Red-Black Tree Neil Tang 02/07/2008
CS223 Advanced Data Structures and Algorithms
2
CS223 Advanced Data Structures and Algorithms
Class Overview Definition Tree height Rotation and color flip Insert Delete CS223 Advanced Data Structures and Algorithms
3
CS223 Advanced Data Structures and Algorithms
Definition A red-black tree is a special binary search tree in which every node is either red or black; the root is black; every leaf (null) is black; if a node is red, then both its children are black; every simple path from a node to a descendant leaf contains the same number of black nodes. CS223 Advanced Data Structures and Algorithms
4
CS223 Advanced Data Structures and Algorithms
Height Theorem: A red-black tree with N nodes has height at most 2log(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. CS223 Advanced Data Structures and Algorithms
5
CS223 Advanced Data Structures and Algorithms
An Example CS223 Advanced Data Structures and Algorithms
6
CS223 Advanced Data Structures and Algorithms
Rotation if S is Black CS223 Advanced Data Structures and Algorithms
7
CS223 Advanced Data Structures and Algorithms
Color Flip if S is red CS223 Advanced Data Structures and Algorithms
8
CS223 Advanced Data Structures and Algorithms
Example: Insert 45 CS223 Advanced Data Structures and Algorithms
9
CS223 Advanced Data Structures and Algorithms
Insert CS223 Advanced Data Structures and Algorithms
10
CS223 Advanced Data Structures and Algorithms
Insert CS223 Advanced Data Structures and Algorithms
11
CS223 Advanced Data Structures and Algorithms
Insert CS223 Advanced Data Structures and Algorithms
12
CS223 Advanced Data Structures and Algorithms
Delete 15 15 5 3 10 6 7 23 16 18 20 12 5 16 Case 1: 3 12 20 18 23 10 13 6 7 15 5 3 10 6 7 23 16 18 20 13 12 15 5 3 10 6 7 23 18 20 13 12 Case 2: 15 5 3 10 6 7 23 16 18 20 13 12 15 5 3 6 23 16 18 20 13 12 10 7 Case 3: CS223 Advanced Data Structures and Algorithms
13
CS223 Advanced Data Structures and Algorithms
Delete In terms of deletion, everything boils down to delete a leaf. Deletion of a red leaf is trivial. How to delete a black leaf: Change the color of the leaf to be deleted to red by a top-down pass. CS223 Advanced Data Structures and Algorithms
14
Three Cases When X Has Two Black Children
CS223 Advanced Data Structures and Algorithms
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.