Red-Black Trees - Bryn Mawr · 7 Red-Black Trees ! Definition: A red-black tree is a binary search...
Transcript of Red-Black Trees - Bryn Mawr · 7 Red-Black Trees ! Definition: A red-black tree is a binary search...
![Page 1: Red-Black Trees - Bryn Mawr · 7 Red-Black Trees ! Definition: A red-black tree is a binary search tree in which: " Every node is colored either Red or Black. " Each NULL pointer](https://reader033.fdocuments.in/reader033/viewer/2022051807/60038daaa4ce62028e70d023/html5/thumbnails/1.jpg)
Red-Black Trees
Based on materials by Dennis Frey, Yun Peng, Jian Chen, and Daniel Hood
![Page 2: Red-Black Trees - Bryn Mawr · 7 Red-Black Trees ! Definition: A red-black tree is a binary search tree in which: " Every node is colored either Red or Black. " Each NULL pointer](https://reader033.fdocuments.in/reader033/viewer/2022051807/60038daaa4ce62028e70d023/html5/thumbnails/2.jpg)
Advanced Data Structures
n CS 206 covered basic data structures q Lists, binary search trees, heaps, hash tables
n CS 246 will introduce you to some advanced data structures and their use in applications q Red-Black Trees: a type of self-balancing BST q KD-Trees: a type of space partitioning tree q Graphs: represents a set of entities and relations
n Over the next few weeks, we will discuss these data structures, starting today with Red-Black Trees
2
![Page 3: Red-Black Trees - Bryn Mawr · 7 Red-Black Trees ! Definition: A red-black tree is a binary search tree in which: " Every node is colored either Red or Black. " Each NULL pointer](https://reader033.fdocuments.in/reader033/viewer/2022051807/60038daaa4ce62028e70d023/html5/thumbnails/3.jpg)
Quick Review of Binary Search Trees
n Given a node n... q All elements of n’s left subtree are less than n.data q All elements of n’s right subtree are greater than
n.data n We are prohibiting duplicate values n Insert/Find/Remove are O(height) (why?) n The tree’s height varies between lg N and N
q A balanced tree has height lg N
3
![Page 4: Red-Black Trees - Bryn Mawr · 7 Red-Black Trees ! Definition: A red-black tree is a binary search tree in which: " Every node is colored either Red or Black. " Each NULL pointer](https://reader033.fdocuments.in/reader033/viewer/2022051807/60038daaa4ce62028e70d023/html5/thumbnails/4.jpg)
4
Review of Tree Rotations: Zig-Zig (Node and Parent are Same Side)
Rotate P around G, then X around P
![Page 5: Red-Black Trees - Bryn Mawr · 7 Red-Black Trees ! Definition: A red-black tree is a binary search tree in which: " Every node is colored either Red or Black. " Each NULL pointer](https://reader033.fdocuments.in/reader033/viewer/2022051807/60038daaa4ce62028e70d023/html5/thumbnails/5.jpg)
5
Review of Tree Rotations: Zig-Zag (Node and Parent are Different Sides)
Rotate X around P, then X around G
![Page 6: Red-Black Trees - Bryn Mawr · 7 Red-Black Trees ! Definition: A red-black tree is a binary search tree in which: " Every node is colored either Red or Black. " Each NULL pointer](https://reader033.fdocuments.in/reader033/viewer/2022051807/60038daaa4ce62028e70d023/html5/thumbnails/6.jpg)
DEFINITIONS
6
![Page 7: Red-Black Trees - Bryn Mawr · 7 Red-Black Trees ! Definition: A red-black tree is a binary search tree in which: " Every node is colored either Red or Black. " Each NULL pointer](https://reader033.fdocuments.in/reader033/viewer/2022051807/60038daaa4ce62028e70d023/html5/thumbnails/7.jpg)
7
Red-Black Trees
n Definition: A red-black tree is a binary search tree in which: q Every node is colored either Red or Black. q Each NULL pointer is considered to be a Black “node”. q If a node is Red, then both of its children are Black. q Every path from a node to a NULL contains the same
number of Black nodes. q By convention, the root is Black
n Definition: The black-height of a node X in a red-black tree is the number of Black nodes on any path to a NULL, not counting X.
![Page 8: Red-Black Trees - Bryn Mawr · 7 Red-Black Trees ! Definition: A red-black tree is a binary search tree in which: " Every node is colored either Red or Black. " Each NULL pointer](https://reader033.fdocuments.in/reader033/viewer/2022051807/60038daaa4ce62028e70d023/html5/thumbnails/8.jpg)
8
A Red-Black Tree with NULLs shown
Black-Height of the tree (the root) = 3 Black-Height of node “X” = 2
X
![Page 9: Red-Black Trees - Bryn Mawr · 7 Red-Black Trees ! Definition: A red-black tree is a binary search tree in which: " Every node is colored either Red or Black. " Each NULL pointer](https://reader033.fdocuments.in/reader033/viewer/2022051807/60038daaa4ce62028e70d023/html5/thumbnails/9.jpg)
9
A Red-Black Tree with
Black-Height = 3
![Page 10: Red-Black Trees - Bryn Mawr · 7 Red-Black Trees ! Definition: A red-black tree is a binary search tree in which: " Every node is colored either Red or Black. " Each NULL pointer](https://reader033.fdocuments.in/reader033/viewer/2022051807/60038daaa4ce62028e70d023/html5/thumbnails/10.jpg)
10
Black Height of the tree?
Black Height of X?
X
![Page 11: Red-Black Trees - Bryn Mawr · 7 Red-Black Trees ! Definition: A red-black tree is a binary search tree in which: " Every node is colored either Red or Black. " Each NULL pointer](https://reader033.fdocuments.in/reader033/viewer/2022051807/60038daaa4ce62028e70d023/html5/thumbnails/11.jpg)
11
Theorem 1 – Any red-black tree with root x, has n ≥ 2bh(x) – 1 nodes, where bh(x) is the black height of node x.
Proof: by induction on height of x.
![Page 12: Red-Black Trees - Bryn Mawr · 7 Red-Black Trees ! Definition: A red-black tree is a binary search tree in which: " Every node is colored either Red or Black. " Each NULL pointer](https://reader033.fdocuments.in/reader033/viewer/2022051807/60038daaa4ce62028e70d023/html5/thumbnails/12.jpg)
12
Theorem 2 – In a red-black tree, at least half the nodes on any path from the root to a NULL must be Black.
Proof – If there is a Red node on the path,
there must be a corresponding Black node.
Algebraically this theorem means
bh( x ) ≥ h/2
![Page 13: Red-Black Trees - Bryn Mawr · 7 Red-Black Trees ! Definition: A red-black tree is a binary search tree in which: " Every node is colored either Red or Black. " Each NULL pointer](https://reader033.fdocuments.in/reader033/viewer/2022051807/60038daaa4ce62028e70d023/html5/thumbnails/13.jpg)
13
Theorem 3 – In a red-black tree, no path from any node, X, to a NULL is more than twice as long as any other path from X to any other NULL.
Proof: By definition, every path from a node to any
NULL contains the same number of Black nodes. By Theorem 2, a least ½ the nodes on any such path are Black. Therefore, there can no more than twice as many nodes on any path from X to a NULL as on any other path. Therefore the length of every path is no more than twice as long as any other path.
![Page 14: Red-Black Trees - Bryn Mawr · 7 Red-Black Trees ! Definition: A red-black tree is a binary search tree in which: " Every node is colored either Red or Black. " Each NULL pointer](https://reader033.fdocuments.in/reader033/viewer/2022051807/60038daaa4ce62028e70d023/html5/thumbnails/14.jpg)
14
Theorem 4 – A red-black tree with n nodes has height h ≤ 2 lg(n + 1).
Proof: Let h be the height of the red-black tree with root x. By Theorem 2,
bh(x) ≥ h/2 From Theorem 1, n ≥ 2bh(x) - 1 Therefore n ≥ 2 h/2 – 1
n + 1 ≥ 2h/2
lg(n + 1) ≥ h/2 2lg(n + 1) ≥ h
![Page 15: Red-Black Trees - Bryn Mawr · 7 Red-Black Trees ! Definition: A red-black tree is a binary search tree in which: " Every node is colored either Red or Black. " Each NULL pointer](https://reader033.fdocuments.in/reader033/viewer/2022051807/60038daaa4ce62028e70d023/html5/thumbnails/15.jpg)
BOTTOM-UP INSERTION
15
![Page 16: Red-Black Trees - Bryn Mawr · 7 Red-Black Trees ! Definition: A red-black tree is a binary search tree in which: " Every node is colored either Red or Black. " Each NULL pointer](https://reader033.fdocuments.in/reader033/viewer/2022051807/60038daaa4ce62028e70d023/html5/thumbnails/16.jpg)
16
Bottom –Up Insertion
n Insert node as usual in BST n Color the node Red n What Red-Black property may be violated?
q Every node is Red or Black? q NULLs are Black? q If node is Red, both children must be Black? q Every path from node to descendant NULL must
contain the same number of Blacks?
![Page 17: Red-Black Trees - Bryn Mawr · 7 Red-Black Trees ! Definition: A red-black tree is a binary search tree in which: " Every node is colored either Red or Black. " Each NULL pointer](https://reader033.fdocuments.in/reader033/viewer/2022051807/60038daaa4ce62028e70d023/html5/thumbnails/17.jpg)
17
Bottom Up Insertion
n Insert node; Color it Red; X is pointer to it n Cases
0: X is the root -- color it Black 1: Both parent and uncle are Red -- color parent and
uncle Black, color grandparent Red. Point X to grandparent and check new situation.
2 (zig-zag): Parent is Red, but uncle is Black. X and its parent are opposite type children -- color grandparent Red, color X Black, rotate left(right) on parent, rotate right(left) on grandparent
3 (zig-zig): Parent is Red, but uncle is Black. X and its parent are both left (right) children -- color parent Black, color grandparent Red, rotate right(left) on grandparent
![Page 18: Red-Black Trees - Bryn Mawr · 7 Red-Black Trees ! Definition: A red-black tree is a binary search tree in which: " Every node is colored either Red or Black. " Each NULL pointer](https://reader033.fdocuments.in/reader033/viewer/2022051807/60038daaa4ce62028e70d023/html5/thumbnails/18.jpg)
18
X
P
G
U
P
G
U
Case 1 – U is Red
Just Recolor and move up
X
![Page 19: Red-Black Trees - Bryn Mawr · 7 Red-Black Trees ! Definition: A red-black tree is a binary search tree in which: " Every node is colored either Red or Black. " Each NULL pointer](https://reader033.fdocuments.in/reader033/viewer/2022051807/60038daaa4ce62028e70d023/html5/thumbnails/19.jpg)
19
X
P
G
U
S X
P G
SU
Case 2 – Zig-Zag
Double Rotate X around P; X around G
Recolor G and X
![Page 20: Red-Black Trees - Bryn Mawr · 7 Red-Black Trees ! Definition: A red-black tree is a binary search tree in which: " Every node is colored either Red or Black. " Each NULL pointer](https://reader033.fdocuments.in/reader033/viewer/2022051807/60038daaa4ce62028e70d023/html5/thumbnails/20.jpg)
20
X
P
G
U
S P
X G
S U
Case 3 – Zig-Zig
Single Rotate P around G
Recolor P and G
![Page 21: Red-Black Trees - Bryn Mawr · 7 Red-Black Trees ! Definition: A red-black tree is a binary search tree in which: " Every node is colored either Red or Black. " Each NULL pointer](https://reader033.fdocuments.in/reader033/viewer/2022051807/60038daaa4ce62028e70d023/html5/thumbnails/21.jpg)
21
Asymptotic Cost of Insertion
n O(lg n) to descend to insertion point n O(1) to do insertion n O(lg n) to ascend and readjust == worst case
only for case 1
n Total: O(lg n)
![Page 22: Red-Black Trees - Bryn Mawr · 7 Red-Black Trees ! Definition: A red-black tree is a binary search tree in which: " Every node is colored either Red or Black. " Each NULL pointer](https://reader033.fdocuments.in/reader033/viewer/2022051807/60038daaa4ce62028e70d023/html5/thumbnails/22.jpg)
22
11
14
15 2
1 7
5 8
Black node Red node
Insert 4 into this R-B Tree
![Page 23: Red-Black Trees - Bryn Mawr · 7 Red-Black Trees ! Definition: A red-black tree is a binary search tree in which: " Every node is colored either Red or Black. " Each NULL pointer](https://reader033.fdocuments.in/reader033/viewer/2022051807/60038daaa4ce62028e70d023/html5/thumbnails/23.jpg)
23
Insertion Practice
Insert the values 2, 1, 4, 5, 9, 3, 6, 7 into an initially empty Red-Black Tree
![Page 24: Red-Black Trees - Bryn Mawr · 7 Red-Black Trees ! Definition: A red-black tree is a binary search tree in which: " Every node is colored either Red or Black. " Each NULL pointer](https://reader033.fdocuments.in/reader033/viewer/2022051807/60038daaa4ce62028e70d023/html5/thumbnails/24.jpg)
24
Top-Down Insertion
An alternative to this “bottom-up” insertion is “top-down” insertion.
Top-down is iterative. It moves down the tree, “fixing” things as it goes.
What is the objective of top-down’s “fixes”?
![Page 25: Red-Black Trees - Bryn Mawr · 7 Red-Black Trees ! Definition: A red-black tree is a binary search tree in which: " Every node is colored either Red or Black. " Each NULL pointer](https://reader033.fdocuments.in/reader033/viewer/2022051807/60038daaa4ce62028e70d023/html5/thumbnails/25.jpg)
BOTTOM-UP DELETION
25
![Page 26: Red-Black Trees - Bryn Mawr · 7 Red-Black Trees ! Definition: A red-black tree is a binary search tree in which: " Every node is colored either Red or Black. " Each NULL pointer](https://reader033.fdocuments.in/reader033/viewer/2022051807/60038daaa4ce62028e70d023/html5/thumbnails/26.jpg)
26
Recall “ordinary” BST Delete
1. If node to be deleted is a leaf, just delete it. 2. If node to be deleted has just one child,
replace it with that child (splice) 3. If node to be deleted has two children,
replace the value in the node by its in-order predecessor/successor’s value then delete the in-order predecessor/successor (a recursive step)
![Page 27: Red-Black Trees - Bryn Mawr · 7 Red-Black Trees ! Definition: A red-black tree is a binary search tree in which: " Every node is colored either Red or Black. " Each NULL pointer](https://reader033.fdocuments.in/reader033/viewer/2022051807/60038daaa4ce62028e70d023/html5/thumbnails/27.jpg)
27
Bottom-Up Deletion
1. Do ordinary BST deletion. Eventually a “case 1” or “case 2” deletion will be done (leaf or just one child). -- If deleted node, U, is a leaf, think of
deletion as replacing U with the NULL pointer, V. -- If U had one child, V, think of deletion
as replacing U with V. 2. What can go wrong??
![Page 28: Red-Black Trees - Bryn Mawr · 7 Red-Black Trees ! Definition: A red-black tree is a binary search tree in which: " Every node is colored either Red or Black. " Each NULL pointer](https://reader033.fdocuments.in/reader033/viewer/2022051807/60038daaa4ce62028e70d023/html5/thumbnails/28.jpg)
28
Which RB Property may be violated after deletion? 1. If U is Red?
Not a problem – no RB properties violated
2. If U is Black? If U is not the root, deleting it will change the black-height along some path
![Page 29: Red-Black Trees - Bryn Mawr · 7 Red-Black Trees ! Definition: A red-black tree is a binary search tree in which: " Every node is colored either Red or Black. " Each NULL pointer](https://reader033.fdocuments.in/reader033/viewer/2022051807/60038daaa4ce62028e70d023/html5/thumbnails/29.jpg)
29
Fixing the problem
n Think of V as having an “extra” unit of blackness. This extra blackness must be absorbed into the tree (by a red node), or propagated up to the root and out of the tree.
n There are four cases – our examples and “rules” assume that V is a left child. There are symmetric cases for V as a right child.
![Page 30: Red-Black Trees - Bryn Mawr · 7 Red-Black Trees ! Definition: A red-black tree is a binary search tree in which: " Every node is colored either Red or Black. " Each NULL pointer](https://reader033.fdocuments.in/reader033/viewer/2022051807/60038daaa4ce62028e70d023/html5/thumbnails/30.jpg)
30
Terminology
n The node just deleted was U n The node that replaces it is V, which has
an extra unit of blackness n The parent of V is P n The sibling of V is S
Black Node
Red Node
Red or Black and don’t care
![Page 31: Red-Black Trees - Bryn Mawr · 7 Red-Black Trees ! Definition: A red-black tree is a binary search tree in which: " Every node is colored either Red or Black. " Each NULL pointer](https://reader033.fdocuments.in/reader033/viewer/2022051807/60038daaa4ce62028e70d023/html5/thumbnails/31.jpg)
31
Bottom-Up Deletion Case 1
n V’s sibling, S, is Red q Rotate S around P and recolor S & P
n NOT a terminal case – One of the other cases will now apply
n All other cases apply when S is Black
![Page 32: Red-Black Trees - Bryn Mawr · 7 Red-Black Trees ! Definition: A red-black tree is a binary search tree in which: " Every node is colored either Red or Black. " Each NULL pointer](https://reader033.fdocuments.in/reader033/viewer/2022051807/60038daaa4ce62028e70d023/html5/thumbnails/32.jpg)
32
Case 1 Diagram
P
SV+ P
S
V+
Rotate S around P
P
V+
S
Recolor S & P
![Page 33: Red-Black Trees - Bryn Mawr · 7 Red-Black Trees ! Definition: A red-black tree is a binary search tree in which: " Every node is colored either Red or Black. " Each NULL pointer](https://reader033.fdocuments.in/reader033/viewer/2022051807/60038daaa4ce62028e70d023/html5/thumbnails/33.jpg)
33
Bottom-Up Deletion Case 2 n V’s sibling, S, is Black and has two Black
children. q Recolor S to be Red q P absorbs V’s extra blackness
n If P is Red, we’re done (it absorbed the blackness) n If P is Black, it now has extra blackness and problem
has been propagated up the tree
![Page 34: Red-Black Trees - Bryn Mawr · 7 Red-Black Trees ! Definition: A red-black tree is a binary search tree in which: " Every node is colored either Red or Black. " Each NULL pointer](https://reader033.fdocuments.in/reader033/viewer/2022051807/60038daaa4ce62028e70d023/html5/thumbnails/34.jpg)
34
Case 2 diagram
P
SV+
P+
SV
Recolor S P absorbs blackness
Either extra Black absorbed by P
or
P now has extra blackness
![Page 35: Red-Black Trees - Bryn Mawr · 7 Red-Black Trees ! Definition: A red-black tree is a binary search tree in which: " Every node is colored either Red or Black. " Each NULL pointer](https://reader033.fdocuments.in/reader033/viewer/2022051807/60038daaa4ce62028e70d023/html5/thumbnails/35.jpg)
35
Bottom-Up Deletion Case 3
n S is Black n S’s right child is RED (Left child either color)
q Rotate S around P q Swap colors of S and P,
and color S’s right child Black
n This is the terminal case – we’re done
![Page 36: Red-Black Trees - Bryn Mawr · 7 Red-Black Trees ! Definition: A red-black tree is a binary search tree in which: " Every node is colored either Red or Black. " Each NULL pointer](https://reader033.fdocuments.in/reader033/viewer/2022051807/60038daaa4ce62028e70d023/html5/thumbnails/36.jpg)
36
Case 3 diagrams
P
SV+ P
S
V+
Rotate S around P
P
S
V Swap colors of S & P Color S’s right child Black
![Page 37: Red-Black Trees - Bryn Mawr · 7 Red-Black Trees ! Definition: A red-black tree is a binary search tree in which: " Every node is colored either Red or Black. " Each NULL pointer](https://reader033.fdocuments.in/reader033/viewer/2022051807/60038daaa4ce62028e70d023/html5/thumbnails/37.jpg)
37
Bottom-Up Deletion Case 4 n S is Black, S’s right child is Black and S’s
left child is Red q Rotate S’s left child around S q Swap color of S and S’s left child q Now in case 3
![Page 38: Red-Black Trees - Bryn Mawr · 7 Red-Black Trees ! Definition: A red-black tree is a binary search tree in which: " Every node is colored either Red or Black. " Each NULL pointer](https://reader033.fdocuments.in/reader033/viewer/2022051807/60038daaa4ce62028e70d023/html5/thumbnails/38.jpg)
38
Case 4 Diagrams
P
SV+
P
SV+
Rotate S’s left around S
P
SV+
Swap colors of S and S’s original left child
![Page 39: Red-Black Trees - Bryn Mawr · 7 Red-Black Trees ! Definition: A red-black tree is a binary search tree in which: " Every node is colored either Red or Black. " Each NULL pointer](https://reader033.fdocuments.in/reader033/viewer/2022051807/60038daaa4ce62028e70d023/html5/thumbnails/39.jpg)
39
Top-Down Deletion
An alternative to the recursive “bottom-up” deletion is “top-down” deletion.
This method is iterative. It moves down the tree only, “fixing” things as it goes.
What is the goal of top-down deletion?
![Page 40: Red-Black Trees - Bryn Mawr · 7 Red-Black Trees ! Definition: A red-black tree is a binary search tree in which: " Every node is colored either Red or Black. " Each NULL pointer](https://reader033.fdocuments.in/reader033/viewer/2022051807/60038daaa4ce62028e70d023/html5/thumbnails/40.jpg)
40
65
50 80
10 60 70 90
62
Perform the following deletions, in the order specified Delete 90, Delete 80, Delete 70